Skip to content

Commit

Permalink
fix compiling iterator suffix with optional operator
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Feb 26, 2022
1 parent a56e0a6 commit 8c50c33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,14 @@
- name: iterator with optional operator
args:
- '.[]?'
input: '10'
input: '0'

- name: iterator with optional operator after index error
args:
- '.x[]?'
input: '0'
error: |
expected an object but got: number (0)
- name: nested iterator
args:
Expand Down
4 changes: 2 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1331,14 +1331,14 @@ func (c *compiler) compileTermSuffix(e *Term, s *Suffix) error {
c.append(&code{op: opeach})
return nil
} else if s.Optional {
if len(e.SuffixList) > 1 || len(e.SuffixList) == 1 && !e.SuffixList[0].Iter {
if len(e.SuffixList) > 0 {
if u, ok := e.SuffixList[len(e.SuffixList)-1].toTerm(); ok {
t := *e // clone without changing e
(&t).SuffixList = t.SuffixList[:len(e.SuffixList)-1]
if err := c.compileTerm(&t); err != nil {
return err
}
return c.compileTermSuffix(u, s)
e = u
}
}
return c.compileTry(&Try{Body: &Query{Term: e}})
Expand Down

0 comments on commit 8c50c33

Please sign in to comment.