From 6393554d0d37f906c5b92fc87bc75f0ec8fbbb11 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Thu, 5 Dec 2024 15:08:52 -0800 Subject: [PATCH] Migrate away from deprecated CelTypes methods PiperOrigin-RevId: 703266084 --- .../src/main/java/dev/cel/bundle/BUILD.bazel | 2 +- .../src/main/java/dev/cel/bundle/CelImpl.java | 4 +-- .../src/main/java/dev/cel/checker/BUILD.bazel | 7 ++--- .../java/dev/cel/checker/CelIdentDecl.java | 4 +-- .../src/main/java/dev/cel/checker/Env.java | 17 ++++++------ .../java/dev/cel/checker/ExprChecker.java | 3 ++- .../java/dev/cel/checker/TypeProvider.java | 8 +++--- .../cel/checker/TypeProviderLegacyImpl.java | 11 ++++---- .../src/main/java/dev/cel/checker/Types.java | 26 ++++++++++--------- .../src/test/java/dev/cel/checker/BUILD.bazel | 1 - .../dev/cel/checker/CelOverloadDeclTest.java | 7 ++--- .../checker/DescriptorTypeProviderTest.java | 21 ++++++++------- .../checker/TypeProviderLegacyImplTest.java | 6 ++--- .../src/main/java/dev/cel/common/BUILD.bazel | 5 ++-- .../dev/cel/common/CelAbstractSyntaxTree.java | 8 ++++-- .../java/dev/cel/common/CelOverloadDecl.java | 10 +++---- .../common/CelProtoAbstractSyntaxTree.java | 10 ++++--- .../java/dev/cel/common/types/CelTypes.java | 6 ++--- .../src/test/java/dev/cel/common/BUILD.bazel | 2 +- .../cel/common/CelAbstractSyntaxTreeTest.java | 12 ++++----- .../CelProtoAbstractSyntaxTreeTest.java | 8 +++--- .../main/java/dev/cel/compiler/BUILD.bazel | 2 +- .../dev/cel/compiler/CelCompilerImpl.java | 6 ++--- .../src/main/java/dev/cel/testing/BUILD.bazel | 3 ++- .../dev/cel/testing/CelBaselineTestCase.java | 8 +++--- .../testing/TestCelVariableDeclWrapper.java | 4 +-- 26 files changed, 110 insertions(+), 91 deletions(-) diff --git a/bundle/src/main/java/dev/cel/bundle/BUILD.bazel b/bundle/src/main/java/dev/cel/bundle/BUILD.bazel index b6f170ee..ab7e0349 100644 --- a/bundle/src/main/java/dev/cel/bundle/BUILD.bazel +++ b/bundle/src/main/java/dev/cel/bundle/BUILD.bazel @@ -30,7 +30,7 @@ java_library( "//common:options", "//common/internal:env_visitor", "//common/internal:file_descriptor_converter", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "//common/values:cel_value_provider", "//compiler", diff --git a/bundle/src/main/java/dev/cel/bundle/CelImpl.java b/bundle/src/main/java/dev/cel/bundle/CelImpl.java index 317eeb49..616a20e8 100644 --- a/bundle/src/main/java/dev/cel/bundle/CelImpl.java +++ b/bundle/src/main/java/dev/cel/bundle/CelImpl.java @@ -39,9 +39,9 @@ import dev.cel.common.internal.EnvVisitable; import dev.cel.common.internal.EnvVisitor; import dev.cel.common.internal.FileDescriptorSetConverter; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; -import dev.cel.common.types.CelTypes; import dev.cel.common.values.CelValueProvider; import dev.cel.compiler.CelCompiler; import dev.cel.compiler.CelCompilerBuilder; @@ -269,7 +269,7 @@ public CelBuilder addFunctionBindings(Iterable bi @Override public CelBuilder setResultType(CelType resultType) { checkNotNull(resultType); - return setProtoResultType(CelTypes.celTypeToType(resultType)); + return setProtoResultType(CelProtoTypes.celTypeToType(resultType)); } @Override diff --git a/checker/src/main/java/dev/cel/checker/BUILD.bazel b/checker/src/main/java/dev/cel/checker/BUILD.bazel index 19caa139..863bca19 100644 --- a/checker/src/main/java/dev/cel/checker/BUILD.bazel +++ b/checker/src/main/java/dev/cel/checker/BUILD.bazel @@ -49,7 +49,7 @@ java_library( "//common/annotations", "//common/internal:file_descriptor_converter", "//common/types", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", @@ -135,7 +135,7 @@ java_library( "//common/annotations", "//common/ast", "//common/ast:expr_converter", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", @@ -153,7 +153,7 @@ java_library( "//:auto_value", "//common/annotations", "//common/types", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", @@ -182,6 +182,7 @@ java_library( "//common/internal:errors", "//common/internal:file_descriptor_converter", "//common/types", + "//common/types:cel_proto_types", "//common/types:cel_types", "//common/types:type_providers", "//parser:macro", diff --git a/checker/src/main/java/dev/cel/checker/CelIdentDecl.java b/checker/src/main/java/dev/cel/checker/CelIdentDecl.java index ac71523f..60f747b7 100644 --- a/checker/src/main/java/dev/cel/checker/CelIdentDecl.java +++ b/checker/src/main/java/dev/cel/checker/CelIdentDecl.java @@ -23,8 +23,8 @@ import dev.cel.common.annotations.Internal; import dev.cel.common.ast.CelConstant; import dev.cel.common.ast.CelExprConverter; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import java.util.Optional; /** @@ -57,7 +57,7 @@ public static Decl celIdentToDecl(CelIdentDecl identDecl) { IdentDecl.Builder identBuilder = IdentDecl.newBuilder() .setDoc(identDecl.doc()) - .setType(CelTypes.celTypeToType(identDecl.type())); + .setType(CelProtoTypes.celTypeToType(identDecl.type())); if (identDecl.constant().isPresent()) { identBuilder.setValue(CelExprConverter.celConstantToExprConstant(identDecl.constant().get())); } diff --git a/checker/src/main/java/dev/cel/checker/Env.java b/checker/src/main/java/dev/cel/checker/Env.java index 867a3a3c..588b98c3 100644 --- a/checker/src/main/java/dev/cel/checker/Env.java +++ b/checker/src/main/java/dev/cel/checker/Env.java @@ -40,6 +40,7 @@ import dev.cel.common.ast.CelReference; import dev.cel.common.internal.Errors; import dev.cel.common.types.CelKind; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypes; import dev.cel.common.types.SimpleType; @@ -342,7 +343,7 @@ public Map getTypeMap() { @Deprecated public Type getType(Expr expr) { Preconditions.checkNotNull(expr); - return CelTypes.celTypeToType(getType(CelExprConverter.fromExpr(expr))); + return CelProtoTypes.celTypeToType(getType(CelExprConverter.fromExpr(expr))); } /** @@ -395,7 +396,7 @@ public Env add(Decl decl) { CelIdentDecl.Builder identBuilder = CelIdentDecl.newBuilder() .setName(decl.getName()) - .setType(CelTypes.typeToCelType(decl.getIdent().getType())) + .setType(CelProtoTypes.typeToCelType(decl.getIdent().getType())) // Note: Setting doc and constant value exists for compatibility reason. This should // not be set by the users. .setDoc(decl.getIdent().getDoc()); @@ -440,7 +441,7 @@ public Env add(CelIdentDecl celIdentDecl) { @CanIgnoreReturnValue @Deprecated public Env add(String name, Type type) { - return add(CelIdentDecl.newIdentDeclaration(name, CelTypes.typeToCelType(type))); + return add(CelIdentDecl.newIdentDeclaration(name, CelProtoTypes.typeToCelType(type))); } /** @@ -766,7 +767,7 @@ public IdentBuilder(String name) { @CanIgnoreReturnValue public IdentBuilder type(Type value) { Preconditions.checkNotNull(value); - builder.setType(CelTypes.typeToCelType(Preconditions.checkNotNull(value))); + builder.setType(CelProtoTypes.typeToCelType(Preconditions.checkNotNull(value))); return this; } @@ -848,12 +849,12 @@ public FunctionBuilder add(String id, Type resultType, Type... argTypes) { public FunctionBuilder add(String id, Type resultType, Iterable argTypes) { ImmutableList.Builder argumentBuilder = new ImmutableList.Builder<>(); for (Type type : argTypes) { - argumentBuilder.add(CelTypes.typeToCelType(type)); + argumentBuilder.add(CelProtoTypes.typeToCelType(type)); } this.overloads.add( CelOverloadDecl.newBuilder() .setOverloadId(id) - .setResultType(CelTypes.typeToCelType(resultType)) + .setResultType(CelProtoTypes.typeToCelType(resultType)) .addParameterTypes(argumentBuilder.build()) .setIsInstanceFunction(isInstance) .build()); @@ -873,12 +874,12 @@ public FunctionBuilder add( String id, List typeParams, Type resultType, Iterable argTypes) { ImmutableList.Builder argumentBuilder = new ImmutableList.Builder<>(); for (Type type : argTypes) { - argumentBuilder.add(CelTypes.typeToCelType(type)); + argumentBuilder.add(CelProtoTypes.typeToCelType(type)); } this.overloads.add( CelOverloadDecl.newBuilder() .setOverloadId(id) - .setResultType(CelTypes.typeToCelType(resultType)) + .setResultType(CelProtoTypes.typeToCelType(resultType)) .addParameterTypes(argumentBuilder.build()) .setIsInstanceFunction(isInstance) .build()); diff --git a/checker/src/main/java/dev/cel/checker/ExprChecker.java b/checker/src/main/java/dev/cel/checker/ExprChecker.java index f4f485e0..c39e4d00 100644 --- a/checker/src/main/java/dev/cel/checker/ExprChecker.java +++ b/checker/src/main/java/dev/cel/checker/ExprChecker.java @@ -35,6 +35,7 @@ import dev.cel.common.ast.CelExpr; import dev.cel.common.ast.CelReference; import dev.cel.common.types.CelKind; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypes; import dev.cel.common.types.ListType; @@ -88,7 +89,7 @@ public static CheckedExpr typecheck( Env env, String inContainer, ParsedExpr parsedExpr, Optional expectedResultType) { Optional type = expectedResultType.isPresent() - ? Optional.of(CelTypes.typeToCelType(expectedResultType.get())) + ? Optional.of(CelProtoTypes.typeToCelType(expectedResultType.get())) : Optional.absent(); CelAbstractSyntaxTree ast = typecheck( diff --git a/checker/src/main/java/dev/cel/checker/TypeProvider.java b/checker/src/main/java/dev/cel/checker/TypeProvider.java index 10ce2a7d..2dd5261a 100644 --- a/checker/src/main/java/dev/cel/checker/TypeProvider.java +++ b/checker/src/main/java/dev/cel/checker/TypeProvider.java @@ -18,8 +18,8 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import java.util.Optional; import java.util.function.Function; import org.jspecify.annotations.Nullable; @@ -38,7 +38,7 @@ public interface TypeProvider { /** Lookup the a {@link CelType} given a qualified {@code typeName}. Returns null if not found. */ default Optional lookupCelType(String typeName) { Type type = lookupType(typeName); - return Optional.ofNullable(type).map(CelTypes::typeToCelType); + return Optional.ofNullable(type).map(CelProtoTypes::typeToCelType); } /** Lookup the {@code Integer} enum value given an {@code enumName}. Returns null if not found. */ @@ -61,7 +61,7 @@ default Optional lookupCelType(String typeName) { * check is supported via the ('has') macro. */ default @Nullable FieldType lookupFieldType(CelType type, String fieldName) { - return lookupFieldType(CelTypes.celTypeToType(type), fieldName); + return lookupFieldType(CelProtoTypes.celTypeToType(type), fieldName); } /** @@ -89,7 +89,7 @@ public abstract class FieldType { public abstract Type type(); public CelType celType() { - return CelTypes.typeToCelType(type()); + return CelProtoTypes.typeToCelType(type()); } /** Create a new {@code FieldType} instance from the provided {@code type}. */ diff --git a/checker/src/main/java/dev/cel/checker/TypeProviderLegacyImpl.java b/checker/src/main/java/dev/cel/checker/TypeProviderLegacyImpl.java index 4d1e0ea3..b2ac51d9 100644 --- a/checker/src/main/java/dev/cel/checker/TypeProviderLegacyImpl.java +++ b/checker/src/main/java/dev/cel/checker/TypeProviderLegacyImpl.java @@ -18,9 +18,9 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CheckReturnValue; import dev.cel.common.annotations.Internal; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; -import dev.cel.common.types.CelTypes; import dev.cel.common.types.EnumType; import dev.cel.common.types.ProtoMessageType; import dev.cel.common.types.StructType; @@ -45,7 +45,7 @@ final class TypeProviderLegacyImpl implements TypeProvider { @Override public @Nullable Type lookupType(String typeName) { - return lookupCelType(typeName).map(CelTypes::celTypeToType).orElse(null); + return lookupCelType(typeName).map(CelProtoTypes::celTypeToType).orElse(null); } @Override @@ -65,13 +65,13 @@ public Optional lookupCelType(String typeName) { return structType .findField(fieldName) - .map(f -> FieldType.of(CelTypes.celTypeToType(f.type()))) + .map(f -> FieldType.of(CelProtoTypes.celTypeToType(f.type()))) .orElse(null); } @Override public @Nullable FieldType lookupFieldType(Type type, String fieldName) { - return lookupFieldType(CelTypes.typeToCelType(type), fieldName); + return lookupFieldType(CelProtoTypes.typeToCelType(type), fieldName); } @Override @@ -114,7 +114,8 @@ public Optional lookupCelType(String typeName) { .map( et -> ExtensionFieldType.of( - CelTypes.celTypeToType(et.type()), CelTypes.celTypeToType(et.messageType()))) + CelProtoTypes.celTypeToType(et.type()), + CelProtoTypes.celTypeToType(et.messageType()))) .orElse(null); } } diff --git a/checker/src/main/java/dev/cel/checker/Types.java b/checker/src/main/java/dev/cel/checker/Types.java index 7f249fc9..62a3b66e 100644 --- a/checker/src/main/java/dev/cel/checker/Types.java +++ b/checker/src/main/java/dev/cel/checker/Types.java @@ -26,8 +26,8 @@ import com.google.protobuf.NullValue; import dev.cel.common.annotations.Internal; import dev.cel.common.types.CelKind; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import dev.cel.common.types.ListType; import dev.cel.common.types.MapType; import dev.cel.common.types.NullableType; @@ -177,7 +177,7 @@ public static Type createWrapper(Type type) { */ @Deprecated public static boolean isDynOrError(Type type) { - return isDynOrError(CelTypes.typeToCelType(type)); + return isDynOrError(CelProtoTypes.typeToCelType(type)); } /** Tests whether the type has error or dyn kind. Both have the property to match any type. */ @@ -238,18 +238,18 @@ public static CelType mostGeneral(CelType type1, CelType type2) { subs.entrySet().stream() .collect( Collectors.toMap( - k -> CelTypes.typeToCelType(k.getKey()), - v -> CelTypes.typeToCelType(v.getValue()), + k -> CelProtoTypes.typeToCelType(k.getKey()), + v -> CelProtoTypes.typeToCelType(v.getValue()), (prev, next) -> next, HashMap::new)); if (internalIsAssignable( - subsCopy, CelTypes.typeToCelType(type1), CelTypes.typeToCelType(type2))) { + subsCopy, CelProtoTypes.typeToCelType(type1), CelProtoTypes.typeToCelType(type2))) { return subsCopy.entrySet().stream() .collect( Collectors.toMap( - k -> CelTypes.celTypeToType(k.getKey()), - v -> CelTypes.celTypeToType(v.getValue()), + k -> CelProtoTypes.celTypeToType(k.getKey()), + v -> CelProtoTypes.celTypeToType(v.getValue()), (prev, next) -> next, HashMap::new)); } @@ -384,7 +384,8 @@ private static boolean isAssignableFromNull(CelType targetType) { */ @Deprecated public static boolean isEqualOrLessSpecific(Type type1, Type type2) { - return isEqualOrLessSpecific(CelTypes.typeToCelType(type1), CelTypes.typeToCelType(type2)); + return isEqualOrLessSpecific( + CelProtoTypes.typeToCelType(type1), CelProtoTypes.typeToCelType(type2)); } /** @@ -426,7 +427,7 @@ public static boolean isEqualOrLessSpecific(CelType type1, CelType type2) { TypeType typeType2 = (TypeType) type2; return isEqualOrLessSpecific(typeType1.type(), typeType2.type()); - // Message, primitive, well-known, and wrapper type names must be equal to be equivalent. + // Message, primitive, well-known, and wrapper type names must be equal to be equivalent. default: return type1.equals(type2); } @@ -493,10 +494,11 @@ private static boolean notReferencedIn( public static Type substitute(Map subs, Type type, boolean typeParamToDyn) { ImmutableMap.Builder subsMap = ImmutableMap.builder(); for (Map.Entry sub : subs.entrySet()) { - subsMap.put(CelTypes.typeToCelType(sub.getKey()), CelTypes.typeToCelType(sub.getValue())); + subsMap.put( + CelProtoTypes.typeToCelType(sub.getKey()), CelProtoTypes.typeToCelType(sub.getValue())); } - return CelTypes.celTypeToType( - substitute(subsMap.buildOrThrow(), CelTypes.typeToCelType(type), typeParamToDyn)); + return CelProtoTypes.celTypeToType( + substitute(subsMap.buildOrThrow(), CelProtoTypes.typeToCelType(type), typeParamToDyn)); } /** diff --git a/checker/src/test/java/dev/cel/checker/BUILD.bazel b/checker/src/test/java/dev/cel/checker/BUILD.bazel index 468c0af9..51daf6cb 100644 --- a/checker/src/test/java/dev/cel/checker/BUILD.bazel +++ b/checker/src/test/java/dev/cel/checker/BUILD.bazel @@ -32,7 +32,6 @@ java_library( "//common/resources/testdata/proto3:standalone_global_enum_java_proto", "//common/types", "//common/types:cel_proto_types", - "//common/types:cel_types", "//common/types:json", "//common/types:message_type_provider", "//common/types:type_providers", diff --git a/checker/src/test/java/dev/cel/checker/CelOverloadDeclTest.java b/checker/src/test/java/dev/cel/checker/CelOverloadDeclTest.java index f78a4fa6..0dd7d83d 100644 --- a/checker/src/test/java/dev/cel/checker/CelOverloadDeclTest.java +++ b/checker/src/test/java/dev/cel/checker/CelOverloadDeclTest.java @@ -22,7 +22,7 @@ import dev.cel.expr.Decl.FunctionDecl.Overload; import com.google.common.collect.ImmutableList; import dev.cel.common.CelOverloadDecl; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.SimpleType; import dev.cel.common.types.TypeParamType; import org.junit.Test; @@ -81,9 +81,10 @@ public void toProtoOverload_withTypeParams() { Overload protoOverload = CelOverloadDecl.celOverloadToOverload(celOverloadDecl); assertThat(protoOverload.getOverloadId()).isEqualTo("overloadId"); assertThat(protoOverload.getIsInstanceFunction()).isTrue(); - assertThat(protoOverload.getResultType()).isEqualTo(CelTypes.createTypeParam("A")); + assertThat(protoOverload.getResultType()).isEqualTo(CelProtoTypes.createTypeParam("A")); assertThat(protoOverload.getParamsList()) - .containsExactly(CelTypes.STRING, CelTypes.DOUBLE, CelTypes.createTypeParam("B")); + .containsExactly( + CelProtoTypes.STRING, CelProtoTypes.DOUBLE, CelProtoTypes.createTypeParam("B")); assertThat(protoOverload.getTypeParamsList()).containsExactly("A", "B"); } diff --git a/checker/src/test/java/dev/cel/checker/DescriptorTypeProviderTest.java b/checker/src/test/java/dev/cel/checker/DescriptorTypeProviderTest.java index ca5655f5..11366a68 100644 --- a/checker/src/test/java/dev/cel/checker/DescriptorTypeProviderTest.java +++ b/checker/src/test/java/dev/cel/checker/DescriptorTypeProviderTest.java @@ -21,7 +21,7 @@ import com.google.rpc.context.AttributeContext; import dev.cel.checker.TypeProvider.CombinedTypeProvider; import dev.cel.checker.TypeProvider.ExtensionFieldType; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import dev.cel.expr.conformance.proto2.TestAllTypes; import dev.cel.expr.conformance.proto2.TestAllTypesExtensions; import java.util.Arrays; @@ -36,7 +36,7 @@ public final class DescriptorTypeProviderTest { @Test public void lookupFieldNames_nonMessageType() { TypeProvider typeProvider = new DescriptorTypeProvider(); - assertThat(typeProvider.lookupFieldNames(CelTypes.STRING)).isNull(); + assertThat(typeProvider.lookupFieldNames(CelProtoTypes.STRING)).isNull(); } @Test @@ -44,19 +44,21 @@ public void lookupFieldNames_undeclaredMessageType() { TypeProvider typeProvider = new DescriptorTypeProvider(); assertThat( typeProvider.lookupFieldNames( - CelTypes.createMessage("google.rpc.context.AttributeContext"))) + CelProtoTypes.createMessage("google.rpc.context.AttributeContext"))) .isNull(); } @Test public void lookupFieldNames_groupTypeField() throws Exception { - Type proto2MessageType = CelTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"); + Type proto2MessageType = + CelProtoTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"); TypeProvider typeProvider = new DescriptorTypeProvider( ImmutableList.of( TestAllTypes.getDescriptor().getFile(), TestAllTypesExtensions.getDescriptor())); assertThat(typeProvider.lookupFieldType(proto2MessageType, "nestedgroup").type()) - .isEqualTo(CelTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes.NestedGroup")); + .isEqualTo( + CelProtoTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes.NestedGroup")); } @Test @@ -99,7 +101,8 @@ public void lookupExtensionType_combinedProvider() { makePartialTypeProvider(configuredProvider); final TypeProvider typeProvider = new CombinedTypeProvider(ImmutableList.of(partialProvider, configuredProvider)); - final Type messageType = CelTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"); + final Type messageType = + CelProtoTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"); assertThat(typeProvider.lookupExtensionType("non.existent")).isNull(); @@ -112,7 +115,7 @@ public void lookupExtensionType_combinedProvider() { ExtensionFieldType int32Ext = typeProvider.lookupExtensionType("cel.expr.conformance.proto2.int32_ext"); assertThat(int32Ext).isNotNull(); - assertThat(int32Ext.fieldType().type()).isEqualTo(CelTypes.INT64); + assertThat(int32Ext.fieldType().type()).isEqualTo(CelProtoTypes.INT64); assertThat(int32Ext.messageType()).isEqualTo(messageType); ExtensionFieldType repeatedExt = @@ -120,8 +123,8 @@ public void lookupExtensionType_combinedProvider() { assertThat(repeatedExt).isNotNull(); assertThat(repeatedExt.fieldType().type()) .isEqualTo( - CelTypes.createList( - CelTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"))); + CelProtoTypes.createList( + CelProtoTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes"))); assertThat(repeatedExt.messageType()).isEqualTo(messageType); // With leading dot '.'. diff --git a/checker/src/test/java/dev/cel/checker/TypeProviderLegacyImplTest.java b/checker/src/test/java/dev/cel/checker/TypeProviderLegacyImplTest.java index 71587ce4..4569877c 100644 --- a/checker/src/test/java/dev/cel/checker/TypeProviderLegacyImplTest.java +++ b/checker/src/test/java/dev/cel/checker/TypeProviderLegacyImplTest.java @@ -21,7 +21,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.protobuf.Descriptors.Descriptor; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.ProtoMessageTypeProvider; import dev.cel.expr.conformance.proto2.Proto2ExtensionScopedMessage; import dev.cel.expr.conformance.proto2.TestAllTypes; @@ -85,8 +85,8 @@ public void lookupExtension() { TypeProvider.ExtensionFieldType extensionType = compatTypeProvider.lookupExtensionType("cel.expr.conformance.proto2.nested_enum_ext"); assertThat(extensionType.messageType()) - .isEqualTo(CelTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes")); - assertThat(extensionType.fieldType().type()).isEqualTo(CelTypes.INT64); + .isEqualTo(CelProtoTypes.createMessage("cel.expr.conformance.proto2.TestAllTypes")); + assertThat(extensionType.fieldType().type()).isEqualTo(CelProtoTypes.INT64); assertThat(extensionType) .isEqualTo( descriptorTypeProvider.lookupExtensionType( diff --git a/common/src/main/java/dev/cel/common/BUILD.bazel b/common/src/main/java/dev/cel/common/BUILD.bazel index 9e4fa12f..53abac0a 100644 --- a/common/src/main/java/dev/cel/common/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/BUILD.bazel @@ -58,6 +58,7 @@ java_library( "//common/internal", "//common/internal:file_descriptor_converter", "//common/types", + "//common/types:cel_proto_types", "//common/types:cel_types", "//common/types:type_providers", "@cel_spec//proto/cel/expr:expr_java_proto", @@ -79,7 +80,7 @@ java_library( "//:auto_value", "//common/annotations", "//common/internal:safe_string_formatter", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", @@ -119,7 +120,7 @@ java_library( deps = [ "//common", "//common/ast:expr_converter", - "//common/types:cel_types", + "//common/types:cel_proto_types", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", diff --git a/common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java b/common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java index 59023adf..426ce76e 100644 --- a/common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java +++ b/common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java @@ -23,8 +23,8 @@ import dev.cel.common.ast.CelConstant; import dev.cel.common.ast.CelExpr; import dev.cel.common.ast.CelReference; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import dev.cel.common.types.SimpleType; import java.util.Map; import java.util.NoSuchElementException; @@ -97,9 +97,13 @@ public CelType getResultType() { /** * For a type checked abstract syntax tree the resulting type is returned in proto format * described in checked.proto. Otherwise, the dynamic type is returned. + * + * @deprecated Use {@link #getResultType} instead. Convert the resulting {@link CelType} into + * canonical CEL expr proto format via {@link CelProtoTypes#celTypeToType} if necessary. */ + @Deprecated public Type getProtoResultType() { - return CelTypes.celTypeToType(getResultType()); + return CelProtoTypes.celTypeToType(getResultType()); } /** diff --git a/common/src/main/java/dev/cel/common/CelOverloadDecl.java b/common/src/main/java/dev/cel/common/CelOverloadDecl.java index 247f86bc..1d9c61e9 100644 --- a/common/src/main/java/dev/cel/common/CelOverloadDecl.java +++ b/common/src/main/java/dev/cel/common/CelOverloadDecl.java @@ -25,8 +25,8 @@ import com.google.errorprone.annotations.CheckReturnValue; import com.google.errorprone.annotations.Immutable; import dev.cel.common.types.CelKind; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import java.util.Arrays; import java.util.List; @@ -230,10 +230,10 @@ public static Overload celOverloadToOverload(CelOverloadDecl overload) { return Overload.newBuilder() .setIsInstanceFunction(overload.isInstanceFunction()) .setOverloadId(overload.overloadId()) - .setResultType(CelTypes.celTypeToType(overload.resultType())) + .setResultType(CelProtoTypes.celTypeToType(overload.resultType())) .addAllParams( overload.parameterTypes().stream() - .map(CelTypes::celTypeToType) + .map(CelProtoTypes::celTypeToType) .collect(toImmutableList())) .addAllTypeParams(overload.typeParameterNames()) .setDoc(overload.doc()) @@ -244,11 +244,11 @@ public static CelOverloadDecl overloadToCelOverload(Overload overload) { return CelOverloadDecl.newBuilder() .setIsInstanceFunction(overload.getIsInstanceFunction()) .setOverloadId(overload.getOverloadId()) - .setResultType(CelTypes.typeToCelType(overload.getResultType())) + .setResultType(CelProtoTypes.typeToCelType(overload.getResultType())) .setDoc(overload.getDoc()) .addParameterTypes( overload.getParamsList().stream() - .map(CelTypes::typeToCelType) + .map(CelProtoTypes::typeToCelType) .collect(toImmutableList())) .build(); } diff --git a/common/src/main/java/dev/cel/common/CelProtoAbstractSyntaxTree.java b/common/src/main/java/dev/cel/common/CelProtoAbstractSyntaxTree.java index f8374e11..7230c80a 100644 --- a/common/src/main/java/dev/cel/common/CelProtoAbstractSyntaxTree.java +++ b/common/src/main/java/dev/cel/common/CelProtoAbstractSyntaxTree.java @@ -29,7 +29,7 @@ import com.google.common.collect.ImmutableList; import com.google.errorprone.annotations.CheckReturnValue; import dev.cel.common.ast.CelExprConverter; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import java.util.Collection; import java.util.Map.Entry; @@ -66,7 +66,8 @@ private CelProtoAbstractSyntaxTree(CheckedExpr checkedExpr) { Entry::getKey, v -> CelExprConverter.exprReferenceToCelReference(v.getValue()))), checkedExpr.getTypeMapMap().entrySet().stream() - .collect(toImmutableMap(Entry::getKey, v -> CelTypes.typeToCelType(v.getValue())))); + .collect( + toImmutableMap(Entry::getKey, v -> CelProtoTypes.typeToCelType(v.getValue())))); } private CelProtoAbstractSyntaxTree(CelAbstractSyntaxTree ast) { @@ -98,7 +99,8 @@ private CelProtoAbstractSyntaxTree(CelAbstractSyntaxTree ast) { v -> CelExprConverter.celReferenceToExprReference(v.getValue())))); checkedExprBuilder.putAllTypeMap( ast.getTypeMap().entrySet().stream() - .collect(toImmutableMap(Entry::getKey, v -> CelTypes.celTypeToType(v.getValue())))); + .collect( + toImmutableMap(Entry::getKey, v -> CelProtoTypes.celTypeToType(v.getValue())))); } this.checkedExpr = checkedExprBuilder.build(); @@ -182,7 +184,7 @@ public ParsedExpr toParsedExpr() { */ @CheckReturnValue public Type getProtoResultType() { - return CelTypes.celTypeToType(ast.getResultType()); + return CelProtoTypes.celTypeToType(ast.getResultType()); } private static ImmutableList fromCelExtensionsToExprExtensions( diff --git a/common/src/main/java/dev/cel/common/types/CelTypes.java b/common/src/main/java/dev/cel/common/types/CelTypes.java index f4d220bc..860b6dd1 100644 --- a/common/src/main/java/dev/cel/common/types/CelTypes.java +++ b/common/src/main/java/dev/cel/common/types/CelTypes.java @@ -303,7 +303,7 @@ public static OpaqueType createFunctionType(CelType resultType, IterableThis method can also format global functions. See the {@link #formatFunction} methods for * richer control over function formatting. * - * @deprecated Use {@link #format(CelType)} instead. + * @deprecated Use {@link CelProtoTypes#format(Type)} instead. */ @Deprecated public static String format(Type type) { @@ -458,7 +458,7 @@ public static Type celTypeToType(CelType celType) { if (celType.name().equals("function")) { Type.FunctionType.Builder functionBuilder = Type.FunctionType.newBuilder(); if (!celType.parameters().isEmpty()) { - functionBuilder.setResultType(CelTypes.celTypeToType(celType.parameters().get(0))); + functionBuilder.setResultType(celTypeToType(celType.parameters().get(0))); functionBuilder.addAllArgTypes( celType.parameters().stream() .skip(1) @@ -538,7 +538,7 @@ public static CelType typeToCelType(Type type) { case FUNCTION: Type.FunctionType functionType = type.getFunction(); return CelTypes.createFunctionType( - CelTypes.typeToCelType(functionType.getResultType()), + typeToCelType(functionType.getResultType()), functionType.getArgTypesList().stream() .map(CelTypes::typeToCelType) .collect(toImmutableList())); diff --git a/common/src/test/java/dev/cel/common/BUILD.bazel b/common/src/test/java/dev/cel/common/BUILD.bazel index bac8affe..e0440d76 100644 --- a/common/src/test/java/dev/cel/common/BUILD.bazel +++ b/common/src/test/java/dev/cel/common/BUILD.bazel @@ -22,7 +22,7 @@ java_library( "//common/ast", "//common/internal", "//common/types", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:cel_v1alpha1_types", "//compiler", "//compiler:compiler_builder", diff --git a/common/src/test/java/dev/cel/common/CelAbstractSyntaxTreeTest.java b/common/src/test/java/dev/cel/common/CelAbstractSyntaxTreeTest.java index 2cdf8138..2e1ae22e 100644 --- a/common/src/test/java/dev/cel/common/CelAbstractSyntaxTreeTest.java +++ b/common/src/test/java/dev/cel/common/CelAbstractSyntaxTreeTest.java @@ -29,7 +29,7 @@ import com.google.common.testing.EqualsTester; import dev.cel.common.ast.CelConstant; import dev.cel.common.ast.CelExpr; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.SimpleType; import dev.cel.compiler.CelCompiler; import dev.cel.compiler.CelCompilerFactory; @@ -58,9 +58,9 @@ public final class CelAbstractSyntaxTreeTest { private static final CelAbstractSyntaxTree CHECKED_ENUM_AST = CelProtoAbstractSyntaxTree.fromCheckedExpr( CheckedExpr.newBuilder() - .putTypeMap(1L, CelTypes.INT64) - .putTypeMap(2L, CelTypes.INT64) - .putTypeMap(3L, CelTypes.BOOL) + .putTypeMap(1L, CelProtoTypes.INT64) + .putTypeMap(2L, CelProtoTypes.INT64) + .putTypeMap(3L, CelProtoTypes.BOOL) .putReferenceMap( 2L, Reference.newBuilder() @@ -95,7 +95,7 @@ public final class CelAbstractSyntaxTreeTest { public void getResultType_isDynWhenParsedExpr() { CelAbstractSyntaxTree ast = PARSED_AST; - assertThat(ast.getProtoResultType()).isEqualTo(CelTypes.DYN); + assertThat(ast.getProtoResultType()).isEqualTo(CelProtoTypes.DYN); assertThat(ast.getResultType()).isEqualTo(SimpleType.DYN); } @@ -103,7 +103,7 @@ public void getResultType_isDynWhenParsedExpr() { public void getResultType_isStaticWhenCheckedExpr() { CelAbstractSyntaxTree ast = CHECKED_AST; - assertThat(ast.getProtoResultType()).isEqualTo(CelTypes.BOOL); + assertThat(ast.getProtoResultType()).isEqualTo(CelProtoTypes.BOOL); assertThat(ast.getResultType()).isEqualTo(SimpleType.BOOL); } diff --git a/common/src/test/java/dev/cel/common/CelProtoAbstractSyntaxTreeTest.java b/common/src/test/java/dev/cel/common/CelProtoAbstractSyntaxTreeTest.java index 1d6170f5..58582854 100644 --- a/common/src/test/java/dev/cel/common/CelProtoAbstractSyntaxTreeTest.java +++ b/common/src/test/java/dev/cel/common/CelProtoAbstractSyntaxTreeTest.java @@ -28,7 +28,7 @@ import dev.cel.expr.SourceInfo.Extension; import dev.cel.expr.SourceInfo.Extension.Component; import dev.cel.expr.SourceInfo.Extension.Version; -import dev.cel.common.types.CelTypes; +import dev.cel.common.types.CelProtoTypes; import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; @@ -79,7 +79,7 @@ public class CelProtoAbstractSyntaxTreeTest { private static final CheckedExpr CHECKED_EXPR = CheckedExpr.newBuilder() - .putTypeMap(1L, CelTypes.BOOL) + .putTypeMap(1L, CelProtoTypes.BOOL) .putReferenceMap(1L, Reference.newBuilder().addOverloadId("not_equals").build()) .setSourceInfo(SOURCE_INFO) .setExpr(EXPR) @@ -114,13 +114,13 @@ public void getSourceInfo_yieldsEquivalentMessage() { @Test public void getProtoResultType_isDynWhenParsedExpr() { CelProtoAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree.fromParsedExpr(PARSED_EXPR); - assertThat(ast.getProtoResultType()).isEqualTo(CelTypes.DYN); + assertThat(ast.getProtoResultType()).isEqualTo(CelProtoTypes.DYN); } @Test public void getProtoResultType_isStaticWhenCheckedExpr() { CelProtoAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree.fromCheckedExpr(CHECKED_EXPR); - assertThat(ast.getProtoResultType()).isEqualTo(CelTypes.BOOL); + assertThat(ast.getProtoResultType()).isEqualTo(CelProtoTypes.BOOL); } @Test diff --git a/compiler/src/main/java/dev/cel/compiler/BUILD.bazel b/compiler/src/main/java/dev/cel/compiler/BUILD.bazel index 0756c5a6..784e83b4 100644 --- a/compiler/src/main/java/dev/cel/compiler/BUILD.bazel +++ b/compiler/src/main/java/dev/cel/compiler/BUILD.bazel @@ -40,7 +40,7 @@ java_library( "//common:options", "//common/annotations", "//common/internal:env_visitor", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "//parser", "//parser:macro", diff --git a/compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java b/compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java index ae65db80..83b89dfb 100644 --- a/compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java +++ b/compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java @@ -37,9 +37,9 @@ import dev.cel.common.annotations.Internal; import dev.cel.common.internal.EnvVisitable; import dev.cel.common.internal.EnvVisitor; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; -import dev.cel.common.types.CelTypes; import dev.cel.parser.CelMacro; import dev.cel.parser.CelParser; import dev.cel.parser.CelParserBuilder; @@ -162,7 +162,7 @@ public CelCompilerBuilder setContainer(String container) { @Override public CelCompilerBuilder addVar(String name, Type type) { - return addVar(name, CelTypes.typeToCelType(type)); + return addVar(name, CelProtoTypes.typeToCelType(type)); } @Override @@ -221,7 +221,7 @@ public CelCompilerBuilder addProtoTypeMasks(Iterable typeMasks) { @Override public CelCompilerBuilder setResultType(CelType resultType) { checkNotNull(resultType); - return setProtoResultType(CelTypes.celTypeToType(resultType)); + return setProtoResultType(CelProtoTypes.celTypeToType(resultType)); } @Override diff --git a/testing/src/main/java/dev/cel/testing/BUILD.bazel b/testing/src/main/java/dev/cel/testing/BUILD.bazel index 1b2d9696..ec2bcc93 100644 --- a/testing/src/main/java/dev/cel/testing/BUILD.bazel +++ b/testing/src/main/java/dev/cel/testing/BUILD.bazel @@ -53,7 +53,7 @@ java_library( srcs = TEST_DECL_SOURCES, deps = [ "//common:compiler_common", - "//common/types:cel_types", + "//common/types:cel_proto_types", "//common/types:type_providers", "//compiler:compiler_builder", "@cel_spec//proto/cel/expr:expr_java_proto", @@ -71,6 +71,7 @@ java_library( "//common", "//common:compiler_common", "//common:options", + "//common/types:cel_proto_types", "//common/types:cel_types", "//common/types:message_type_provider", "//common/types:type_providers", diff --git a/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java b/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java index ca48de46..630cb26b 100644 --- a/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java +++ b/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java @@ -28,6 +28,7 @@ import dev.cel.common.CelOptions; import dev.cel.common.CelOverloadDecl; import dev.cel.common.CelValidationException; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; import dev.cel.common.types.CelTypes; @@ -208,7 +209,8 @@ protected String formatDecl(String name, List declarations) { private void formatDeclImpl(Decl decl, StringBuilder declStr) { switch (decl.getDeclKindCase()) { case IDENT: - declStr.append(String.format(" value %s%n", CelTypes.format(decl.getIdent().getType()))); + declStr.append( + String.format(" value %s%n", CelProtoTypes.format(decl.getIdent().getType()))); break; case FUNCTION: for (Overload overload : decl.getFunction().getOverloadsList()) { @@ -217,9 +219,9 @@ private void formatDeclImpl(Decl decl, StringBuilder declStr) { " function %s %s%n", overload.getOverloadId(), CelTypes.formatFunction( - CelTypes.typeToCelType(overload.getResultType()), + CelProtoTypes.typeToCelType(overload.getResultType()), overload.getParamsList().stream() - .map(CelTypes::typeToCelType) + .map(CelProtoTypes::typeToCelType) .collect(toImmutableList()), overload.getIsInstanceFunction(), /* typeParamToDyn= */ false))); diff --git a/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java b/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java index 20c2c484..2cb94f87 100644 --- a/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java +++ b/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java @@ -17,8 +17,8 @@ import dev.cel.expr.Decl; import dev.cel.expr.Decl.IdentDecl; import dev.cel.expr.Type; +import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; -import dev.cel.common.types.CelTypes; import dev.cel.compiler.CelCompilerBuilder; /** Wrapper for CEL native type based variable declarations */ @@ -33,7 +33,7 @@ class TestCelVariableDeclWrapper extends TestDecl { @Override void loadDeclsToCompiler(CelCompilerBuilder compiler) { - CelType celType = CelTypes.typeToCelType(type); + CelType celType = CelProtoTypes.typeToCelType(type); compiler.addVar(name, celType); }