3636import dev .cel .common .CelFunctionDecl ;
3737import dev .cel .common .CelIssue ;
3838import dev .cel .common .CelOptions ;
39- import dev .cel .common .CelOverloadDecl ;
39+ import dev .cel .common .CelProtoDeclConverter ;
4040import dev .cel .common .CelSource ;
4141import dev .cel .common .CelSourceLocation ;
4242import dev .cel .common .CelValidationResult ;
4343import dev .cel .common .CelVarDecl ;
4444import dev .cel .common .annotations .Internal ;
45- import dev .cel .common .ast .CelExprConverter ;
4645import dev .cel .common .internal .EnvVisitable ;
4746import dev .cel .common .internal .EnvVisitor ;
4847import dev .cel .common .internal .Errors ;
@@ -70,7 +69,7 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
7069
7170 private final CelOptions celOptions ;
7271 private final CelContainer container ;
73- private final ImmutableSet <CelIdentDecl > identDeclarations ;
72+ private final ImmutableSet <CelVarDecl > identDeclarations ;
7473 private final ImmutableSet <CelFunctionDecl > functionDeclarations ;
7574 private final Optional <CelType > expectedResultType ;
7675
@@ -115,7 +114,7 @@ public CelTypeProvider getTypeProvider() {
115114 public CelCheckerBuilder toCheckerBuilder () {
116115 CelCheckerBuilder builder =
117116 new Builder ()
118- .addIdentDeclarations (identDeclarations )
117+ .addVarDeclarations (identDeclarations )
119118 .setOptions (celOptions )
120119 .setTypeProvider (celTypeProvider )
121120 .setContainer (container )
@@ -146,14 +145,14 @@ public void accept(EnvVisitor envVisitor) {
146145 names .addAll (declGroup .getIdents ().keySet ());
147146 names .addAll (declGroup .getFunctions ().keySet ());
148147 for (String name : names ) {
149- CelIdentDecl ident = declGroup .getIdent (name );
148+ CelVarDecl ident = declGroup .getIdent (name );
150149 CelFunctionDecl func = declGroup .getFunction (name );
151150 List <Decl > decls = new ArrayList <>();
152151 if (ident != null ) {
153- decls .add (CelIdentDecl . celIdentToDecl (ident ));
152+ decls .add (CelProtoDeclConverter . celVarDeclToDecl (ident ));
154153 }
155154 if (func != null ) {
156- decls .add (CelFunctionDecl .celFunctionDeclToDecl (func ));
155+ decls .add (CelProtoDeclConverter .celFunctionDeclToDecl (func ));
157156 }
158157 envVisitor .visitDecl (name , decls );
159158 }
@@ -182,7 +181,7 @@ public static CelCheckerBuilder newBuilder() {
182181 /** Builder class for the legacy {@code CelChecker} implementation. */
183182 public static final class Builder implements CelCheckerBuilder {
184183
185- private final ImmutableSet .Builder <CelIdentDecl > identDeclarations ;
184+ private final ImmutableSet .Builder <CelVarDecl > identDeclarations ;
186185 private final ImmutableSet .Builder <CelFunctionDecl > functionDeclarations ;
187186 private final ImmutableSet .Builder <ProtoTypeMask > protoTypeMasks ;
188187 private final ImmutableSet .Builder <Descriptor > messageTypes ;
@@ -231,27 +230,10 @@ public CelCheckerBuilder addDeclarations(Iterable<Decl> declarations) {
231230 for (Decl decl : declarations ) {
232231 switch (decl .getDeclKindCase ()) {
233232 case IDENT :
234- CelIdentDecl .Builder identBuilder =
235- CelIdentDecl .newBuilder ()
236- .setName (decl .getName ())
237- .setType (CelProtoTypes .typeToCelType (decl .getIdent ().getType ()))
238- // Note: Setting doc and constant value exists for compatibility reason. This
239- // should not be set by the users.
240- .setDoc (decl .getIdent ().getDoc ());
241- if (decl .getIdent ().hasValue ()) {
242- identBuilder .setConstant (
243- CelExprConverter .exprConstantToCelConstant (decl .getIdent ().getValue ()));
244- }
245-
246- this .identDeclarations .add (identBuilder .build ());
233+ this .identDeclarations .add (CelProtoDeclConverter .declToCelVarDecl (decl ));
247234 break ;
248235 case FUNCTION :
249- addFunctionDeclarations (
250- CelFunctionDecl .newFunctionDeclaration (
251- decl .getName (),
252- decl .getFunction ().getOverloadsList ().stream ()
253- .map (CelOverloadDecl ::overloadToCelOverload )
254- .collect (toImmutableList ())));
236+ addFunctionDeclarations (CelProtoDeclConverter .declToCelFunctionDecl (decl ));
255237 break ;
256238 default :
257239 throw new IllegalArgumentException ("unexpected decl kind: " + decl .getDeclKindCase ());
@@ -283,10 +265,7 @@ public CelCheckerBuilder addVarDeclarations(CelVarDecl... celVarDecls) {
283265 @ Override
284266 public CelCheckerBuilder addVarDeclarations (Iterable <CelVarDecl > celVarDecls ) {
285267 checkNotNull (celVarDecls );
286- for (CelVarDecl celVarDecl : celVarDecls ) {
287- this .identDeclarations .add (
288- CelIdentDecl .newIdentDeclaration (celVarDecl .name (), celVarDecl .type ()));
289- }
268+ this .identDeclarations .addAll (celVarDecls );
290269 return this ;
291270 }
292271
@@ -384,12 +363,6 @@ public CelCheckerBuilder addLibraries(Iterable<? extends CelCheckerLibrary> libr
384363 return this ;
385364 }
386365
387- @ CanIgnoreReturnValue
388- Builder addIdentDeclarations (ImmutableSet <CelIdentDecl > identDeclarations ) {
389- this .identDeclarations .addAll (identDeclarations );
390- return this ;
391- }
392-
393366 // The following getters marked @VisibleForTesting exist for testing toCheckerBuilder copies
394367 // over all properties. Do not expose these to public
395368 @ VisibleForTesting
@@ -398,7 +371,7 @@ ImmutableSet.Builder<CelFunctionDecl> functionDecls() {
398371 }
399372
400373 @ VisibleForTesting
401- ImmutableSet .Builder <CelIdentDecl > identDecls () {
374+ ImmutableSet .Builder <CelVarDecl > identDecls () {
402375 return this .identDeclarations ;
403376 }
404377
@@ -468,15 +441,20 @@ public CelCheckerLegacyImpl build() {
468441 // Configure the declaration set, and possibly alter the type provider if ProtoDecl values
469442 // are provided as they may prevent the use of certain field selection patterns against the
470443 // proto.
471- ImmutableSet <CelIdentDecl > identDeclarationSet = identDeclarations .build ();
444+ ImmutableSet <CelVarDecl > identDeclarationSet = identDeclarations .build ();
472445 ImmutableSet <ProtoTypeMask > protoTypeMaskSet = protoTypeMasks .build ();
473446 if (!protoTypeMaskSet .isEmpty ()) {
474447 ProtoTypeMaskTypeProvider protoTypeMaskTypeProvider =
475448 new ProtoTypeMaskTypeProvider (messageTypeProvider , protoTypeMaskSet );
449+ ImmutableSet <String > declaredNames =
450+ identDeclarationSet .stream ().map (CelVarDecl ::name ).collect (toImmutableSet ());
476451 identDeclarationSet =
477- ImmutableSet .<CelIdentDecl >builder ()
452+ ImmutableSet .<CelVarDecl >builder ()
478453 .addAll (identDeclarationSet )
479- .addAll (protoTypeMaskTypeProvider .computeDeclsFromProtoTypeMasks ())
454+ .addAll (
455+ protoTypeMaskTypeProvider .computeDeclsFromProtoTypeMasks ().stream ()
456+ .filter (decl -> !declaredNames .contains (decl .name ()))
457+ .collect (toImmutableSet ()))
480458 .build ();
481459 messageTypeProvider = protoTypeMaskTypeProvider ;
482460 }
@@ -518,7 +496,7 @@ private Builder() {
518496 private CelCheckerLegacyImpl (
519497 CelOptions celOptions ,
520498 CelContainer container ,
521- ImmutableSet <CelIdentDecl > identDeclarations ,
499+ ImmutableSet <CelVarDecl > identDeclarations ,
522500 ImmutableSet <CelFunctionDecl > functionDeclarations ,
523501 Optional <CelType > expectedResultType ,
524502 TypeProvider typeProvider ,
@@ -528,6 +506,7 @@ private CelCheckerLegacyImpl(
528506 ImmutableSet <CelCheckerLibrary > checkerLibraries ,
529507 ImmutableSet <FileDescriptor > fileDescriptors ,
530508 ImmutableSet <ProtoTypeMask > protoTypeMasks ) {
509+
531510 this .celOptions = celOptions ;
532511 this .container = container ;
533512 this .identDeclarations = identDeclarations ;
0 commit comments