diff --git a/pkg/yqlib/treeops/data_tree_navigator.go b/pkg/yqlib/treeops/data_tree_navigator.go index be30b8c5..08f0edeb 100644 --- a/pkg/yqlib/treeops/data_tree_navigator.go +++ b/pkg/yqlib/treeops/data_tree_navigator.go @@ -72,6 +72,8 @@ func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMa return matchingNodes, nil } else if pathNode.PathElement.PathElementType == PathKey { return d.traverse(matchingNodes, pathNode.PathElement) + } else if pathNode.PathElement.PathElementType == Value { + return nodeToMap(BuildCandidateNodeFrom(pathNode.PathElement)), nil } else { handler := pathNode.PathElement.OperationType.Handler if handler != nil { diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 1d8f52e2..e0d2c00d 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -625,12 +625,12 @@ func TestDataTreeNavigatorArraySimple(t *testing.T) { test.AssertResult(t, expected, resultsToString(results)) } -func TestDataTreeNavigatorSimpleAssign(t *testing.T) { +func TestDataTreeNavigatorSimpleAssignCmd(t *testing.T) { nodes := readDoc(t, `a: b: apple`) - path, errPath := treeCreator.ParsePath("a.b := frog") + path, errPath := treeCreator.ParsePath(`.a.b |= "frog"`) if errPath != nil { t.Error(errPath) } @@ -650,6 +650,80 @@ func TestDataTreeNavigatorSimpleAssign(t *testing.T) { test.AssertResult(t, expected, resultsToString(results)) } +func TestDataTreeNavigatorSimpleAssignNumberCmd(t *testing.T) { + + nodes := readDoc(t, `a: + b: apple`) + + path, errPath := treeCreator.ParsePath(`.a.b |= 5`) + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a b] + Tag: !!int, Kind: ScalarNode, Anchor: + 5 +` + + test.AssertResult(t, expected, resultsToString(results)) +} + +func TestDataTreeNavigatorSimpleAssignFloatCmd(t *testing.T) { + + nodes := readDoc(t, `a: + b: apple`) + + path, errPath := treeCreator.ParsePath(`.a.b |= 3.142`) + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a b] + Tag: !!float, Kind: ScalarNode, Anchor: + 3.142 +` + + test.AssertResult(t, expected, resultsToString(results)) +} + +func TestDataTreeNavigatorSimpleAssignBooleanCmd(t *testing.T) { + + nodes := readDoc(t, `a: + b: apple`) + path, errPath := treeCreator.ParsePath(`.a.b |= true`) + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a b] + Tag: !!bool, Kind: ScalarNode, Anchor: + true +` + + test.AssertResult(t, expected, resultsToString(results)) +} + func TestDataTreeNavigatorSimpleAssignSelf(t *testing.T) { nodes := readDoc(t, `a: diff --git a/pkg/yqlib/treeops/value_node_builder.go b/pkg/yqlib/treeops/value_node_builder.go new file mode 100644 index 00000000..2a7203f7 --- /dev/null +++ b/pkg/yqlib/treeops/value_node_builder.go @@ -0,0 +1,20 @@ +package treeops + +import "gopkg.in/yaml.v3" + +func BuildCandidateNodeFrom(p *PathElement) *CandidateNode { + var node yaml.Node = yaml.Node{Kind: yaml.ScalarNode} + node.Value = p.StringValue + + switch p.Value.(type) { + case float32, float64: + node.Tag = "!!float" + case int, int64, int32: + node.Tag = "!!int" + case bool: + node.Tag = "!!bool" + case string: + node.Tag = "!!str" + } + return &CandidateNode{Node: &node} +}