-
Notifications
You must be signed in to change notification settings - Fork 14
/
yaml.go
66 lines (55 loc) · 1.27 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
55
56
57
58
59
60
61
62
63
64
65
66
package util
import (
"bytes"
"strings"
yamlv3 "gopkg.in/yaml.v3"
)
func MarshalYaml(node *yamlv3.Node) ([]byte, error) {
buf := bytes.Buffer{}
enc := yamlv3.NewEncoder(&buf)
enc.SetIndent(2)
err := enc.Encode(node)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func FindNodeByPath(node *yamlv3.Node, path string) (*yamlv3.Node, *yamlv3.Node) {
if node == nil || path == "" {
return nil, nil
}
var keyNode, valueNode *yamlv3.Node
if node.Kind == yamlv3.DocumentNode {
valueNode = node.Content[0]
} else {
valueNode = node
}
splittedPath := strings.Split(path, ".")
for _, p := range splittedPath {
keyNode, valueNode = findNode(valueNode.Content, p)
if keyNode == nil && valueNode == nil {
break
}
}
return keyNode, valueNode
}
func findNode(nodes []*yamlv3.Node, name string) (*yamlv3.Node, *yamlv3.Node) {
if nodes == nil {
return nil, nil
}
var keyNode, valueNode *yamlv3.Node
for i, node := range nodes {
if node.Value == name {
keyNode = node
if i < len(nodes)-1 {
valueNode = nodes[i+1]
}
} else if node.Kind == yamlv3.SequenceNode || node.Kind == yamlv3.MappingNode {
keyNode, valueNode = findNode(node.Content, name)
}
if keyNode != nil && valueNode != nil {
break
}
}
return keyNode, valueNode
}