-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_test.go
196 lines (173 loc) · 4.8 KB
/
merge_test.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gofigure
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"
)
var _ = Describe("Merge", func() {
It("should not merge document node", func() {
var nodeA yaml.Node
Expect(yaml.Unmarshal([]byte(`name: John"`), &nodeA)).To(BeNil())
var nodeB yaml.Node
Expect(yaml.Unmarshal([]byte(`age: 123`), &nodeB)).To(BeNil())
_, err := MergeNodes(NewNode(&nodeA), NewNode(&nodeB))
Expect(err).To(And(Not(BeNil()), MatchError("document node cannot be merged")))
})
It("should not merge unmatched node", func() {
var nodeA yaml.Node
Expect(yaml.Unmarshal([]byte(`name: John"`), &nodeA)).To(BeNil())
var nodeB yaml.Node
Expect(yaml.Unmarshal([]byte(`name: { age: 1 }`), &nodeB)).To(BeNil())
_, err := MergeNodes(NewNode(nodeA.Content[0]), NewNode(nodeB.Content[0]))
Expect(err).To(And(Not(BeNil()), MatchError("cannot merge 8 with 4")))
})
It("should merge", func() {
var nodeA yaml.Node
Expect(yaml.Unmarshal([]byte(`name: Doe
tagged: value
map:
key: 123`), &nodeA)).To(BeNil())
var nodeB yaml.Node
Expect(yaml.Unmarshal([]byte(`realname: &realname John
name: *realname
tagged: !tagged override
age: 123
map: !replace
key2: 456`), &nodeB)).To(BeNil())
result, err := MergeNodes(NewNode(nodeA.Content[0]), NewNode(nodeB.Content[0]))
Expect(err).To(BeNil())
b, err := yaml.Marshal(result.ToYAMLNode())
Expect(err).To(BeNil())
var s struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
Tagged string `yaml:"tagged"`
Map map[string]int `yaml:"map"`
}
Expect(yaml.Unmarshal(b, &s)).To(BeNil())
Expect(s.Name).To(Equal("John"))
Expect(s.Age).To(Equal(123))
Expect(s.Tagged).To(Equal("override"))
Expect(s.Map).To(And(
HaveLen(1),
HaveKeyWithValue("key2", 456),
))
})
It("should pack node in nested keys", func() {
b, err := yaml.Marshal(map[string]any{
"name": "John",
"birthdate": map[string]any{
"year": 1970,
"month": 1,
"day": 1,
},
})
Expect(err).To(BeNil())
var node yaml.Node
Expect(yaml.Unmarshal(b, &node)).To(BeNil())
result := PackNodeInNestedKeys(NewNode(node.Content[0]), "nested", "keys")
resultNode := result.ToYAMLNode()
b, err = yaml.Marshal(resultNode)
Expect(err).To(BeNil())
var s struct {
Nested struct {
Keys struct {
Name string `yaml:"name"`
Birthdate struct {
Year int `yaml:"year"`
Month int `yaml:"month"`
Day int `yaml:"day"`
}
} `yaml:"keys"`
} `yaml:"nested"`
}
Expect(yaml.Unmarshal(b, &s)).To(BeNil())
Expect(s.Nested.Keys.Name).To(Equal("John"))
Expect(s.Nested.Keys.Birthdate.Year).To(BeEquivalentTo(1970))
Expect(s.Nested.Keys.Birthdate.Month).To(BeEquivalentTo(1))
Expect(s.Nested.Keys.Birthdate.Day).To(BeEquivalentTo(1))
})
It("should merge two nodes", func() {
a, err := yaml.Marshal(map[string]any{
"str": "abc",
"array": []any{
1,
2,
},
"map": map[string]any{
"a": "abc",
"b": 123,
"d": []any{
"a",
},
},
})
Expect(err).To(BeNil())
b, err := yaml.Marshal(map[string]any{
"str": "def",
"array": []any{
3,
},
"map": map[string]any{
"b": 456,
"c": "def",
"d": []any{
"b",
},
},
})
Expect(err).To(BeNil())
var nodeA, nodeB yaml.Node
Expect(yaml.Unmarshal(a, &nodeA)).To(BeNil())
Expect(yaml.Unmarshal(b, &nodeB)).To(BeNil())
result, err := MergeNodes(NewNode(nodeA.Content[0]), NewNode(nodeB.Content[0]))
Expect(err).To(BeNil())
resultNode := result.ToYAMLNode()
resultBytes, err := yaml.Marshal(resultNode)
Expect(err).To(BeNil())
var s struct {
Str string `yaml:"str"`
Array []int `yaml:"array"`
Map struct {
A string `yaml:"a"`
B int `yaml:"b"`
C string `yaml:"c"`
D []string `yaml:"d"`
}
}
Expect(yaml.Unmarshal(resultBytes, &s)).To(BeNil())
Expect(s.Str).To(Equal("def"))
Expect(s.Array).To(Equal([]int{3}))
Expect(s.Map.A).To(Equal("abc"))
Expect(s.Map.B).To(BeEquivalentTo(456))
Expect(s.Map.C).To(Equal("def"))
Expect(s.Map.D).To(Equal([]string{"b"}))
})
It("should only merge slices with append tag", func() {
a := `first:
- 1
- 2
second:
- 'a'
- 'b'`
b := `first:
- 3
second: !append
- 'c'`
var nodeA, nodeB yaml.Node
Expect(yaml.Unmarshal([]byte(a), &nodeA)).To(BeNil())
Expect(yaml.Unmarshal([]byte(b), &nodeB)).To(BeNil())
result, err := MergeNodes(NewNode(nodeA.Content[0]), NewNode(nodeB.Content[0]))
Expect(err).To(BeNil())
resultNode := result.ToYAMLNode()
resultBytes, err := yaml.Marshal(resultNode)
Expect(err).To(BeNil())
var s struct {
First []int `yaml:"first"`
Second []string `yaml:"second"`
}
Expect(yaml.Unmarshal(resultBytes, &s)).To(BeNil())
Expect(s.First).To(Equal([]int{3}))
Expect(s.Second).To(Equal([]string{"a", "b", "c"}))
})
})