@@ -9,14 +9,22 @@ import (
9
9
"github.com/evilmonkeyinc/jsonpath/script"
10
10
)
11
11
12
- func newFilterToken (expression string , engine script.Engine , options * option.QueryOptions ) * filterToken {
13
- return & filterToken {expression : expression , engine : engine , options : options }
12
+ func newFilterToken (expression string , engine script.Engine , options * option.QueryOptions ) (* filterToken , error ) {
13
+ compiledExpression , err := engine .Compile (expression , options )
14
+ if err != nil {
15
+ return nil , err
16
+ }
17
+ return & filterToken {
18
+ expression : expression ,
19
+ compiledExpression : compiledExpression ,
20
+ options : options ,
21
+ }, nil
14
22
}
15
23
16
24
type filterToken struct {
17
- expression string
18
- engine script.Engine
19
- options * option.QueryOptions
25
+ expression string
26
+ compiledExpression script.CompiledExpression
27
+ options * option.QueryOptions
20
28
}
21
29
22
30
func (token * filterToken ) String () string {
@@ -69,11 +77,6 @@ func (token *filterToken) Apply(root, current interface{}, next []Token) (interf
69
77
return nil , getInvalidTokenTargetNilError (token .Type (), reflect .Array , reflect .Map , reflect .Slice )
70
78
}
71
79
72
- compiledExpression , err := token .engine .Compile (token .expression , token .options )
73
- if err != nil {
74
- return nil , getInvalidExpressionError (err )
75
- }
76
-
77
80
switch objType .Kind () {
78
81
case reflect .Map :
79
82
keys := objVal .MapKeys ()
@@ -82,7 +85,7 @@ func (token *filterToken) Apply(root, current interface{}, next []Token) (interf
82
85
for _ , kv := range keys {
83
86
element := objVal .MapIndex (kv ).Interface ()
84
87
85
- evaluation , err := compiledExpression .Evaluate (root , element )
88
+ evaluation , err := token . compiledExpression .Evaluate (root , element )
86
89
if err != nil {
87
90
// we ignore errors, it has failed evaluation
88
91
evaluation = nil
@@ -98,7 +101,7 @@ func (token *filterToken) Apply(root, current interface{}, next []Token) (interf
98
101
for i := 0 ; i < length ; i ++ {
99
102
element := objVal .Index (i ).Interface ()
100
103
101
- evaluation , err := compiledExpression .Evaluate (root , element )
104
+ evaluation , err := token . compiledExpression .Evaluate (root , element )
102
105
if err != nil {
103
106
// we ignore errors, it has failed evaluation
104
107
evaluation = nil
0 commit comments