can assign values

This commit is contained in:
Mike Farah 2020-10-16 12:47:31 +11:00
parent 6829d8cb78
commit fccd03036f
3 changed files with 98 additions and 2 deletions

View File

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

View File

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

View File

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