1414
1515package dev .cel .checker ;
1616
17+ import dev .cel .common .types .EnumType ;
18+ import dev .cel .common .types .TypeType ;
1719import dev .cel .expr .Constant ;
1820import dev .cel .expr .Decl ;
1921import dev .cel .expr .Decl .FunctionDecl .Overload ;
2022import dev .cel .expr .Expr ;
2123import dev .cel .expr .Type ;
22- import com .google .common .annotations .VisibleForTesting ;
2324import com .google .common .base .Preconditions ;
2425import com .google .common .collect .ImmutableList ;
2526import com .google .common .collect .ImmutableMap ;
4344import dev .cel .common .types .CelKind ;
4445import dev .cel .common .types .CelProtoTypes ;
4546import dev .cel .common .types .CelType ;
47+ import dev .cel .common .types .CelTypeProvider ;
4648import dev .cel .common .types .CelTypes ;
49+ import dev .cel .common .types .ProtoMessageTypeProvider ;
4750import dev .cel .common .types .SimpleType ;
4851import dev .cel .parser .CelStandardMacro ;
4952import java .util .ArrayList ;
@@ -78,7 +81,7 @@ public class Env {
7881 CelFunctionDecl .newBuilder ().setName ("*error*" ).build ();
7982
8083 /** Type provider responsible for resolving CEL message references to strong types. */
81- private final TypeProvider typeProvider ;
84+ private final CelTypeProvider typeProvider ;
8285
8386 /**
8487 * Stack of declaration groups where each entry in stack represents a scope capable of hinding
@@ -105,7 +108,7 @@ public class Env {
105108 .build ();
106109
107110 private Env (
108- Errors errors , TypeProvider typeProvider , DeclGroup declGroup , CelOptions celOptions ) {
111+ Errors errors , CelTypeProvider typeProvider , DeclGroup declGroup , CelOptions celOptions ) {
109112 this .celOptions = celOptions ;
110113 this .errors = Preconditions .checkNotNull (errors );
111114 this .typeProvider = Preconditions .checkNotNull (typeProvider );
@@ -118,27 +121,10 @@ private Env(
118121 */
119122 @ Deprecated
120123 public static Env unconfigured (Errors errors ) {
121- return unconfigured (errors , LEGACY_TYPE_CHECKER_OPTIONS );
124+ return unconfigured (errors , new ProtoMessageTypeProvider (), LEGACY_TYPE_CHECKER_OPTIONS );
122125 }
123126
124- /**
125- * Creates an unconfigured {@code Env} value without the standard CEL types, functions, and
126- * operators with a reference to the configured {@code celOptions}.
127- */
128- @ VisibleForTesting
129- static Env unconfigured (Errors errors , CelOptions celOptions ) {
130- return unconfigured (errors , new DescriptorTypeProvider (), celOptions );
131- }
132-
133- /**
134- * Creates an unconfigured {@code Env} value without the standard CEL types, functions, and
135- * operators using a custom {@code typeProvider}.
136- *
137- * @deprecated Do not use. This exists for compatibility reasons. Migrate to CEL-Java fluent APIs.
138- * See {@code CelCompilerFactory}.
139- */
140- @ Deprecated
141- public static Env unconfigured (Errors errors , TypeProvider typeProvider , CelOptions celOptions ) {
127+ static Env unconfigured (Errors errors , CelTypeProvider typeProvider , CelOptions celOptions ) {
142128 return new Env (errors , typeProvider , new DeclGroup (), celOptions );
143129 }
144130
@@ -148,7 +134,7 @@ public static Env unconfigured(Errors errors, TypeProvider typeProvider, CelOpti
148134 */
149135 @ Deprecated
150136 public static Env standard (Errors errors ) {
151- return standard (errors , new DescriptorTypeProvider () );
137+ return standard (errors , new ProtoMessageTypeProvider (), LEGACY_TYPE_CHECKER_OPTIONS );
152138 }
153139
154140 /**
@@ -173,6 +159,11 @@ public static Env standard(Errors errors, TypeProvider typeProvider) {
173159 */
174160 @ Deprecated
175161 public static Env standard (Errors errors , TypeProvider typeProvider , CelOptions celOptions ) {
162+ CelTypeProvider adapted = new TypeProviderLegacyImpl (typeProvider );
163+ return standard (errors , adapted , celOptions );
164+ }
165+
166+ static Env standard (Errors errors , CelTypeProvider typeProvider , CelOptions celOptions ) {
176167 CelStandardDeclarations celStandardDeclaration =
177168 CelStandardDeclarations .newBuilder ()
178169 .filterFunctions (
@@ -209,10 +200,10 @@ public static Env standard(Errors errors, TypeProvider typeProvider, CelOptions
209200 return standard (celStandardDeclaration , errors , typeProvider , celOptions );
210201 }
211202
212- public static Env standard (
203+ static Env standard (
213204 CelStandardDeclarations celStandardDeclaration ,
214205 Errors errors ,
215- TypeProvider typeProvider ,
206+ CelTypeProvider typeProvider ,
216207 CelOptions celOptions ) {
217208 Env env = Env .unconfigured (errors , typeProvider , celOptions );
218209 // Isolate the standard declarations into their own scope for forward compatibility.
@@ -228,8 +219,8 @@ public Errors getErrorContext() {
228219 return errors ;
229220 }
230221
231- /** Returns the {@code TypeProvider }. */
232- public TypeProvider getTypeProvider () {
222+ /** Returns the {@code CelTypeProvider }. */
223+ public CelTypeProvider getTypeProvider () {
233224 return typeProvider ;
234225 }
235226
@@ -491,30 +482,47 @@ public Env add(String name, Type type) {
491482
492483 // Next try to import the name as a reference to a message type.
493484 // This is done via the type provider.
494- Optional <CelType > type = typeProvider .lookupCelType (cand );
485+ Optional <CelType > type = typeProvider .findType (cand );
495486 if (type .isPresent ()) {
496- decl = CelIdentDecl .newIdentDeclaration (cand , type .get ());
487+ decl = CelIdentDecl .newIdentDeclaration (cand , TypeType . create ( type .get () ));
497488 decls .get (0 ).putIdent (decl );
498489 return decl ;
499490 }
500491
501492 // Next try to import this as an enum value by splitting the name in a type prefix and
502493 // the enum inside.
503- Integer enumValue = typeProvider . lookupEnumValue (cand );
504- if (enumValue != null ) {
494+ Optional < Integer > enumValue = findEnumValue (cand );
495+ if (enumValue . isPresent () ) {
505496 decl =
506497 CelIdentDecl .newBuilder ()
507498 .setName (cand )
508499 .setType (SimpleType .INT )
509- .setConstant (CelConstant .ofValue (enumValue ))
510- .build ();
500+ .setConstant (CelConstant .ofValue (enumValue .get ())).build ();
511501
512502 decls .get (0 ).putIdent (decl );
513503 return decl ;
514504 }
505+
515506 return null ;
516507 }
517508
509+ private Optional <Integer > findEnumValue (String fullyQualifiedEnumName ) {
510+ int dot = fullyQualifiedEnumName .lastIndexOf ('.' );
511+ if (dot <= 0 ) {
512+ return Optional .empty ();
513+ }
514+
515+ String enumTypeName = fullyQualifiedEnumName .substring (0 , dot );
516+ EnumType enumType = typeProvider .findType (enumTypeName ).filter (t -> t instanceof EnumType )
517+ .map (EnumType .class ::cast ).orElse (null );
518+ if (enumType == null ) {
519+ return Optional .empty ();
520+ }
521+
522+ String enumValueName = fullyQualifiedEnumName .substring (dot + 1 );
523+ return enumType .findNumberByName (enumValueName );
524+ }
525+
518526 /**
519527 * Lookup a local identifier by name. This searches only comprehension scopes, bypassing standard
520528 * environment or user-defined environment.
0 commit comments