14
14
15
15
package dev .cel .runtime ;
16
16
17
- import com .google .auto .value .AutoValue ;
18
- import com .google .common .base .Preconditions ;
19
17
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
20
18
import com .google .errorprone .annotations .Immutable ;
21
19
import javax .annotation .concurrent .ThreadSafe ;
22
20
import com .google .protobuf .Message ;
23
21
import dev .cel .common .CelAbstractSyntaxTree ;
24
22
import dev .cel .common .CelOptions ;
25
23
import java .util .Map ;
26
- import java .util .Optional ;
27
24
28
25
/**
29
26
* The CelRuntime creates executable {@code Program} instances from {@code CelAbstractSyntaxTree}
@@ -39,112 +36,69 @@ public interface CelRuntime {
39
36
CelRuntimeBuilder toRuntimeBuilder ();
40
37
41
38
/** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
42
- @ AutoValue
43
39
@ Immutable
44
- abstract class Program implements CelLiteRuntime .Program {
45
-
46
- @ Override
47
- public Object eval () throws CelEvaluationException {
48
- return evalInternal (Activation .EMPTY );
49
- }
50
-
51
- @ Override
52
- public Object eval (Map <String , ?> mapValue ) throws CelEvaluationException {
53
- return evalInternal (Activation .copyOf (mapValue ));
54
- }
40
+ interface Program extends CelLiteRuntime .Program {
55
41
56
42
/** Evaluate the expression using {@code message} fields as the source of input variables. */
57
- public Object eval (Message message ) throws CelEvaluationException {
58
- return evalInternal (ProtoMessageActivationFactory .fromProto (message , getOptions ()));
59
- }
43
+ Object eval (Message message ) throws CelEvaluationException ;
60
44
61
45
/** Evaluate a compiled program with a custom variable {@code resolver}. */
62
- public Object eval (CelVariableResolver resolver ) throws CelEvaluationException {
63
- return evalInternal ((name ) -> resolver .find (name ).orElse (null ));
64
- }
46
+ Object eval (CelVariableResolver resolver ) throws CelEvaluationException ;
65
47
66
48
/**
67
49
* Evaluate a compiled program with a custom variable {@code resolver} and late-bound functions
68
50
* {@code lateBoundFunctionResolver}.
69
51
*/
70
- public Object eval (CelVariableResolver resolver , CelFunctionResolver lateBoundFunctionResolver )
71
- throws CelEvaluationException {
72
- return evalInternal (
73
- (name ) -> resolver .find (name ).orElse (null ),
74
- lateBoundFunctionResolver ,
75
- CelEvaluationListener .noOpListener ());
76
- }
77
-
78
- @ Override
79
- public Object eval (Map <String , ?> mapValue , CelFunctionResolver lateBoundFunctionResolver )
80
- throws CelEvaluationException {
81
- return evalInternal (
82
- Activation .copyOf (mapValue ),
83
- lateBoundFunctionResolver ,
84
- CelEvaluationListener .noOpListener ());
85
- }
52
+ Object eval (CelVariableResolver resolver , CelFunctionResolver lateBoundFunctionResolver )
53
+ throws CelEvaluationException ;
86
54
87
55
/**
88
56
* Trace evaluates a compiled program without any variables and invokes the listener as
89
57
* evaluation progresses through the AST.
90
58
*/
91
- public Object trace (CelEvaluationListener listener ) throws CelEvaluationException {
92
- return evalInternal (Activation .EMPTY , listener );
93
- }
59
+ Object trace (CelEvaluationListener listener ) throws CelEvaluationException ;
94
60
95
61
/**
96
62
* Trace evaluates a compiled program using a {@code mapValue} as the source of input variables.
97
63
* The listener is invoked as evaluation progresses through the AST.
98
64
*/
99
- public Object trace (Map <String , ?> mapValue , CelEvaluationListener listener )
100
- throws CelEvaluationException {
101
- return evalInternal (Activation .copyOf (mapValue ), listener );
102
- }
65
+ Object trace (Map <String , ?> mapValue , CelEvaluationListener listener )
66
+ throws CelEvaluationException ;
103
67
104
68
/**
105
69
* Trace evaluates a compiled program using {@code message} fields as the source of input
106
70
* variables. The listener is invoked as evaluation progresses through the AST.
107
71
*/
108
- public Object trace (Message message , CelEvaluationListener listener )
109
- throws CelEvaluationException {
110
- return evalInternal (ProtoMessageActivationFactory .fromProto (message , getOptions ()), listener );
111
- }
72
+ Object trace (Message message , CelEvaluationListener listener ) throws CelEvaluationException ;
112
73
113
74
/**
114
75
* Trace evaluates a compiled program using a custom variable {@code resolver}. The listener is
115
76
* invoked as evaluation progresses through the AST.
116
77
*/
117
- public Object trace (CelVariableResolver resolver , CelEvaluationListener listener )
118
- throws CelEvaluationException {
119
- return evalInternal ((name ) -> resolver .find (name ).orElse (null ), listener );
120
- }
78
+ Object trace (CelVariableResolver resolver , CelEvaluationListener listener )
79
+ throws CelEvaluationException ;
121
80
122
81
/**
123
82
* Trace evaluates a compiled program using a custom variable {@code resolver} and late-bound
124
83
* functions {@code lateBoundFunctionResolver}. The listener is invoked as evaluation progresses
125
84
* through the AST.
126
85
*/
127
- public Object trace (
86
+ Object trace (
128
87
CelVariableResolver resolver ,
129
88
CelFunctionResolver lateBoundFunctionResolver ,
130
89
CelEvaluationListener listener )
131
- throws CelEvaluationException {
132
- return evalInternal (
133
- (name ) -> resolver .find (name ).orElse (null ), lateBoundFunctionResolver , listener );
134
- }
90
+ throws CelEvaluationException ;
135
91
136
92
/**
137
93
* Trace evaluates a compiled program using a {@code mapValue} as the source of input variables
138
94
* and late-bound functions {@code lateBoundFunctionResolver}. The listener is invoked as
139
95
* evaluation progresses through the AST.
140
96
*/
141
- public Object trace (
97
+ Object trace (
142
98
Map <String , ?> mapValue ,
143
99
CelFunctionResolver lateBoundFunctionResolver ,
144
100
CelEvaluationListener listener )
145
- throws CelEvaluationException {
146
- return evalInternal (Activation .copyOf (mapValue ), lateBoundFunctionResolver , listener );
147
- }
101
+ throws CelEvaluationException ;
148
102
149
103
/**
150
104
* Advance evaluation based on the current unknown context.
@@ -155,70 +109,6 @@ public Object trace(
155
109
* <p>If no unknowns are declared in the context or {@link CelOptions#enableUnknownTracking()
156
110
* UnknownTracking} is disabled, this is equivalent to eval.
157
111
*/
158
- public Object advanceEvaluation (UnknownContext context ) throws CelEvaluationException {
159
- return evalInternal (context , Optional .empty (), CelEvaluationListener .noOpListener ());
160
- }
161
-
162
- private Object evalInternal (GlobalResolver resolver ) throws CelEvaluationException {
163
- return evalInternal (
164
- UnknownContext .create (resolver ), Optional .empty (), CelEvaluationListener .noOpListener ());
165
- }
166
-
167
- private Object evalInternal (GlobalResolver resolver , CelEvaluationListener listener )
168
- throws CelEvaluationException {
169
- return evalInternal (UnknownContext .create (resolver ), Optional .empty (), listener );
170
- }
171
-
172
- private Object evalInternal (
173
- GlobalResolver resolver ,
174
- CelFunctionResolver lateBoundFunctionResolver ,
175
- CelEvaluationListener listener )
176
- throws CelEvaluationException {
177
- return evalInternal (
178
- UnknownContext .create (resolver ), Optional .of (lateBoundFunctionResolver ), listener );
179
- }
180
-
181
- /**
182
- * Evaluate an expr node with an UnknownContext (an activation annotated with which attributes
183
- * are unknown).
184
- */
185
- private Object evalInternal (
186
- UnknownContext context ,
187
- Optional <CelFunctionResolver > lateBoundFunctionResolver ,
188
- CelEvaluationListener listener )
189
- throws CelEvaluationException {
190
- Interpretable impl = getInterpretable ();
191
- if (getOptions ().enableUnknownTracking ()) {
192
- Preconditions .checkState (
193
- impl instanceof UnknownTrackingInterpretable ,
194
- "Environment misconfigured. Requested unknown tracking without a compatible"
195
- + " implementation." );
196
-
197
- UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable ) impl ;
198
- return interpreter .evalTrackingUnknowns (
199
- RuntimeUnknownResolver .builder ()
200
- .setResolver (context .variableResolver ())
201
- .setAttributeResolver (context .createAttributeResolver ())
202
- .build (),
203
- lateBoundFunctionResolver ,
204
- listener );
205
- } else {
206
- if (lateBoundFunctionResolver .isPresent ()) {
207
- return impl .eval (context .variableResolver (), lateBoundFunctionResolver .get (), listener );
208
- }
209
- return impl .eval (context .variableResolver (), listener );
210
- }
211
- }
212
-
213
- /** Get the underlying {@link Interpretable} for the {@code Program}. */
214
- abstract Interpretable getInterpretable ();
215
-
216
- /** Get the {@code CelOptions} configured for this program. */
217
- abstract CelOptions getOptions ();
218
-
219
- /** Instantiate a new {@code Program} from the input {@code interpretable}. */
220
- static Program from (Interpretable interpretable , CelOptions options ) {
221
- return new AutoValue_CelRuntime_Program (interpretable , options );
222
- }
112
+ Object advanceEvaluation (UnknownContext context ) throws CelEvaluationException ;
223
113
}
224
114
}
0 commit comments