Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate away from deprecated CelTypes methods #525

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle/src/main/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -269,7 +269,7 @@ public CelBuilder addFunctionBindings(Iterable<CelRuntime.CelFunctionBinding> bi
@Override
public CelBuilder setResultType(CelType resultType) {
checkNotNull(resultType);
return setProtoResultType(CelTypes.celTypeToType(resultType));
return setProtoResultType(CelProtoTypes.celTypeToType(resultType));
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions checker/src/main/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions checker/src/main/java/dev/cel/checker/CelIdentDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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()));
}
Expand Down
17 changes: 9 additions & 8 deletions checker/src/main/java/dev/cel/checker/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -342,7 +343,7 @@ public Map<Long, CelType> getTypeMap() {
@Deprecated
public Type getType(Expr expr) {
Preconditions.checkNotNull(expr);
return CelTypes.celTypeToType(getType(CelExprConverter.fromExpr(expr)));
return CelProtoTypes.celTypeToType(getType(CelExprConverter.fromExpr(expr)));
}

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)));
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -848,12 +849,12 @@ public FunctionBuilder add(String id, Type resultType, Type... argTypes) {
public FunctionBuilder add(String id, Type resultType, Iterable<Type> argTypes) {
ImmutableList.Builder<CelType> 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());
Expand All @@ -873,12 +874,12 @@ public FunctionBuilder add(
String id, List<String> typeParams, Type resultType, Iterable<Type> argTypes) {
ImmutableList.Builder<CelType> 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());
Expand Down
3 changes: 2 additions & 1 deletion checker/src/main/java/dev/cel/checker/ExprChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static CheckedExpr typecheck(
Env env, String inContainer, ParsedExpr parsedExpr, Optional<Type> expectedResultType) {
Optional<CelType> type =
expectedResultType.isPresent()
? Optional.of(CelTypes.typeToCelType(expectedResultType.get()))
? Optional.of(CelProtoTypes.typeToCelType(expectedResultType.get()))
: Optional.absent();
CelAbstractSyntaxTree ast =
typecheck(
Expand Down
8 changes: 4 additions & 4 deletions checker/src/main/java/dev/cel/checker/TypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +38,7 @@ public interface TypeProvider {
/** Lookup the a {@link CelType} given a qualified {@code typeName}. Returns null if not found. */
default Optional<CelType> 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. */
Expand All @@ -61,7 +61,7 @@ default Optional<CelType> 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);
}

/**
Expand Down Expand Up @@ -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}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -65,13 +65,13 @@ public Optional<CelType> 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
Expand Down Expand Up @@ -114,7 +114,8 @@ public Optional<CelType> 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);
}
}
26 changes: 14 additions & 12 deletions checker/src/main/java/dev/cel/checker/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -493,10 +494,11 @@ private static boolean notReferencedIn(
public static Type substitute(Map<Type, Type> subs, Type type, boolean typeParamToDyn) {
ImmutableMap.Builder<CelType, CelType> subsMap = ImmutableMap.builder();
for (Map.Entry<Type, Type> 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));
}

/**
Expand Down
1 change: 0 additions & 1 deletion checker/src/test/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand Down
Loading
Loading