3131import com .google .protobuf .Descriptors .Descriptor ;
3232import com .google .protobuf .Descriptors .FileDescriptor ;
3333import dev .cel .common .CelAbstractSyntaxTree ;
34+ import dev .cel .common .CelContainer ;
3435import dev .cel .common .CelDescriptorUtil ;
3536import dev .cel .common .CelFunctionDecl ;
3637import dev .cel .common .CelIssue ;
6768public final class CelCheckerLegacyImpl implements CelChecker , EnvVisitable {
6869
6970 private final CelOptions celOptions ;
70- private final String container ;
71+ private final CelContainer container ;
7172 private final ImmutableSet <CelIdentDecl > identDeclarations ;
7273 private final ImmutableSet <CelFunctionDecl > functionDeclarations ;
7374 private final Optional <CelType > expectedResultType ;
@@ -80,9 +81,12 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
8081
8182 private final CelStandardDeclarations overriddenStandardDeclarations ;
8283
83- // Builder is mutable by design. APIs must make defensive copies in and out of this class .
84+ // This does not affect the type-checking behavior in any manner .
8485 @ SuppressWarnings ("Immutable" )
85- private final Builder checkerBuilder ;
86+ private final ImmutableSet <CelCheckerLibrary > checkerLibraries ;
87+
88+ private final ImmutableSet <FileDescriptor > fileDescriptors ;
89+ private final ImmutableSet <ProtoTypeMask > protoTypeMasks ;
8690
8791 @ Override
8892 public CelValidationResult check (CelAbstractSyntaxTree ast ) {
@@ -108,7 +112,23 @@ public CelTypeProvider getTypeProvider() {
108112
109113 @ Override
110114 public CelCheckerBuilder toCheckerBuilder () {
111- return new Builder (checkerBuilder );
115+ CelCheckerBuilder builder =
116+ new Builder ()
117+ .addIdentDeclarations (identDeclarations )
118+ .setOptions (celOptions )
119+ .setTypeProvider (celTypeProvider )
120+ .setContainer (container )
121+ .setStandardEnvironmentEnabled (standardEnvironmentEnabled )
122+ .addFunctionDeclarations (functionDeclarations )
123+ .addLibraries (checkerLibraries )
124+ .addFileTypes (fileDescriptors )
125+ .addProtoTypeMasks (protoTypeMasks );
126+
127+ if (expectedResultType .isPresent ()) {
128+ builder .setResultType (expectedResultType .get ());
129+ }
130+
131+ return builder ;
112132 }
113133
114134 @ Override
@@ -163,8 +183,9 @@ public static final class Builder implements CelCheckerBuilder {
163183 private final ImmutableSet .Builder <Descriptor > messageTypes ;
164184 private final ImmutableSet .Builder <FileDescriptor > fileTypes ;
165185 private final ImmutableSet .Builder <CelCheckerLibrary > celCheckerLibraries ;
186+ private CelContainer container ;
166187 private CelOptions celOptions ;
167- private String container ;
188+ private String deprecatedContainerString ;
168189 private CelType expectedResultType ;
169190 private TypeProvider customTypeProvider ;
170191 private CelTypeProvider celTypeProvider ;
@@ -179,6 +200,13 @@ public CelCheckerBuilder setOptions(CelOptions celOptions) {
179200
180201 @ Override
181202 public CelCheckerBuilder setContainer (String container ) {
203+ checkNotNull (container );
204+ this .deprecatedContainerString = container ;
205+ return this ;
206+ }
207+
208+ @ Override
209+ public CelCheckerBuilder setContainer (CelContainer container ) {
182210 checkNotNull (container );
183211 this .container = container ;
184212 return this ;
@@ -348,6 +376,12 @@ public CelCheckerBuilder addLibraries(Iterable<? extends CelCheckerLibrary> libr
348376 return this ;
349377 }
350378
379+ @ CanIgnoreReturnValue
380+ Builder addIdentDeclarations (ImmutableSet <CelIdentDecl > identDeclarations ) {
381+ this .identDeclarations .addAll (identDeclarations );
382+ return this ;
383+ }
384+
351385 // The following getters exist for asserting immutability for collections held by this builder,
352386 // and shouldn't be exposed to the public.
353387 @ VisibleForTesting
@@ -388,8 +422,22 @@ public CelCheckerLegacyImpl build() {
388422 "setStandardEnvironmentEnabled must be set to false to override standard"
389423 + " declarations." );
390424 }
425+
426+ if (!deprecatedContainerString .isEmpty () && !container .name ().isEmpty ()) {
427+ throw new IllegalArgumentException (
428+ "Both container and celContainer cannot be set. Container: "
429+ + deprecatedContainerString
430+ + ",celContainer: "
431+ + container );
432+ }
433+ if (!deprecatedContainerString .isEmpty ()) {
434+ // Compatibility only. Will be removed.
435+ container = CelContainer .ofName (deprecatedContainerString );
436+ }
437+
391438 // Add libraries, such as extensions
392- celCheckerLibraries .build ().forEach (celLibrary -> celLibrary .setCheckerOptions (this ));
439+ ImmutableSet <CelCheckerLibrary > checkerLibraries = celCheckerLibraries .build ();
440+ checkerLibraries .forEach (celLibrary -> celLibrary .setCheckerOptions (this ));
393441
394442 // Configure the type provider.
395443 ImmutableSet <FileDescriptor > fileTypeSet = fileTypes .build ();
@@ -447,7 +495,9 @@ public CelCheckerLegacyImpl build() {
447495 messageTypeProvider ,
448496 standardEnvironmentEnabled ,
449497 standardDeclarations ,
450- this );
498+ checkerLibraries ,
499+ fileTypeSet ,
500+ protoTypeMaskSet );
451501 }
452502
453503 private Builder () {
@@ -458,45 +508,24 @@ private Builder() {
458508 this .messageTypes = ImmutableSet .builder ();
459509 this .protoTypeMasks = ImmutableSet .builder ();
460510 this .celCheckerLibraries = ImmutableSet .builder ();
461- this .container = "" ;
462- }
463-
464- private Builder (Builder builder ) {
465- // The following properties are either immutable or simple primitives, thus can be assigned
466- // directly.
467- this .celOptions = builder .celOptions ;
468- this .celTypeProvider = builder .celTypeProvider ;
469- this .container = builder .container ;
470- this .customTypeProvider = builder .customTypeProvider ;
471- this .expectedResultType = builder .expectedResultType ;
472- this .standardEnvironmentEnabled = builder .standardEnvironmentEnabled ;
473- // The following needs to be deep copied as they are collection builders
474- this .functionDeclarations = deepCopy (builder .functionDeclarations );
475- this .identDeclarations = deepCopy (builder .identDeclarations );
476- this .fileTypes = deepCopy (builder .fileTypes );
477- this .messageTypes = deepCopy (builder .messageTypes );
478- this .protoTypeMasks = deepCopy (builder .protoTypeMasks );
479- this .celCheckerLibraries = deepCopy (builder .celCheckerLibraries );
480- }
481-
482- private static <T > ImmutableSet .Builder <T > deepCopy (ImmutableSet .Builder <T > builderToCopy ) {
483- ImmutableSet .Builder <T > newBuilder = ImmutableSet .builder ();
484- newBuilder .addAll (builderToCopy .build ());
485- return newBuilder ;
511+ this .container = CelContainer .ofName ("" );
512+ this .deprecatedContainerString = "" ;
486513 }
487514 }
488515
489516 private CelCheckerLegacyImpl (
490517 CelOptions celOptions ,
491- String container ,
518+ CelContainer container ,
492519 ImmutableSet <CelIdentDecl > identDeclarations ,
493520 ImmutableSet <CelFunctionDecl > functionDeclarations ,
494521 Optional <CelType > expectedResultType ,
495522 TypeProvider typeProvider ,
496523 CelTypeProvider celTypeProvider ,
497524 boolean standardEnvironmentEnabled ,
498525 CelStandardDeclarations overriddenStandardDeclarations ,
499- Builder checkerBuilder ) {
526+ ImmutableSet <CelCheckerLibrary > checkerLibraries ,
527+ ImmutableSet <FileDescriptor > fileDescriptors ,
528+ ImmutableSet <ProtoTypeMask > protoTypeMasks ) {
500529 this .celOptions = celOptions ;
501530 this .container = container ;
502531 this .identDeclarations = identDeclarations ;
@@ -506,7 +535,9 @@ private CelCheckerLegacyImpl(
506535 this .celTypeProvider = celTypeProvider ;
507536 this .standardEnvironmentEnabled = standardEnvironmentEnabled ;
508537 this .overriddenStandardDeclarations = overriddenStandardDeclarations ;
509- this .checkerBuilder = new Builder (checkerBuilder );
538+ this .checkerLibraries = checkerLibraries ;
539+ this .fileDescriptors = fileDescriptors ;
540+ this .protoTypeMasks = protoTypeMasks ;
510541 }
511542
512543 private static ImmutableList <CelIssue > errorsToIssues (Errors errors ) {
0 commit comments