mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 20:15:57 +00:00
ops work in theory!
This commit is contained in:
parent
f479a7e8e3
commit
f95226e267
@ -34,12 +34,16 @@ func (d *dataTreeNavigator) traverse(matchingNodes []*CandidateNode, pathNode *P
|
|||||||
return newMatchingNodes, nil
|
return newMatchingNodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dataTreeNavigator) setFunction(op OperationType, lhs []*CandidateNode, rhs []*CandidateNode) ([]*CandidateNode, error) {
|
||||||
|
return append(lhs, rhs...), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error) {
|
func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error) {
|
||||||
log.Debugf("Processing Path: %v", pathNode.PathElement.toString())
|
log.Debugf("Processing Path: %v", pathNode.PathElement.toString())
|
||||||
if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex {
|
if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex {
|
||||||
return d.traverse(matchingNodes, pathNode.PathElement)
|
return d.traverse(matchingNodes, pathNode.PathElement)
|
||||||
} else {
|
} else {
|
||||||
var lhs []*CandidateNode //, rhs
|
var lhs, rhs []*CandidateNode
|
||||||
var err error
|
var err error
|
||||||
switch pathNode.PathElement.OperationType {
|
switch pathNode.PathElement.OperationType {
|
||||||
case Traverse:
|
case Traverse:
|
||||||
@ -48,16 +52,16 @@ func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pat
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return d.GetMatchingNodes(lhs, pathNode.Rhs)
|
return d.GetMatchingNodes(lhs, pathNode.Rhs)
|
||||||
// case Or, And:
|
case Or, And:
|
||||||
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
|
lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
// rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
|
rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
// return d.setFunction(pathNode.PathElement, lhs, rhs), nil
|
return d.setFunction(pathNode.PathElement.OperationType, lhs, rhs)
|
||||||
// case Equals:
|
// case Equals:
|
||||||
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
|
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
@ -102,7 +102,8 @@ func TestDataTreeNavigatorSimpleMismatch(t *testing.T) {
|
|||||||
func TestDataTreeNavigatorWild(t *testing.T) {
|
func TestDataTreeNavigatorWild(t *testing.T) {
|
||||||
|
|
||||||
nodes := readDoc(t, `a:
|
nodes := readDoc(t, `a:
|
||||||
cat: apple`)
|
cat: apple
|
||||||
|
mad: things`)
|
||||||
|
|
||||||
path, errPath := treeCreator.ParsePath("a.*a*")
|
path, errPath := treeCreator.ParsePath("a.*a*")
|
||||||
if errPath != nil {
|
if errPath != nil {
|
||||||
@ -119,7 +120,106 @@ func TestDataTreeNavigatorWild(t *testing.T) {
|
|||||||
Document 0, path: [a cat]
|
Document 0, path: [a cat]
|
||||||
Tag: !!str, Kind: ScalarNode, Anchor:
|
Tag: !!str, Kind: ScalarNode, Anchor:
|
||||||
apple
|
apple
|
||||||
|
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a mad]
|
||||||
|
Tag: !!str, Kind: ScalarNode, Anchor:
|
||||||
|
things
|
||||||
`
|
`
|
||||||
|
|
||||||
test.AssertResult(t, expected, resultsToString(results))
|
test.AssertResult(t, expected, resultsToString(results))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDataTreeNavigatorWildDeepish(t *testing.T) {
|
||||||
|
|
||||||
|
nodes := readDoc(t, `a:
|
||||||
|
cat: {b: 3}
|
||||||
|
mad: {b: 4}
|
||||||
|
fad: {c: t}`)
|
||||||
|
|
||||||
|
path, errPath := treeCreator.ParsePath("a.*a*.b")
|
||||||
|
if errPath != nil {
|
||||||
|
t.Error(errPath)
|
||||||
|
}
|
||||||
|
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
|
||||||
|
|
||||||
|
if errNav != nil {
|
||||||
|
t.Error(errNav)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a cat b]
|
||||||
|
Tag: !!int, Kind: ScalarNode, Anchor:
|
||||||
|
3
|
||||||
|
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a mad b]
|
||||||
|
Tag: !!int, Kind: ScalarNode, Anchor:
|
||||||
|
4
|
||||||
|
`
|
||||||
|
|
||||||
|
test.AssertResult(t, expected, resultsToString(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDataTreeNavigatorOrSimple(t *testing.T) {
|
||||||
|
|
||||||
|
nodes := readDoc(t, `a:
|
||||||
|
cat: apple
|
||||||
|
mad: things`)
|
||||||
|
|
||||||
|
path, errPath := treeCreator.ParsePath("a.(cat or mad)")
|
||||||
|
if errPath != nil {
|
||||||
|
t.Error(errPath)
|
||||||
|
}
|
||||||
|
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
|
||||||
|
|
||||||
|
if errNav != nil {
|
||||||
|
t.Error(errNav)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a cat]
|
||||||
|
Tag: !!str, Kind: ScalarNode, Anchor:
|
||||||
|
apple
|
||||||
|
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a mad]
|
||||||
|
Tag: !!str, Kind: ScalarNode, Anchor:
|
||||||
|
things
|
||||||
|
`
|
||||||
|
|
||||||
|
test.AssertResult(t, expected, resultsToString(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDataTreeNavigatorOrSimpleWithDepth(t *testing.T) {
|
||||||
|
|
||||||
|
nodes := readDoc(t, `a:
|
||||||
|
cat: {b: 3}
|
||||||
|
mad: {b: 4}
|
||||||
|
fad: {c: t}`)
|
||||||
|
|
||||||
|
path, errPath := treeCreator.ParsePath("a.(cat.* or fad.*)")
|
||||||
|
if errPath != nil {
|
||||||
|
t.Error(errPath)
|
||||||
|
}
|
||||||
|
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
|
||||||
|
|
||||||
|
if errNav != nil {
|
||||||
|
t.Error(errNav)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a cat b]
|
||||||
|
Tag: !!int, Kind: ScalarNode, Anchor:
|
||||||
|
3
|
||||||
|
|
||||||
|
-- Node --
|
||||||
|
Document 0, path: [a fad c]
|
||||||
|
Tag: !!str, Kind: ScalarNode, Anchor:
|
||||||
|
t
|
||||||
|
`
|
||||||
|
test.AssertResult(t, expected, resultsToString(results))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user