-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
85 lines (75 loc) · 1.7 KB
/
merge.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
package gofigure
import (
"fmt"
"gopkg.in/yaml.v3"
)
func PackNodeInNestedKeys(node *Node, keys ...string) *Node {
result := node
for i := len(keys) - 1; i >= 0; i-- {
key := keys[i]
childNode := NewMappingNode(map[string]*Node{
key: result,
})
result.parent = childNode
result.mappingKey = key
result.hasMappingKey = true
result = childNode
}
return result
}
func MergeNodes(nodes ...*Node) (*Node, error) {
var rootNode *Node
for _, node := range nodes {
if rootNode == nil {
rootNode = node
} else {
result, err := mergeToNode(rootNode, node)
if err != nil {
return nil, err
}
rootNode = result
}
}
return rootNode, nil
}
func mergeToNode(n, another *Node) (*Node, error) {
var err error
shouldAppend := false
if another.style == yaml.TaggedStyle {
if another.tag != "!append" {
return another, nil
}
another.style = n.style
another.tag = ""
shouldAppend = true
}
if n.kind != another.kind {
return nil, fmt.Errorf("cannot merge %d with %d", n.kind, another.kind)
}
switch n.kind {
case yaml.AliasNode:
return nil, fmt.Errorf("alias node cannot be merged")
case yaml.DocumentNode:
return nil, fmt.Errorf("document node cannot be merged")
case yaml.MappingNode:
for key, value := range another.mappingNodes {
if destNode, ok := n.mappingNodes[key]; ok {
n.mappingNodes[key], err = mergeToNode(destNode, value)
if err != nil {
return nil, err
}
} else {
n.mappingNodes[key] = value
}
}
case yaml.SequenceNode:
if shouldAppend {
n.sequenceNodes = append(n.sequenceNodes, another.sequenceNodes...)
return n, nil
}
return another, nil
case yaml.ScalarNode:
return another, nil
}
return n, nil
}