-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
173 lines (147 loc) · 3.72 KB
/
loader.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gofigure
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type Loader struct {
features []Feature
root *Node
}
func New() *Loader {
return &Loader{}
}
func (l *Loader) WithFeatures(features ...Feature) *Loader {
l.features = append(l.features, features...)
return l
}
func (l *Loader) Load(name string, contents []byte) error {
name = filepath.Clean(name)
name = strings.TrimSuffix(name, filepath.Ext(name))
var yamlNode yaml.Node
if err := yaml.Unmarshal(contents, &yamlNode); err != nil {
return fmt.Errorf("unable to unmarshal file %q: %w", name, errors.Join(err, ErrConfigParseError))
}
// root node is a document node, and the first child is a map holds all the values
fileNode := NewNode(yamlNode.Content[0], NodeFilepath(name))
if name != "" {
names := strings.Split(name, string(filepath.Separator))
// nest the file with its path, e.g, config/app.yaml -> config.app
fileNode = PackNodeInNestedKeys(fileNode, names...)
}
if l.root == nil {
l.root = fileNode
} else {
newNode, err := mergeToNode(l.root, fileNode)
if err != nil {
return fmt.Errorf("unable to merge file %q: %w", name, errors.Join(err, ErrConfigParseError))
}
l.root = newNode
}
return nil
}
func (l *Loader) Get(ctx context.Context, path string, target any) error {
node, err := l.GetNode(ctx, path)
if err != nil {
return err
}
if node == nil {
return nil
}
return node.ToYAMLNode().Decode(target)
}
func (l *Loader) GetNode(ctx context.Context, path string) (*Node, error) {
current := l.root
if len(path) > 0 {
paths, err := ParseDotPath(path)
if err != nil {
return nil, fmt.Errorf("unable to parse path %q: %w", path, err)
}
for _, p := range paths {
if current == nil {
break
}
if p.Key != "" { // map
current, err = current.GetMappingChild(p.Key)
if err != nil {
return nil, err
}
} else { // slice
current, err = current.GetSequenceChild(p.Index)
if err != nil {
return nil, err
}
}
if current == nil {
break
}
// always try to resolve the node, so if it has resolvedNode, it will be used instead
current, err = l.resolve(ctx, current)
if err != nil {
return nil, err
}
if current == nil {
break
}
}
}
if current == nil {
return nil, nil
}
return l.resolve(ctx, current)
}
func (l *Loader) resolve(ctx context.Context, node *Node) (resultNode *Node, reterr error) {
if node.resolved {
// if the node is resolved by tagged resolver, the result is stored in resolvedNode (so the original value can be preserved)
if node.resolvedNode != nil {
return node.resolvedNode, nil
}
return node, nil
}
// set it to true first to avoid infinite loop
node.resolved = true
defer func() {
if reterr != nil {
node.resolved = false
}
}()
// resolve children first for mapping and sequence nodes
if node.kind == yaml.MappingNode {
for key := range node.mappingNodes {
childNode, err := l.resolve(ctx, node.mappingNodes[key])
if err != nil {
return nil, err
}
node.mappingNodes[key] = childNode
}
}
if node.kind == yaml.SequenceNode {
for i := range node.sequenceNodes {
childNode, err := l.resolve(ctx, node.sequenceNodes[i])
if err != nil {
return nil, err
}
node.sequenceNodes[i] = childNode
}
}
// if not tagged, no further action is needed
if node.style&yaml.TaggedStyle == 0 {
return node, nil
}
// resolve the node with the feature if matched
for _, feature := range l.features {
if feature.Name() == node.tag {
result, err := feature.Resolve(ctx, l, node)
if err != nil {
node.resolved = false
return nil, newNodeError(node, err)
}
node.resolvedNode = result
return node, nil
}
}
return node, nil
}