From e6fd6905eb2bb80a75094ae75dea64fdb46ae41b Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 10 Oct 2020 23:04:10 +1100 Subject: [PATCH] wip --- pkg/yqlib/treeops/data_tree_navigator_test.go | 59 +++++++++++++++++++ pkg/yqlib/treeops/operators.go | 1 + 2 files changed, 60 insertions(+) diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index c48b5b54..2b183da9 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -133,6 +133,65 @@ func TestDataTreeNavigatorSubtractWithUnion(t *testing.T) { test.AssertResult(t, expected, resultsToString(results)) } +func TestDataTreeNavigatorSubtractByIndex(t *testing.T) { + + nodes := readDoc(t, `a: + - b: apple + - b: sdfsd + - b: apple`) + + path, errPath := treeCreator.ParsePath("(a .- (0 or 1))") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a] + Tag: !!seq, Kind: SequenceNode, Anchor: + - b: apple +` + + test.AssertResult(t, expected, resultsToString(results)) +} + +func TestDataTreeNavigatorDeleteAndWrite(t *testing.T) { + + nodes := readDoc(t, `a: + - b: apple + - b: sdfsd + - { b: apple, c: cat }`) + + path, errPath := treeCreator.ParsePath("(a .- (0 or 1)) or (a[0].b := frog)") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a] + Tag: !!seq, Kind: SequenceNode, Anchor: + - {b: frog, c: cat} + +-- Node -- + Document 0, path: [a 0 b] + Tag: !!str, Kind: ScalarNode, Anchor: + frog +` + + test.AssertResult(t, expected, resultsToString(results)) +} + func TestDataTreeNavigatorSubtractArray(t *testing.T) { nodes := readDoc(t, `a: diff --git a/pkg/yqlib/treeops/operators.go b/pkg/yqlib/treeops/operators.go index aae4d89d..26ea678e 100644 --- a/pkg/yqlib/treeops/operators.go +++ b/pkg/yqlib/treeops/operators.go @@ -19,6 +19,7 @@ func AssignOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, } for el := lhs.Front(); el != nil; el = el.Next() { node := el.Value.(*CandidateNode) + log.Debugf("Assiging %v to %v", node.getKey(), pathNode.Rhs.PathElement.StringValue) node.Node.Value = pathNode.Rhs.PathElement.StringValue } return lhs, nil