Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Dec 9, 2023
1 parent 4f98673 commit 5b49892
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
6 changes: 4 additions & 2 deletions jp/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,8 @@ func (x Expr) Get(data any) (results []any) {
}
case *Filter:
before := len(stack)
stack, _ = tf.EvalWithRoot(stack, prev, data).([]any)
ns, _ := tf.evalWithRoot(stack, prev, data)
stack, _ = ns.([]any)
if int(fi) == len(x)-1 { // last one
for i := len(stack) - 1; before <= i; i-- {
results = append(results, stack[i])
Expand Down Expand Up @@ -1616,7 +1617,8 @@ func (x Expr) FirstFound(data any) (any, bool) {
}
case *Filter:
before := len(stack)
stack, _ = tf.EvalWithRoot(stack, prev, data).([]any)
ns, _ := tf.evalWithRoot(stack, prev, data)
stack, _ = ns.([]any)
if int(fi) == len(x)-1 { // last one
if before < len(stack) {
result := stack[len(stack)-1]
Expand Down
3 changes: 2 additions & 1 deletion jp/has.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ func (x Expr) Has(data any) bool {
}
case *Filter:
before := len(stack)
stack, _ = tf.EvalWithRoot(stack, prev, data).([]any)
ns, _ := tf.evalWithRoot(stack, prev, data)
stack, _ = ns.([]any)
if int(fi) == len(x)-1 { // last one
if before < len(stack) {
stack = stack[:before]
Expand Down
3 changes: 2 additions & 1 deletion jp/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ done:
}
}
} else {
stack, _ = tf.EvalWithRoot(stack, prev, data).([]any)
ns, _ := tf.evalWithRoot(stack, prev, data)
stack, _ = ns.([]any)
}
case Descent:
di, _ := stack[len(stack)-1].(fragIndex)
Expand Down
6 changes: 4 additions & 2 deletions jp/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ func (x Expr) GetNodes(n gen.Node) (results []gen.Node) {
}
case *Filter:
before := len(stack)
stack, _ = tf.EvalWithRoot(stack, prev, n).([]gen.Node)
ns, _ := tf.evalWithRoot(stack, prev, n)
stack, _ = ns.([]gen.Node)
if int(fi) == len(x)-1 { // last one
for i := before; i < len(stack); i++ {
results = append(results, stack[i])
Expand Down Expand Up @@ -589,7 +590,8 @@ func (x Expr) FirstNode(n gen.Node) (result gen.Node) {
}
case *Filter:
before := len(stack)
stack, _ = tf.EvalWithRoot(stack, prev, n).([]gen.Node)
ns, _ := tf.evalWithRoot(stack, prev, n)
stack, _ = ns.([]gen.Node)
if int(fi) == len(x)-1 { // last one
if before < len(stack) {
result := stack[before]
Expand Down
17 changes: 10 additions & 7 deletions jp/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,23 @@ func (s *Script) String() string {
func (s *Script) Match(data any) bool {
stack := []any{}
if node, ok := data.(gen.Node); ok {
stack, _ = s.EvalWithRoot(stack, gen.Array{node}, data).([]any)
ns, _ := s.evalWithRoot(stack, gen.Array{node}, data)
stack, _ = ns.([]any)
} else {
stack, _ = s.EvalWithRoot(stack, []any{data}, data).([]any)
ns, _ := s.evalWithRoot(stack, []any{data}, data)
stack, _ = ns.([]any)
}
return 0 < len(stack)
}

// Eval is primarily used by the Expr parser but is public for testing.
func (s *Script) Eval(stack any, data any) any {
return s.EvalWithRoot(stack, data, nil)
ns, _ := s.evalWithRoot(stack, data, nil)
return ns
}

// EvalWithRoot is primarily used by the Expr parser but is public for testing.
func (s *Script) EvalWithRoot(stack any, data, root any) any {
// evalWithRoot is primarily used by the Expr parser but is public for testing.
func (s *Script) evalWithRoot(stack any, data, root any) (any, Expr) {
// Checking the type each iteration adds 2.5% but allows code not to be
// duplicated and not to call a separate function. Using just one more
// function call for each iteration adds 6.5%.
Expand Down Expand Up @@ -210,7 +213,7 @@ func (s *Script) EvalWithRoot(stack any, data, root any) any {
default:
rv := reflect.ValueOf(td)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return stack
return stack, nil
}
dlen = rv.Len()
da := make([]any, 0, dlen)
Expand Down Expand Up @@ -646,7 +649,7 @@ func (s *Script) EvalWithRoot(stack any, data, root any) any {
for i := range sstack {
sstack[i] = nil
}
return stack
return stack, nil
}

// Inspect the script.
Expand Down
3 changes: 2 additions & 1 deletion jp/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,8 @@ func (x Expr) set(data, value any, fun string, one bool) error {
}
}
case *Filter:
stack, _ = tf.EvalWithRoot(stack, prev, data).([]any)
ns, _ := tf.evalWithRoot(stack, prev, data)
stack, _ = ns.([]any)
case Root:
stack = append(stack, data)
case At, Bracket:
Expand Down

0 comments on commit 5b49892

Please sign in to comment.