Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple merge #976

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
nerrs := len(d.terrors)
for i := 0; i < l; i += 2 {
ni := n.Content[i]
if isMerge(ni) {
// Merge keys ("<<") are discarded during processing, so don't need to be unique.
continue
}

for j := i + 2; j < l; j += 2 {
nj := n.Content[j]
if ni.Kind == nj.Kind && ni.Value == nj.Value {
Expand Down Expand Up @@ -816,7 +821,7 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
mergedFields := d.mergedFields
d.mergedFields = nil

var mergeNode *Node
mergeNodes := []*Node{}

mapIsNew := false
if out.IsNil() {
Expand All @@ -825,7 +830,7 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
}
for i := 0; i < l; i += 2 {
if isMerge(n.Content[i]) {
mergeNode = n.Content[i+1]
mergeNodes = append(mergeNodes, n.Content[i+1])
continue
}
k := reflect.New(kt).Elem()
Expand All @@ -852,8 +857,8 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
}

d.mergedFields = mergedFields
if mergeNode != nil {
d.merge(n, mergeNode, out)
for _, node := range mergeNodes {
d.merge(n, node, out)
}

d.stringMapType = stringMapType
Expand Down
63 changes: 47 additions & 16 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ var unmarshalTests = []struct {
map[string]interface{}{"seq": []interface{}{"A", "B"}},
}, {
"seq: [A,B,C,]",
map[string][]string{"seq": []string{"A", "B", "C"}},
map[string][]string{"seq": {"A", "B", "C"}},
}, {
"seq: [A,1,C]",
map[string][]string{"seq": []string{"A", "1", "C"}},
map[string][]string{"seq": {"A", "1", "C"}},
}, {
"seq: [A,1,C]",
map[string][]int{"seq": []int{1}},
map[string][]int{"seq": {1}},
}, {
"seq: [A,1,C]",
map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
Expand All @@ -226,13 +226,13 @@ var unmarshalTests = []struct {
map[string]interface{}{"seq": []interface{}{"A", "B"}},
}, {
"seq:\n - A\n - B\n - C",
map[string][]string{"seq": []string{"A", "B", "C"}},
map[string][]string{"seq": {"A", "B", "C"}},
}, {
"seq:\n - A\n - 1\n - C",
map[string][]string{"seq": []string{"A", "1", "C"}},
map[string][]string{"seq": {"A", "1", "C"}},
}, {
"seq:\n - A\n - 1\n - C",
map[string][]int{"seq": []int{1}},
map[string][]int{"seq": {1}},
}, {
"seq:\n - A\n - 1\n - C",
map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
Expand Down Expand Up @@ -453,7 +453,7 @@ var unmarshalTests = []struct {
map[interface{}]interface{}{"1": "\"2\""},
}, {
"v:\n- A\n- 'B\n\n C'\n",
map[string][]string{"v": []string{"A", "B\nC"}},
map[string][]string{"v": {"A", "B\nC"}},
},

// Explicit tags.
Expand Down Expand Up @@ -657,11 +657,11 @@ var unmarshalTests = []struct {
// Support encoding.TextUnmarshaler.
{
"a: 1.2.3.4\n",
map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}},
map[string]textUnmarshaler{"a": {S: "1.2.3.4"}},
},
{
"a: 2015-02-24T18:19:39Z\n",
map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}},
map[string]textUnmarshaler{"a": {"2015-02-24T18:19:39Z"}},
},

// Timestamps
Expand Down Expand Up @@ -947,7 +947,7 @@ var unmarshalErrorTests = []struct {
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
{"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},
{"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"},
{"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665
{"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665
{"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666
{
"a: &a [00,00,00,00,00,00,00,00,00]\n" +
Expand Down Expand Up @@ -1381,7 +1381,7 @@ longTag:
label: center/big

inlineMap:
# Inlined map
# Inlined map
<< : {"x": 1, "y": 2, "r": 10}
label: center/big

Expand Down Expand Up @@ -1482,7 +1482,7 @@ func (s *S) TestMergeNestedStruct(c *C) {
// 2) A simple implementation might attempt to handle the key skipping
// directly by iterating over the merging map without recursion, but
// there are more complex cases that require recursion.
//
//
// Quick summary of the fields:
//
// - A must come from outer and not overriden
Expand All @@ -1498,7 +1498,7 @@ func (s *S) TestMergeNestedStruct(c *C) {
A, B, C int
}
type Outer struct {
D, E int
D, E int
Inner Inner
Inline map[string]int `yaml:",inline"`
}
Expand All @@ -1516,10 +1516,10 @@ func (s *S) TestMergeNestedStruct(c *C) {
// Repeat test with a map.

var testm map[string]interface{}
var wantm = map[string]interface {} {
"f": 60,
var wantm = map[string]interface{}{
"f": 60,
"inner": map[string]interface{}{
"a": 10,
"a": 10,
},
"d": 40,
"e": 50,
Expand All @@ -1530,6 +1530,37 @@ func (s *S) TestMergeNestedStruct(c *C) {
c.Assert(testm["outer"], DeepEquals, wantm)
}

const doubleMerge = `
one: &one
a: 1
b: 2
d: 5

two: &two
b: 3
c: 4
e: 6

merged:
a: 0
<<: *one
<<: *two
e: 7
`

func (s *S) TestDoubleMerge(c *C) {
var m map[string]interface{}
err := yaml.Unmarshal([]byte(doubleMerge), &m)
c.Assert(err, IsNil)
c.Assert(m["merged"], DeepEquals, map[string]interface{}{
"a": 0, // The value of the parent is preserved over what's brought in by the merge
"b": 3,
"c": 4,
"d": 5,
"e": 7,
})
}

var unmarshalNullTests = []struct {
input string
pristine, expected func() interface{}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=