Skip to content

Commit c6c4399

Browse files
committed
fix delayed list merge + marshal non-printable
1 parent fa7d37e commit c6c4399

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

flow/flow.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,13 @@ func flowMap(root yaml.Node, env dynaml.Binding, shouldOverride, template bool)
481481
func flowList(root yaml.Node, env dynaml.Binding, template bool) yaml.Node {
482482
rootList := root.Value().([]yaml.Node)
483483

484+
if root.Merged() {
485+
return root
486+
}
487+
484488
debug.Debug("HANDLE LIST %v\n", env.Path())
485-
merged, process, replaced, redirectPath, keyName, ismerged, flags, tag, stub, nomerge := processMerges(root, rootList, env, template)
489+
merged, process, replaced, redirectPath, keyName, ismerged, flags, tag, stub := processMerges(root, rootList, env, template)
490+
nomerge := flags.IsNoMerge()
486491

487492
if process {
488493
debug.Debug("process list (key: %s) %v\n", keyName, env.Path())
@@ -622,20 +627,20 @@ func useMerge(n yaml.Node) bool {
622627
return false
623628
}
624629

625-
func processMerges(orig yaml.Node, root []yaml.Node, env dynaml.Binding, template bool) (interface{}, bool, bool, []string, string, bool, yaml.NodeFlags, string, yaml.Node, bool) {
630+
func processMerges(orig yaml.Node, root []yaml.Node, env dynaml.Binding, template bool) (interface{}, bool, bool, []string, string, bool, yaml.NodeFlags, string, yaml.Node) {
626631
var flags yaml.NodeFlags
627632
var stub yaml.Node
628633
flags, stub = get_inherited_flags(env)
629634
tag := orig.GetAnnotation().Tag()
630635
spliced := []yaml.Node{}
631636
process := true
632-
merged := false
637+
merged := orig.Merged()
633638
keyName := orig.KeyName()
634639
replaced := orig.ReplaceFlag()
635640
redirectPath := orig.RedirectPath()
636641

637642
// first, check for omitted stub merging for non-keyed values
638-
nomerge := false
643+
nomerge := orig.Flags().IsNoMerge()
639644
for _, val := range root {
640645
if val == nil {
641646
continue
@@ -751,8 +756,12 @@ func processMerges(orig yaml.Node, root []yaml.Node, env dynaml.Binding, templat
751756
result = processed
752757
}
753758

759+
if nomerge {
760+
flags |= yaml.FLAG_NOMERGE
761+
}
762+
754763
debug.Debug("--> %+v proc=%v replaced=%v redirect=%v key=%s\n", result, process, replaced, redirectPath, keyName)
755-
return result, process, replaced, redirectPath, keyName, merged, flags, tag, stub, nomerge
764+
return result, process, replaced, redirectPath, keyName, merged, flags, tag, stub
756765
}
757766

758767
const NO_LIST_KEY = "<<<NO LIST KEY>>"

flow/flow2_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ var _ = Describe("merging lists", func() {
1313
list:
1414
- field: e
1515
attr: f
16+
- field: g
17+
attr: h
1618
- <<<: (( merge ))
19+
- field: i
20+
attr: j
21+
- field: k
22+
attr: l
1723
`)
1824
stub := parseYAML(`
1925
---
@@ -28,10 +34,61 @@ list:
2834
list:
2935
- field: e
3036
attr: f
37+
- field: g
38+
attr: h
3139
- field: a
3240
attr: b
3341
- field: c
3442
attr: d
43+
- field: i
44+
attr: j
45+
- field: k
46+
attr: l
47+
`)
48+
Expect(source).To(FlowAs(resolved, stub))
49+
})
50+
51+
It("merges map lists with two inserts", func() {
52+
source := parseYAML(`
53+
---
54+
temp:
55+
- <<: (( &temporary ))
56+
- field: t1
57+
attr: vt1
58+
- field: t2
59+
attr: vt2
60+
61+
list:
62+
- field: a1
63+
attr: va1
64+
- <<<: (( merge ))
65+
- field: a2
66+
attr: va2
67+
- <<: (( temp ))
68+
`)
69+
stub := parseYAML(`
70+
---
71+
list:
72+
- field: b1
73+
attr: vb1
74+
- field: b2
75+
attr: vb2
76+
`)
77+
resolved := parseYAML(`
78+
---
79+
list:
80+
- field: a1
81+
attr: va1
82+
- field: b1
83+
attr: vb1
84+
- field: b2
85+
attr: vb2
86+
- field: a2
87+
attr: va2
88+
- field: t1
89+
attr: vt1
90+
- field: t2
91+
attr: vt2
3592
`)
3693
Expect(source).To(FlowAs(resolved, stub))
3794
})

legacy/candiedyaml/encode.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,17 @@ func (e *Encoder) emitSlice(tag string, v reflect.Value) {
250250
}
251251

252252
func (e *Encoder) emitBase64(tag string, v reflect.Value) {
253-
if v.IsNil() {
253+
if v.Kind() == reflect.Slice && v.IsNil() {
254254
e.emitNil()
255255
return
256256
}
257257

258-
s := v.Bytes()
258+
var s []byte
259+
if v.Kind() == reflect.String {
260+
s = []byte(v.String())
261+
} else {
262+
s = v.Bytes()
263+
}
259264

260265
dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
261266

@@ -267,10 +272,13 @@ func (e *Encoder) emitString(tag string, v reflect.Value) {
267272
var style yaml_scalar_style_t
268273
s := v.String()
269274

270-
if nonPrintable.MatchString(s) {
271-
e.emitBase64(tag, v)
272-
return
273-
}
275+
/*
276+
if nonPrintable.MatchString(s) {
277+
e.emitBase64(tag, v)
278+
return
279+
}
280+
281+
*/
274282

275283
if v.Type() == numberType {
276284
style = yaml_PLAIN_SCALAR_STYLE

yaml/node.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const (
7777

7878
FLAG_INJECTED = 0x040
7979
FLAG_IMPLIED = 0x080
80+
81+
FLAG_NOMERGE = 0x100
8082
)
8183

8284
type NodeFlags int
@@ -160,6 +162,14 @@ func (f *NodeFlags) SetInjected() *NodeFlags {
160162
return f
161163
}
162164

165+
func (f NodeFlags) IsNoMerge() bool {
166+
return (f & FLAG_NOMERGE) != 0
167+
}
168+
func (f *NodeFlags) SetNoMerge() *NodeFlags {
169+
*f |= FLAG_NOMERGE
170+
return f
171+
}
172+
163173
type Annotation struct {
164174
redirectPath []string
165175
replace bool
@@ -303,7 +313,7 @@ func (n Annotation) Preferred() bool {
303313
}
304314

305315
func (n Annotation) Merged() bool {
306-
return n.merged //|| n.ReplaceFlag() || len(n.RedirectPath()) > 0
316+
return n.merged // || n.ReplaceFlag() || len(n.RedirectPath()) > 0
307317
}
308318

309319
func (n Annotation) StandardOverride() bool {

0 commit comments

Comments
 (0)