@@ -78,6 +78,8 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
78
78
private final CelTypeProvider celTypeProvider ;
79
79
private final boolean standardEnvironmentEnabled ;
80
80
81
+ private final CelStandardDeclaration overriddenStandardDeclarations ;
82
+
81
83
// Builder is mutable by design. APIs must make defensive copies in and out of this class.
82
84
@ SuppressWarnings ("Immutable" )
83
85
private final Builder checkerBuilder ;
@@ -137,6 +139,8 @@ private Env getEnv(Errors errors) {
137
139
Env env ;
138
140
if (standardEnvironmentEnabled ) {
139
141
env = Env .standard (errors , typeProvider , celOptions );
142
+ } else if (overriddenStandardDeclarations != null ) {
143
+ env = Env .standard (overriddenStandardDeclarations , errors , typeProvider , celOptions );
140
144
} else {
141
145
env = Env .unconfigured (errors , typeProvider , celOptions );
142
146
}
@@ -165,6 +169,7 @@ public static final class Builder implements CelCheckerBuilder {
165
169
private TypeProvider customTypeProvider ;
166
170
private CelTypeProvider celTypeProvider ;
167
171
private boolean standardEnvironmentEnabled ;
172
+ private CelStandardDeclaration standardDeclarations ;
168
173
169
174
@ Override
170
175
public CelCheckerBuilder setOptions (CelOptions celOptions ) {
@@ -320,7 +325,13 @@ public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
320
325
321
326
@ Override
322
327
public CelCheckerBuilder setStandardEnvironmentEnabled (boolean value ) {
323
- standardEnvironmentEnabled = value ;
328
+ this .standardEnvironmentEnabled = value ;
329
+ return this ;
330
+ }
331
+
332
+ @ Override
333
+ public CelCheckerBuilder setStandardDeclarations (CelStandardDeclaration standardDeclarations ) {
334
+ this .standardDeclarations = checkNotNull (standardDeclarations );
324
335
return this ;
325
336
}
326
337
@@ -372,6 +383,11 @@ ImmutableSet.Builder<CelCheckerLibrary> getCheckerLibraries() {
372
383
@ Override
373
384
@ CheckReturnValue
374
385
public CelCheckerLegacyImpl build () {
386
+ if (standardEnvironmentEnabled && standardDeclarations != null ) {
387
+ throw new IllegalArgumentException (
388
+ "setStandardEnvironmentEnabled must be set to false to override standard"
389
+ + " declarations." );
390
+ }
375
391
// Add libraries, such as extensions
376
392
celCheckerLibraries .build ().forEach (celLibrary -> celLibrary .setCheckerOptions (this ));
377
393
@@ -430,6 +446,7 @@ public CelCheckerLegacyImpl build() {
430
446
legacyProvider ,
431
447
messageTypeProvider ,
432
448
standardEnvironmentEnabled ,
449
+ standardDeclarations ,
433
450
this );
434
451
}
435
452
@@ -478,6 +495,7 @@ private CelCheckerLegacyImpl(
478
495
TypeProvider typeProvider ,
479
496
CelTypeProvider celTypeProvider ,
480
497
boolean standardEnvironmentEnabled ,
498
+ CelStandardDeclaration overriddenStandardDeclarations ,
481
499
Builder checkerBuilder ) {
482
500
this .celOptions = celOptions ;
483
501
this .container = container ;
@@ -487,6 +505,7 @@ private CelCheckerLegacyImpl(
487
505
this .typeProvider = typeProvider ;
488
506
this .celTypeProvider = celTypeProvider ;
489
507
this .standardEnvironmentEnabled = standardEnvironmentEnabled ;
508
+ this .overriddenStandardDeclarations = overriddenStandardDeclarations ;
490
509
this .checkerBuilder = new Builder (checkerBuilder );
491
510
}
492
511
0 commit comments