ops work in theory!

This commit is contained in:
Mike Farah 2020-10-09 11:10:37 +11:00
parent f479a7e8e3
commit f95226e267
2 changed files with 116 additions and 12 deletions

View File

@ -34,12 +34,16 @@ func (d *dataTreeNavigator) traverse(matchingNodes []*CandidateNode, pathNode *P
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) {
log.Debugf("Processing Path: %v", pathNode.PathElement.toString())
if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex {
return d.traverse(matchingNodes, pathNode.PathElement)
} else {
var lhs []*CandidateNode //, rhs
var lhs, rhs []*CandidateNode
var err error
switch pathNode.PathElement.OperationType {
case Traverse:
@ -48,16 +52,16 @@ func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pat
return nil, err
}
return d.GetMatchingNodes(lhs, pathNode.Rhs)
// case Or, And:
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
// if err != nil {
// return nil, err
// }
// rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
// if err != nil {
// return nil, err
// }
// return d.setFunction(pathNode.PathElement, lhs, rhs), nil
case Or, And:
lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
if err != nil {
return nil, err
}
return d.setFunction(pathNode.PathElement.OperationType, lhs, rhs)
// case Equals:
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
// if err != nil {

View File

@ -102,7 +102,8 @@ func TestDataTreeNavigatorSimpleMismatch(t *testing.T) {
func TestDataTreeNavigatorWild(t *testing.T) {
nodes := readDoc(t, `a:
cat: apple`)
cat: apple
mad: things`)
path, errPath := treeCreator.ParsePath("a.*a*")
if errPath != nil {
@ -119,7 +120,106 @@ func TestDataTreeNavigatorWild(t *testing.T) {
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 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))
}