forked from cel-expr/cel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelImpl.java
More file actions
368 lines (315 loc) · 11.5 KB
/
Copy pathCelImpl.java
File metadata and controls
368 lines (315 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dev.cel.bundle;
import static com.google.common.base.Preconditions.checkNotNull;
import dev.cel.expr.Decl;
import dev.cel.expr.Type;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.checker.TypeProvider;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.FileDescriptorSetConverter;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.CelTypes;
import dev.cel.common.values.CelValueProvider;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerBuilder;
import dev.cel.compiler.CelCompilerImpl;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelParserBuilder;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLegacyImpl;
import dev.cel.runtime.CelRuntimeLibrary;
import java.util.Arrays;
import java.util.function.Function;
/**
* Implementation of the synchronous CEL stack.
*
* <p>Note, the underlying {@link CelCompiler} and {@link CelRuntime} values are constructed lazily.
*/
@Immutable
final class CelImpl implements Cel, EnvVisitable {
// The lazily constructed compiler and runtime values are memoized and guaranteed to be
// constructed only once without side effects, thus making them effectively immutable.
@SuppressWarnings("Immutable")
private final Supplier<CelCompiler> compiler;
@SuppressWarnings("Immutable")
private final Supplier<CelRuntime> runtime;
@Override
public CelValidationResult parse(String expression, String description) {
return compiler.get().parse(expression, description);
}
@Override
public CelValidationResult parse(CelSource source) {
return compiler.get().parse(source);
}
@Override
public CelValidationResult check(CelAbstractSyntaxTree ast) {
return compiler.get().check(ast);
}
@Override
public CelRuntime.Program createProgram(CelAbstractSyntaxTree ast) throws CelEvaluationException {
return runtime.get().createProgram(ast);
}
@Override
public void accept(EnvVisitor envVisitor) {
CelCompiler celCompiler = compiler.get();
if (celCompiler instanceof EnvVisitable) {
((EnvVisitable) celCompiler).accept(envVisitor);
}
}
/** Combines a prebuilt {@link CelCompiler} and {@link CelRuntime} into {@link CelImpl}. */
static CelImpl combine(CelCompiler compiler, CelRuntime runtime) {
return new CelImpl(Suppliers.memoize(() -> compiler), Suppliers.memoize(() -> runtime));
}
/**
* Create a new builder for constructing a {@code CelImpl} instance.
*
* <p>By default, {@link CelOptions#DEFAULT} are enabled, as is the CEL standard environment.
*/
static CelBuilder newBuilder(CelParserBuilder parserBuilder, CelCheckerBuilder checkerBuilder) {
return new CelImpl.Builder(parserBuilder, checkerBuilder);
}
/** Builder class for CelImpl instances. */
public static final class Builder implements CelBuilder {
private final CelCompilerBuilder compilerBuilder;
private final CelRuntimeBuilder runtimeBuilder;
private Builder(CelParserBuilder parserBuilder, CelCheckerBuilder checkerBuilder) {
this.compilerBuilder = CelCompilerImpl.newBuilder(parserBuilder, checkerBuilder);
this.runtimeBuilder = CelRuntimeLegacyImpl.newBuilder();
}
@Override
public CelBuilder setOptions(CelOptions options) {
compilerBuilder.setOptions(options);
runtimeBuilder.setOptions(options);
return this;
}
@Override
public CelBuilder setStandardMacros(CelStandardMacro... macros) {
compilerBuilder.setStandardMacros(macros);
return this;
}
@Override
public CelBuilder setStandardMacros(Iterable<CelStandardMacro> macros) {
compilerBuilder.setStandardMacros(macros);
return this;
}
@Override
public CelBuilder addMacros(CelMacro... macros) {
checkNotNull(macros);
return addMacros(Arrays.asList(macros));
}
@Override
public CelBuilder addMacros(Iterable<CelMacro> macros) {
checkNotNull(macros);
compilerBuilder.addMacros(macros);
return this;
}
@Override
public CelBuilder setContainer(String container) {
compilerBuilder.setContainer(container);
return this;
}
@Override
public CelBuilder addVar(String name, Type type) {
compilerBuilder.addVar(name, type);
return this;
}
@Override
public CelBuilder addVar(String name, CelType type) {
compilerBuilder.addVar(name, type);
return this;
}
@Override
public CelBuilder addDeclarations(Decl... declarations) {
compilerBuilder.addDeclarations(declarations);
return this;
}
@Override
public CelBuilder addDeclarations(Iterable<Decl> declarations) {
compilerBuilder.addDeclarations(declarations);
return this;
}
@Override
public CelBuilder addFunctionDeclarations(CelFunctionDecl... celFunctionDecls) {
compilerBuilder.addFunctionDeclarations(celFunctionDecls);
return this;
}
@Override
public CelBuilder addFunctionDeclarations(Iterable<CelFunctionDecl> celFunctionDecls) {
compilerBuilder.addFunctionDeclarations(celFunctionDecls);
return this;
}
@Override
public CelBuilder addVarDeclarations(CelVarDecl... celVarDecls) {
compilerBuilder.addVarDeclarations(celVarDecls);
return this;
}
@Override
public CelBuilder addVarDeclarations(Iterable<CelVarDecl> celVarDecls) {
compilerBuilder.addVarDeclarations(celVarDecls);
return this;
}
@Override
public CelBuilder addProtoTypeMasks(ProtoTypeMask... typeMasks) {
compilerBuilder.addProtoTypeMasks(typeMasks);
return this;
}
@Override
public CelBuilder addProtoTypeMasks(Iterable<ProtoTypeMask> typeMasks) {
compilerBuilder.addProtoTypeMasks(typeMasks);
return this;
}
@Override
public CelBuilder addFunctionBindings(CelRuntime.CelFunctionBinding... bindings) {
runtimeBuilder.addFunctionBindings(bindings);
return this;
}
@Override
public CelBuilder addFunctionBindings(Iterable<CelRuntime.CelFunctionBinding> bindings) {
runtimeBuilder.addFunctionBindings(bindings);
return this;
}
@Override
public CelBuilder setResultType(CelType resultType) {
checkNotNull(resultType);
return setProtoResultType(CelTypes.celTypeToType(resultType));
}
@Override
public CelBuilder setProtoResultType(Type resultType) {
compilerBuilder.setProtoResultType(resultType);
return this;
}
@Override
public CelBuilder setTypeFactory(Function<String, Message.Builder> typeFactory) {
runtimeBuilder.setTypeFactory(typeFactory);
return this;
}
@Override
public CelBuilder setValueProvider(CelValueProvider celValueProvider) {
runtimeBuilder.setValueProvider(celValueProvider);
return this;
}
@Override
@Deprecated
public Builder setTypeProvider(TypeProvider typeProvider) {
compilerBuilder.setTypeProvider(typeProvider);
return this;
}
@Override
public CelBuilder setTypeProvider(CelTypeProvider celTypeProvider) {
compilerBuilder.setTypeProvider(celTypeProvider);
return this;
}
@Override
public CelBuilder addMessageTypes(Descriptor... descriptors) {
compilerBuilder.addMessageTypes(descriptors);
runtimeBuilder.addMessageTypes(descriptors);
return this;
}
@Override
public CelBuilder addMessageTypes(Iterable<Descriptor> descriptors) {
compilerBuilder.addMessageTypes(descriptors);
runtimeBuilder.addMessageTypes(descriptors);
return this;
}
@Override
public CelBuilder addFileTypes(FileDescriptor... fileDescriptors) {
compilerBuilder.addFileTypes(fileDescriptors);
runtimeBuilder.addFileTypes(fileDescriptors);
return this;
}
@Override
public CelBuilder addFileTypes(Iterable<FileDescriptor> fileDescriptors) {
compilerBuilder.addFileTypes(fileDescriptors);
runtimeBuilder.addFileTypes(fileDescriptors);
return this;
}
@Override
public CelBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
// Note: The convert method here constructs unique instances of FileDescriptors, so we convert
// it here in advance and pass it down to compiler/runtime to avoid creating additional
// instances.
ImmutableSet<FileDescriptor> fileDescriptors =
FileDescriptorSetConverter.convert(fileDescriptorSet);
compilerBuilder.addFileTypes(fileDescriptors);
runtimeBuilder.addFileTypes(fileDescriptors);
return this;
}
@Override
public CelBuilder setStandardEnvironmentEnabled(boolean value) {
compilerBuilder.setStandardEnvironmentEnabled(value);
runtimeBuilder.setStandardEnvironmentEnabled(value);
return this;
}
@Override
public CelBuilder addCompilerLibraries(CelCompilerLibrary... libraries) {
checkNotNull(libraries);
return this.addCompilerLibraries(Arrays.asList(libraries));
}
@Override
public CelBuilder addCompilerLibraries(Iterable<CelCompilerLibrary> libraries) {
checkNotNull(libraries);
compilerBuilder.addLibraries(libraries);
return this;
}
@Override
public CelBuilder addRuntimeLibraries(CelRuntimeLibrary... libraries) {
checkNotNull(libraries);
return this.addRuntimeLibraries(Arrays.asList(libraries));
}
@Override
public CelBuilder addRuntimeLibraries(Iterable<CelRuntimeLibrary> libraries) {
checkNotNull(libraries);
runtimeBuilder.addLibraries(libraries);
return this;
}
@Override
public CelBuilder setExtensionRegistry(ExtensionRegistry extensionRegistry) {
checkNotNull(extensionRegistry);
runtimeBuilder.setExtensionRegistry(extensionRegistry);
return this;
}
@Override
public Cel build() {
return new CelImpl(
Suppliers.memoize(compilerBuilder::build), Suppliers.memoize(runtimeBuilder::build));
}
}
private CelImpl(Supplier<CelCompiler> compiler, Supplier<CelRuntime> runtime) {
this.compiler = compiler;
this.runtime = runtime;
}
}