-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathCelCheckerLegacyImpl.java
More file actions
566 lines (498 loc) · 19.9 KB
/
Copy pathCelCheckerLegacyImpl.java
File metadata and controls
566 lines (498 loc) · 19.9 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// 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.checker;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import dev.cel.expr.Decl;
import dev.cel.expr.Type;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
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 dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelContainer;
import dev.cel.common.CelDescriptorUtil;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelIssue;
import dev.cel.common.CelOptions;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.CelSource;
import dev.cel.common.CelSourceLocation;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.ast.CelExprConverter;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.Errors;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.ProtoMessageTypeProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.jspecify.annotations.Nullable;
/**
* {@code CelChecker} implementation which uses the original CEL-Java APIs to provide a simple,
* consistent interface for type-checking.
*
* <p>CEL Library Internals. Do Not Use. Consumers should use {@code CelCompilerFactory} to
* instantiate a type-checker.
*/
@Immutable
@Internal
public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
private final CelOptions celOptions;
private final CelContainer container;
private final ImmutableSet<CelIdentDecl> identDeclarations;
private final ImmutableSet<CelFunctionDecl> functionDeclarations;
private final Optional<CelType> expectedResultType;
@SuppressWarnings("Immutable")
private final TypeProvider typeProvider;
private final CelTypeProvider celTypeProvider;
private final boolean standardEnvironmentEnabled;
private final CelStandardDeclarations overriddenStandardDeclarations;
// This does not affect the type-checking behavior in any manner.
@SuppressWarnings("Immutable")
private final ImmutableSet<CelCheckerLibrary> checkerLibraries;
private final ImmutableSet<FileDescriptor> fileDescriptors;
private final ImmutableSet<ProtoTypeMask> protoTypeMasks;
@Override
public CelValidationResult check(CelAbstractSyntaxTree ast) {
CelSource source = ast.getSource();
Errors errors = new Errors(source.getDescription(), source.getContent().toString());
Env env = getEnv(errors);
if (errors.getErrorCount() > 0) {
return new CelValidationResult(source, errorsToIssues(errors));
}
CelAbstractSyntaxTree checkedAst =
ExprChecker.typecheck(env, container, ast, expectedResultType);
if (errors.getErrorCount() > 0) {
return new CelValidationResult(source, errorsToIssues(errors));
}
return new CelValidationResult(checkedAst, ImmutableList.of());
}
@Override
public CelTypeProvider getTypeProvider() {
return this.celTypeProvider;
}
@Override
public CelCheckerBuilder toCheckerBuilder() {
CelCheckerBuilder builder =
new Builder()
.addIdentDeclarations(identDeclarations)
.setOptions(celOptions)
.setTypeProvider(celTypeProvider)
.setContainer(container)
.setStandardEnvironmentEnabled(standardEnvironmentEnabled)
.addFunctionDeclarations(functionDeclarations)
.addLibraries(checkerLibraries)
.addFileTypes(fileDescriptors)
.addProtoTypeMasks(protoTypeMasks);
if (expectedResultType.isPresent()) {
builder.setResultType(expectedResultType.get());
}
if (overriddenStandardDeclarations != null) {
builder.setStandardDeclarations(overriddenStandardDeclarations);
}
return builder;
}
@Override
public void accept(EnvVisitor envVisitor) {
Errors errors = new Errors("", "");
Env env = getEnv(errors);
for (int i = env.scopeDepth(); i >= 0; i--) {
Env.DeclGroup declGroup = env.getDeclGroup(i);
SortedSet<String> names = new TreeSet<>();
names.addAll(declGroup.getIdents().keySet());
names.addAll(declGroup.getFunctions().keySet());
for (String name : names) {
CelIdentDecl ident = declGroup.getIdent(name);
CelFunctionDecl func = declGroup.getFunction(name);
List<Decl> decls = new ArrayList<>();
if (ident != null) {
decls.add(CelIdentDecl.celIdentToDecl(ident));
}
if (func != null) {
decls.add(CelFunctionDecl.celFunctionDeclToDecl(func));
}
envVisitor.visitDecl(name, decls);
}
}
}
private Env getEnv(Errors errors) {
Env env;
if (standardEnvironmentEnabled) {
env = Env.standard(errors, typeProvider, celOptions);
} else if (overriddenStandardDeclarations != null) {
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
} else {
env = Env.unconfigured(errors, typeProvider, celOptions);
}
identDeclarations.forEach(env::add);
functionDeclarations.forEach(env::add);
return env;
}
/** Create a new builder to construct a {@code CelChecker} instance. */
public static CelCheckerBuilder newBuilder() {
return new Builder();
}
/** Builder class for the legacy {@code CelChecker} implementation. */
public static final class Builder implements CelCheckerBuilder {
private final ImmutableSet.Builder<CelIdentDecl> identDeclarations;
private final ImmutableSet.Builder<CelFunctionDecl> functionDeclarations;
private final ImmutableSet.Builder<ProtoTypeMask> protoTypeMasks;
private final ImmutableSet.Builder<Descriptor> messageTypes;
private final ImmutableSet.Builder<FileDescriptor> fileTypes;
private final ImmutableSet.Builder<CelCheckerLibrary> celCheckerLibraries;
private CelContainer container;
private CelOptions celOptions;
private CelType expectedResultType;
private TypeProvider customTypeProvider;
private CelTypeProvider celTypeProvider;
private boolean standardEnvironmentEnabled;
private CelStandardDeclarations standardDeclarations;
@Override
public CelCheckerBuilder setOptions(CelOptions celOptions) {
this.celOptions = checkNotNull(celOptions);
return this;
}
@Override
public CelCheckerBuilder setContainer(CelContainer container) {
checkNotNull(container);
this.container = container;
return this;
}
@Override
public CelContainer container() {
return this.container;
}
@Override
public CelCheckerBuilder addDeclarations(Decl... declarations) {
checkNotNull(declarations);
return addDeclarations(Arrays.asList(declarations));
}
@Override
public CelCheckerBuilder addDeclarations(Iterable<Decl> declarations) {
checkNotNull(declarations);
for (Decl decl : declarations) {
switch (decl.getDeclKindCase()) {
case IDENT:
CelIdentDecl.Builder identBuilder =
CelIdentDecl.newBuilder()
.setName(decl.getName())
.setType(CelProtoTypes.typeToCelType(decl.getIdent().getType()))
// Note: Setting doc and constant value exists for compatibility reason. This
// should not be set by the users.
.setDoc(decl.getIdent().getDoc());
if (decl.getIdent().hasValue()) {
identBuilder.setConstant(
CelExprConverter.exprConstantToCelConstant(decl.getIdent().getValue()));
}
this.identDeclarations.add(identBuilder.build());
break;
case FUNCTION:
addFunctionDeclarations(
CelFunctionDecl.newFunctionDeclaration(
decl.getName(),
decl.getFunction().getOverloadsList().stream()
.map(CelOverloadDecl::overloadToCelOverload)
.collect(toImmutableList())));
break;
default:
throw new IllegalArgumentException("unexpected decl kind: " + decl.getDeclKindCase());
}
}
return this;
}
@Override
@CanIgnoreReturnValue
public CelCheckerBuilder addFunctionDeclarations(CelFunctionDecl... celFunctionDecls) {
checkNotNull(celFunctionDecls);
return addFunctionDeclarations(Arrays.asList(celFunctionDecls));
}
@Override
public CelCheckerBuilder addFunctionDeclarations(Iterable<CelFunctionDecl> celFunctionDecls) {
checkNotNull(celFunctionDecls);
this.functionDeclarations.addAll(celFunctionDecls);
return this;
}
@Override
public CelCheckerBuilder addVarDeclarations(CelVarDecl... celVarDecls) {
checkNotNull(celVarDecls);
return addVarDeclarations(Arrays.asList(celVarDecls));
}
@Override
public CelCheckerBuilder addVarDeclarations(Iterable<CelVarDecl> celVarDecls) {
checkNotNull(celVarDecls);
for (CelVarDecl celVarDecl : celVarDecls) {
this.identDeclarations.add(
CelIdentDecl.newIdentDeclaration(celVarDecl.name(), celVarDecl.type()));
}
return this;
}
@Override
public CelCheckerBuilder addProtoTypeMasks(ProtoTypeMask... typeMasks) {
checkNotNull(typeMasks);
return addProtoTypeMasks(Arrays.asList(typeMasks));
}
@Override
public CelCheckerBuilder addProtoTypeMasks(Iterable<ProtoTypeMask> typeMasks) {
checkNotNull(typeMasks);
protoTypeMasks.addAll(typeMasks);
return this;
}
@Override
public CelCheckerBuilder setResultType(CelType resultType) {
checkNotNull(resultType);
this.expectedResultType = resultType;
return this;
}
@Override
public CelCheckerBuilder setProtoResultType(Type resultType) {
checkNotNull(resultType);
return setResultType(CelProtoTypes.typeToCelType(resultType));
}
@Override
@Deprecated
public CelCheckerBuilder setTypeProvider(TypeProvider typeProvider) {
this.customTypeProvider = typeProvider;
return this;
}
@Override
public CelCheckerBuilder setTypeProvider(CelTypeProvider celTypeProvider) {
this.celTypeProvider = celTypeProvider;
return this;
}
@Override
public CelCheckerBuilder addMessageTypes(Descriptor... descriptors) {
return addMessageTypes(Arrays.asList(checkNotNull(descriptors)));
}
@Override
public CelCheckerBuilder addMessageTypes(Iterable<Descriptor> descriptors) {
this.messageTypes.addAll(checkNotNull(descriptors));
return this;
}
@Override
public CelCheckerBuilder addFileTypes(FileDescriptor... fileDescriptors) {
return addFileTypes(Arrays.asList(checkNotNull(fileDescriptors)));
}
@Override
public CelCheckerBuilder addFileTypes(Iterable<FileDescriptor> fileDescriptors) {
this.fileTypes.addAll(checkNotNull(fileDescriptors));
return this;
}
@Override
public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
checkNotNull(fileDescriptorSet);
return addFileTypes(
CelDescriptorUtil.getFileDescriptorsFromFileDescriptorSet(fileDescriptorSet));
}
@Override
public CelCheckerBuilder setStandardEnvironmentEnabled(boolean value) {
this.standardEnvironmentEnabled = value;
return this;
}
@Override
public CelCheckerBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations) {
this.standardDeclarations = checkNotNull(standardDeclarations);
return this;
}
@Override
public CelCheckerBuilder addLibraries(CelCheckerLibrary... libraries) {
checkNotNull(libraries);
return this.addLibraries(Arrays.asList(libraries));
}
@Override
public CelCheckerBuilder addLibraries(Iterable<? extends CelCheckerLibrary> libraries) {
checkNotNull(libraries);
this.celCheckerLibraries.addAll(libraries);
return this;
}
@CanIgnoreReturnValue
Builder addIdentDeclarations(ImmutableSet<CelIdentDecl> identDeclarations) {
this.identDeclarations.addAll(identDeclarations);
return this;
}
// The following getters marked @VisibleForTesting exist for testing toCheckerBuilder copies
// over all properties. Do not expose these to public
@VisibleForTesting
ImmutableSet.Builder<CelFunctionDecl> functionDecls() {
return this.functionDeclarations;
}
@VisibleForTesting
ImmutableSet.Builder<CelIdentDecl> identDecls() {
return this.identDeclarations;
}
@VisibleForTesting
ImmutableSet.Builder<ProtoTypeMask> protoTypeMasks() {
return this.protoTypeMasks;
}
@VisibleForTesting
ImmutableSet.Builder<Descriptor> messageTypes() {
return this.messageTypes;
}
@VisibleForTesting
ImmutableSet.Builder<FileDescriptor> fileTypes() {
return this.fileTypes;
}
@VisibleForTesting
ImmutableSet.Builder<CelCheckerLibrary> checkerLibraries() {
return this.celCheckerLibraries;
}
@VisibleForTesting
CelStandardDeclarations standardDeclarations() {
return this.standardDeclarations;
}
@VisibleForTesting
CelOptions options() {
return this.celOptions;
}
@VisibleForTesting
CelTypeProvider celTypeProvider() {
return this.celTypeProvider;
}
@Override
@CheckReturnValue
public CelCheckerLegacyImpl build() {
if (standardEnvironmentEnabled && standardDeclarations != null) {
throw new IllegalArgumentException(
"setStandardEnvironmentEnabled must be set to false to override standard"
+ " declarations.");
}
// Add libraries, such as extensions
ImmutableSet<CelCheckerLibrary> checkerLibraries = celCheckerLibraries.build();
checkerLibraries.forEach(celLibrary -> celLibrary.setCheckerOptions(this));
// Configure the type provider.
ImmutableSet<FileDescriptor> fileTypeSet = fileTypes.build();
ImmutableSet<Descriptor> messageTypeSet = messageTypes.build();
if (!messageTypeSet.isEmpty()) {
fileTypeSet =
new ImmutableSet.Builder<FileDescriptor>()
.addAll(fileTypeSet)
.addAll(messageTypeSet.stream().map(Descriptor::getFile).collect(toImmutableSet()))
.build();
}
CelTypeProvider messageTypeProvider =
ProtoMessageTypeProvider.newBuilder()
.setAllowJsonFieldNames(celOptions.enableJsonFieldNames())
.setResolveTypeDependencies(celOptions.resolveTypeDependencies())
.addFileDescriptors(fileTypeSet)
.build();
if (celTypeProvider != null && fileTypeSet.isEmpty()) {
messageTypeProvider = celTypeProvider;
} else if (celTypeProvider != null) {
messageTypeProvider =
new CelTypeProvider.CombinedCelTypeProvider(
ImmutableList.of(celTypeProvider, messageTypeProvider));
}
// Configure the declaration set, and possibly alter the type provider if ProtoDecl values
// are provided as they may prevent the use of certain field selection patterns against the
// proto.
ImmutableSet<CelIdentDecl> identDeclarationSet = identDeclarations.build();
ImmutableSet<ProtoTypeMask> protoTypeMaskSet = protoTypeMasks.build();
if (!protoTypeMaskSet.isEmpty()) {
ProtoTypeMaskTypeProvider protoTypeMaskTypeProvider =
new ProtoTypeMaskTypeProvider(messageTypeProvider, protoTypeMaskSet);
identDeclarationSet =
ImmutableSet.<CelIdentDecl>builder()
.addAll(identDeclarationSet)
.addAll(protoTypeMaskTypeProvider.computeDeclsFromProtoTypeMasks())
.build();
messageTypeProvider = protoTypeMaskTypeProvider;
}
TypeProvider legacyProvider = new TypeProviderLegacyImpl(messageTypeProvider);
if (customTypeProvider != null) {
legacyProvider =
new TypeProvider.CombinedTypeProvider(
ImmutableList.of(customTypeProvider, legacyProvider));
}
return new CelCheckerLegacyImpl(
celOptions,
container,
identDeclarationSet,
functionDeclarations.build(),
Optional.fromNullable(expectedResultType),
legacyProvider,
messageTypeProvider,
standardEnvironmentEnabled,
standardDeclarations,
checkerLibraries,
fileTypeSet,
protoTypeMaskSet);
}
private Builder() {
this.celOptions = CelOptions.newBuilder().build();
this.identDeclarations = ImmutableSet.builder();
this.functionDeclarations = ImmutableSet.builder();
this.fileTypes = ImmutableSet.builder();
this.messageTypes = ImmutableSet.builder();
this.protoTypeMasks = ImmutableSet.builder();
this.celCheckerLibraries = ImmutableSet.builder();
this.container = CelContainer.ofName("");
}
}
private CelCheckerLegacyImpl(
CelOptions celOptions,
CelContainer container,
ImmutableSet<CelIdentDecl> identDeclarations,
ImmutableSet<CelFunctionDecl> functionDeclarations,
Optional<CelType> expectedResultType,
TypeProvider typeProvider,
CelTypeProvider celTypeProvider,
boolean standardEnvironmentEnabled,
@Nullable CelStandardDeclarations overriddenStandardDeclarations,
ImmutableSet<CelCheckerLibrary> checkerLibraries,
ImmutableSet<FileDescriptor> fileDescriptors,
ImmutableSet<ProtoTypeMask> protoTypeMasks) {
this.celOptions = celOptions;
this.container = container;
this.identDeclarations = identDeclarations;
this.functionDeclarations = functionDeclarations;
this.expectedResultType = expectedResultType;
this.typeProvider = typeProvider;
this.celTypeProvider = celTypeProvider;
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
this.overriddenStandardDeclarations = overriddenStandardDeclarations;
this.checkerLibraries = checkerLibraries;
this.fileDescriptors = fileDescriptors;
this.protoTypeMasks = protoTypeMasks;
}
private static ImmutableList<CelIssue> errorsToIssues(Errors errors) {
ImmutableList<Errors.Error> errorList = errors.getErrors();
CelIssue.Builder issueBuilder = CelIssue.newBuilder().setSeverity(CelIssue.Severity.ERROR);
return errorList.stream()
.map(
e -> {
Errors.SourceLocation loc = errors.getPositionLocation(e.position());
CelSourceLocation newLoc = CelSourceLocation.of(loc.line(), loc.column() - 1);
return issueBuilder
.setExprId(e.exprId())
.setMessage(e.rawMessage())
.setSourceLocation(newLoc)
.build();
})
.collect(toImmutableList());
}
}