@@ -20,12 +20,12 @@ public JsonLogicEvaluator(Map<String, JsonLogicExpression> expressions) {
20
20
this .expressions = Collections .unmodifiableMap (expressions );
21
21
}
22
22
23
- public Object evaluate (JsonLogicNode node , Object data ) throws JsonLogicEvaluationException {
23
+ public Object evaluate (JsonLogicNode node , Object data , String jsonPath ) throws JsonLogicEvaluationException {
24
24
switch (node .getType ()) {
25
25
case PRIMITIVE : return evaluate ((JsonLogicPrimitive ) node );
26
- case VARIABLE : return evaluate ((JsonLogicVariable ) node , data );
27
- case ARRAY : return evaluate ((JsonLogicArray ) node , data );
28
- default : return evaluate ((JsonLogicOperation ) node , data );
26
+ case VARIABLE : return evaluate ((JsonLogicVariable ) node , data , jsonPath + ".var" );
27
+ case ARRAY : return evaluate ((JsonLogicArray ) node , data , jsonPath );
28
+ default : return evaluate ((JsonLogicOperation ) node , data , jsonPath );
29
29
}
30
30
}
31
31
@@ -38,19 +38,20 @@ public Object evaluate(JsonLogicPrimitive<?> primitive) {
38
38
}
39
39
}
40
40
41
- public Object evaluate (JsonLogicVariable variable , Object data ) throws JsonLogicEvaluationException {
42
- Object defaultValue = evaluate (variable .getDefaultValue (), null );
41
+ public Object evaluate (JsonLogicVariable variable , Object data , String jsonPath )
42
+ throws JsonLogicEvaluationException {
43
+ Object defaultValue = evaluate (variable .getDefaultValue (), null , jsonPath + "[1]" );
43
44
44
45
if (data == null ) {
45
46
return defaultValue ;
46
47
}
47
48
48
- Object key = evaluate (variable .getKey (), data );
49
+ Object key = evaluate (variable .getKey (), data , jsonPath + "[0]" );
49
50
50
51
if (key == null ) {
51
52
return Optional .of (data )
52
53
.map (JsonLogicEvaluator ::transform )
53
- .orElse (evaluate (variable .getDefaultValue (), null ));
54
+ .orElse (evaluate (variable .getDefaultValue (), null , jsonPath + "[1]" ));
54
55
}
55
56
56
57
if (key instanceof Number ) {
@@ -78,21 +79,21 @@ public Object evaluate(JsonLogicVariable variable, Object data) throws JsonLogic
78
79
String [] keys = name .split ("\\ ." );
79
80
Object result = data ;
80
81
81
- for (String partial : keys ) {
82
- result = evaluatePartialVariable (partial , result );
82
+ for (String partial : keys ) {
83
+ result = evaluatePartialVariable (partial , result , jsonPath + "[0]" );
83
84
84
- if (result == null ) {
85
+ if (result == null ) {
85
86
return defaultValue ;
86
87
}
87
88
}
88
89
89
90
return result ;
90
91
}
91
92
92
- throw new JsonLogicEvaluationException ("var first argument must be null, number, or string" );
93
+ throw new JsonLogicEvaluationException ("var first argument must be null, number, or string" , jsonPath + "[0]" );
93
94
}
94
95
95
- private Object evaluatePartialVariable (String key , Object data ) throws JsonLogicEvaluationException {
96
+ private Object evaluatePartialVariable (String key , Object data , String jsonPath ) throws JsonLogicEvaluationException {
96
97
if (ArrayLike .isEligible (data )) {
97
98
ArrayLike list = new ArrayLike (data );
98
99
int index ;
@@ -101,7 +102,7 @@ private Object evaluatePartialVariable(String key, Object data) throws JsonLogic
101
102
index = Integer .parseInt (key );
102
103
}
103
104
catch (NumberFormatException e ) {
104
- throw new JsonLogicEvaluationException (e );
105
+ throw new JsonLogicEvaluationException (e , jsonPath );
105
106
}
106
107
107
108
if (index < 0 || index >= list .size ()) {
@@ -118,24 +119,25 @@ private Object evaluatePartialVariable(String key, Object data) throws JsonLogic
118
119
return null ;
119
120
}
120
121
121
- public List <Object > evaluate (JsonLogicArray array , Object data ) throws JsonLogicEvaluationException {
122
+ public List <Object > evaluate (JsonLogicArray array , Object data , String jsonPath ) throws JsonLogicEvaluationException {
122
123
List <Object > values = new ArrayList <>(array .size ());
123
124
125
+ int index = 0 ;
124
126
for (JsonLogicNode element : array ) {
125
- values .add (evaluate (element , data ));
127
+ values .add (evaluate (element , data , String . format ( "%s[%d]" , jsonPath , index ++) ));
126
128
}
127
129
128
130
return values ;
129
131
}
130
132
131
- public Object evaluate (JsonLogicOperation operation , Object data ) throws JsonLogicEvaluationException {
133
+ public Object evaluate (JsonLogicOperation operation , Object data , String jsonPath ) throws JsonLogicEvaluationException {
132
134
JsonLogicExpression handler = expressions .get (operation .getOperator ());
133
135
134
136
if (handler == null ) {
135
- throw new JsonLogicEvaluationException ("Undefined operation '" + operation .getOperator () + "'" );
137
+ throw new JsonLogicEvaluationException ("Undefined operation '" + operation .getOperator () + "'" , jsonPath );
136
138
}
137
139
138
- return handler .evaluate (this , operation .getArguments (), data );
140
+ return handler .evaluate (this , operation .getArguments (), data , String . format ( "%s.%s" , jsonPath , operation . getOperator ()) );
139
141
}
140
142
141
143
public static Object transform (Object value ) {
@@ -145,4 +147,4 @@ public static Object transform(Object value) {
145
147
146
148
return value ;
147
149
}
148
- }
150
+ }
0 commit comments