Skip to content

Commit

Permalink
fix(filter): correct filter logic for maps and scalars
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Davis <[email protected]>
  • Loading branch information
Jeff Davis committed Jun 15, 2021
1 parent ed4ed3e commit 150a7f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
7 changes: 6 additions & 1 deletion pkg/yamlpath/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
lexemePropertyName
lexemeBracketPropertyName
lexemeArraySubscriptPropertyName
lexemeRecursiveFilterBegin
lexemeEOF // lexing complete
)

Expand Down Expand Up @@ -596,7 +597,11 @@ func lexSubPath(l *lexer) stateFn {
return lexOptionalArrayIndex

case l.consumed(filterBegin):
l.emit(lexemeFilterBegin)
if l.lastEmittedLexemeType == lexemeRecursiveDescent {
l.emit(lexemeRecursiveFilterBegin)
} else {
l.emit(lexemeFilterBegin)
}
l.push(lexFilterEnd)
return lexFilterExprInitial

Expand Down
2 changes: 1 addition & 1 deletion pkg/yamlpath/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func TestLexer(t *testing.T) {
expected: []lexeme{
{typ: lexemeRoot, val: "$"},
{typ: lexemeRecursiveDescent, val: ".."},
{typ: lexemeFilterBegin, val: "[?("},
{typ: lexemeRecursiveFilterBegin, val: "[?("},
{typ: lexemeFilterAt, val: "@"},
{typ: lexemeDotChild, val: ".child"},
{typ: lexemeFilterEnd, val: ")]"},
Expand Down
34 changes: 30 additions & 4 deletions pkg/yamlpath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func newPath(l *lexer) (*Path, error) {
subscript := strings.TrimSuffix(strings.TrimPrefix(lx.val, "["), "]")
return arraySubscriptThen(subscript, subPath), nil

case lexemeFilterBegin:
case lexemeFilterBegin, lexemeRecursiveFilterBegin:
var recursive bool

if lx.typ == lexemeRecursiveFilterBegin {
recursive = true
}
filterLexemes := []lexeme{}
filterNestingLevel := 1
f:
Expand Down Expand Up @@ -144,6 +149,9 @@ func newPath(l *lexer) (*Path, error) {
if err != nil {
return nil, err
}
if recursive {
return recursiveFilterThen(filterLexemes, subPath), nil
}
return filterThen(filterLexemes, subPath), nil
case lexemePropertyName:
subPath, err := newPath(l)
Expand Down Expand Up @@ -427,10 +435,28 @@ func filterThen(filterLexemes []lexeme, p *Path) *Path {
filter := newFilter(newFilterNode(filterLexemes))
return new(func(node, root *yaml.Node) yit.Iterator {
its := []yit.Iterator{}
for _, c := range node.Content {
if filter(c, root) {
its = append(its, compose(yit.FromNode(c), p, root))
if node.Kind == yaml.SequenceNode {
for _, c := range node.Content {
if filter(c, root) {
its = append(its, compose(yit.FromNode(c), p, root))
}
}
} else {
if filter(node, root) {
its = append(its, compose(yit.FromNode(node), p, root))
}
}
return yit.FromIterators(its...)
})
}

func recursiveFilterThen(filterLexemes []lexeme, p *Path) *Path {
filter := newFilter(newFilterNode(filterLexemes))
return new(func(node, root *yaml.Node) yit.Iterator {
its := []yit.Iterator{}

if filter(node, root) {
its = append(its, compose(yit.FromNode(node), p, root))
}
return yit.FromIterators(its...)
})
Expand Down
9 changes: 9 additions & 0 deletions pkg/yamlpath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,15 @@ price: 12.99
},
expectedPathErr: "",
},
{
name: "map filter",
path: `$.store.bicycle[?(@.color == "red")]`,
expectedStrings: []string{
`color: red
price: 19.95
`,
},
},
}

focussed := false
Expand Down

0 comments on commit 150a7f4

Please sign in to comment.