This commit is contained in:
Mike Farah 2020-10-19 16:36:46 +11:00
parent 2ddf8dd4ed
commit 1910563bfe
4 changed files with 60 additions and 10 deletions

View File

@ -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

View File

@ -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}}

View File

@ -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),

View File

@ -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"
`,
},
},
}