diff --git a/pkg/yqlib/treeops/candidate_node.go b/pkg/yqlib/treeops/candidate_node.go index 4d98e8ac..e714fc16 100644 --- a/pkg/yqlib/treeops/candidate_node.go +++ b/pkg/yqlib/treeops/candidate_node.go @@ -20,13 +20,18 @@ func (n *CandidateNode) GetKey() string { // updates this candidate from the given candidate node func (n *CandidateNode) UpdateFrom(other *CandidateNode) { - + n.UpdateAttributesFrom(other) n.Node.Content = other.Node.Content n.Node.Value = other.Node.Value - n.UpdateAttributesFrom(other) } func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode) { + if n.Node.Kind != other.Node.Kind { + // clear out the contents when switching to a different type + // e.g. map to array + n.Node.Content = make([]*yaml.Node, 0) + n.Node.Value = "" + } n.Node.Kind = other.Node.Kind n.Node.Tag = other.Node.Tag n.Node.Style = other.Node.Style diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 01654d70..234d6296 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -19,6 +19,7 @@ func readDoc(t *testing.T, content string) []*CandidateNode { var dataBucket yaml.Node err := decoder.Decode(&dataBucket) if err != nil { + t.Error(content) t.Error(err) } return []*CandidateNode{&CandidateNode{Node: dataBucket.Content[0], Document: 0}} diff --git a/pkg/yqlib/treeops/leaf_traverser.go b/pkg/yqlib/treeops/leaf_traverser.go index 1b60b22b..21539a6a 100644 --- a/pkg/yqlib/treeops/leaf_traverser.go +++ b/pkg/yqlib/treeops/leaf_traverser.go @@ -69,8 +69,8 @@ func (t *traverser) traverseArray(candidate *CandidateNode, pathNode *PathElemen var contents = candidate.Node.Content var newMatches = make([]*CandidateNode, len(contents)) - - for index := 0; index < len(contents); index = index + 1 { + var index int64 + for index = 0; index < int64(len(contents)); index = index + 1 { newMatches[index] = &CandidateNode{ Document: candidate.Document, Path: append(candidate.Path, index), diff --git a/pkg/yqlib/treeops/operator_multiply_test.go b/pkg/yqlib/treeops/operator_multiply_test.go index 14101621..a4c89459 100644 --- a/pkg/yqlib/treeops/operator_multiply_test.go +++ b/pkg/yqlib/treeops/operator_multiply_test.go @@ -6,17 +6,61 @@ import ( var multiplyOperatorScenarios = []expressionScenario{ { - // document: `{a: frog, b: cat}`, - // expression: `.a * .b`, - // expected: []string{ - // "D0, P[], (!!map)::{a: cat, b: cat}\n", - // }, - // }, { + document: `{a: {also: [1]}, b: {also: me}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n", + }, + }, { + document: `{a: {also: me}, b: {also: [1]}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n", + }, + }, { + document: `{a: {also: me}, b: {also: {g: wizz}}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n", + }, + }, { + document: `{a: {also: {g: wizz}}, b: {also: me}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n", + }, + }, { + document: `{a: {also: {g: wizz}}, b: {also: [1]}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n", + }, + }, { + document: `{a: {also: [1]}, b: {also: {g: wizz}}}`, + expression: `.a * .b`, + expected: []string{ + "D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n", + }, + }, { document: `{a: {things: great}, b: {also: me}}`, expression: `.a * .b`, expected: []string{ "D0, P[], (!!map)::{a: {things: great, also: me}, b: {also: me}}\n", }, + }, { + document: `a: {things: great} +b: + also: "me" +`, + expression: `.a * .b`, + expected: []string{ + `D0, P[], (!!map)::a: + things: great + also: "me" +b: + also: "me" +`, + }, }, }