smartconfig/resolver_json.go
sneak 8a38afba5e passes tests, has cli filter now.
* still has not been *really* tested yet
2025-07-20 15:29:06 +02:00

43 lines
1.1 KiB
Go

package smartconfig
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// JSONResolver reads values from JSON files.
// Usage: ${JSON:/path/to/file.json:json.path}
type JSONResolver struct{}
// Resolve reads a JSON file and extracts the value at the specified path.
func (r *JSONResolver) Resolve(value string) (string, error) {
parts := strings.SplitN(value, ":", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid JSON resolver format, expected FILE:PATH")
}
filePath := parts[0]
jsonPath := parts[1]
data, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read JSON file %s: %w", filePath, err)
}
var jsonData interface{}
if err := json.Unmarshal(data, &jsonData); err != nil {
return "", fmt.Errorf("failed to parse JSON: %w", err)
}
// Simple JSON path evaluation (would need a proper library for complex paths)
if jsonPath == "." {
return fmt.Sprintf("%v", jsonData), nil
}
// This is a simplified implementation
// In production, use a proper JSON path library
return fmt.Sprintf("%v", jsonData), nil
}