-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
54 lines (44 loc) · 1.4 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package resolver
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// Resolves a value by loading a YAML file and extracting a nested key using dot notation.
// Similar usage as JSONResolver:
// "yaml:/config/app.yaml//server.host"
type YAMLResolver struct{}
func (r *YAMLResolver) Resolve(value string) (string, error) {
filePath, keyPath := splitFileAndKey(value)
filePath = os.ExpandEnv(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read YAML file '%s': %w", filePath, err)
}
var content interface{}
if err := yaml.Unmarshal(data, &content); err != nil {
return "", fmt.Errorf("failed to parse YAML in '%s': %w", filePath, err)
}
// Convert YAML to map[string]interface{} if needed
contentMap, err := convertToMapStringInterface(content)
if err != nil {
return "", fmt.Errorf("failed to process YAML '%s': %w", filePath, err)
}
if keyPath == "" {
// Return whole file as YAML string
return strings.TrimSpace(string(data)), nil
}
val, err := navigateData(contentMap, strings.Split(keyPath, "."))
if err != nil {
return "", fmt.Errorf("key path '%s' not found in YAML '%s': %w", keyPath, filePath, err)
}
// If the value isn't a string, return its YAML representation
switch typedVal := val.(type) {
case string:
return typedVal, nil
default:
yData, _ := yaml.Marshal(typedVal)
return strings.TrimSpace(string(yData)), nil
}
}