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,8 @@ 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 ;
168188 private CelType expectedResultType ;
169189 private TypeProvider customTypeProvider ;
170190 private CelTypeProvider celTypeProvider ;
@@ -179,6 +199,12 @@ public CelCheckerBuilder setOptions(CelOptions celOptions) {
179199
180200 @ Override
181201 public CelCheckerBuilder setContainer (String container ) {
202+ checkNotNull (container );
203+ return setContainer (CelContainer .ofName (container ));
204+ }
205+
206+ @ Override
207+ public CelCheckerBuilder setContainer (CelContainer container ) {
182208 checkNotNull (container );
183209 this .container = container ;
184210 return this ;
@@ -348,6 +374,12 @@ public CelCheckerBuilder addLibraries(Iterable<? extends CelCheckerLibrary> libr
348374 return this ;
349375 }
350376
377+ @ CanIgnoreReturnValue
378+ Builder addIdentDeclarations (ImmutableSet <CelIdentDecl > identDeclarations ) {
379+ this .identDeclarations .addAll (identDeclarations );
380+ return this ;
381+ }
382+
351383 // The following getters exist for asserting immutability for collections held by this builder,
352384 // and shouldn't be exposed to the public.
353385 @ VisibleForTesting
@@ -388,8 +420,10 @@ public CelCheckerLegacyImpl build() {
388420 "setStandardEnvironmentEnabled must be set to false to override standard"
389421 + " declarations." );
390422 }
423+
391424 // Add libraries, such as extensions
392- celCheckerLibraries .build ().forEach (celLibrary -> celLibrary .setCheckerOptions (this ));
425+ ImmutableSet <CelCheckerLibrary > checkerLibraries = celCheckerLibraries .build ();
426+ checkerLibraries .forEach (celLibrary -> celLibrary .setCheckerOptions (this ));
393427
394428 // Configure the type provider.
395429 ImmutableSet <FileDescriptor > fileTypeSet = fileTypes .build ();
@@ -447,7 +481,9 @@ public CelCheckerLegacyImpl build() {
447481 messageTypeProvider ,
448482 standardEnvironmentEnabled ,
449483 standardDeclarations ,
450- this );
484+ checkerLibraries ,
485+ fileTypeSet ,
486+ protoTypeMaskSet );
451487 }
452488
453489 private Builder () {
@@ -458,45 +494,23 @@ private Builder() {
458494 this .messageTypes = ImmutableSet .builder ();
459495 this .protoTypeMasks = ImmutableSet .builder ();
460496 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 ;
497+ this .container = CelContainer .ofName ("" );
486498 }
487499 }
488500
489501 private CelCheckerLegacyImpl (
490502 CelOptions celOptions ,
491- String container ,
503+ CelContainer container ,
492504 ImmutableSet <CelIdentDecl > identDeclarations ,
493505 ImmutableSet <CelFunctionDecl > functionDeclarations ,
494506 Optional <CelType > expectedResultType ,
495507 TypeProvider typeProvider ,
496508 CelTypeProvider celTypeProvider ,
497509 boolean standardEnvironmentEnabled ,
498510 CelStandardDeclarations overriddenStandardDeclarations ,
499- Builder checkerBuilder ) {
511+ ImmutableSet <CelCheckerLibrary > checkerLibraries ,
512+ ImmutableSet <FileDescriptor > fileDescriptors ,
513+ ImmutableSet <ProtoTypeMask > protoTypeMasks ) {
500514 this .celOptions = celOptions ;
501515 this .container = container ;
502516 this .identDeclarations = identDeclarations ;
@@ -506,7 +520,9 @@ private CelCheckerLegacyImpl(
506520 this .celTypeProvider = celTypeProvider ;
507521 this .standardEnvironmentEnabled = standardEnvironmentEnabled ;
508522 this .overriddenStandardDeclarations = overriddenStandardDeclarations ;
509- this .checkerBuilder = new Builder (checkerBuilder );
523+ this .checkerLibraries = checkerLibraries ;
524+ this .fileDescriptors = fileDescriptors ;
525+ this .protoTypeMasks = protoTypeMasks ;
510526 }
511527
512528 private static ImmutableList <CelIssue > errorsToIssues (Errors errors ) {
0 commit comments