diff --git a/common/src/main/java/dev/cel/common/internal/BUILD.bazel b/common/src/main/java/dev/cel/common/internal/BUILD.bazel index 6b83b441..bb9d67bf 100644 --- a/common/src/main/java/dev/cel/common/internal/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/internal/BUILD.bazel @@ -126,7 +126,6 @@ java_library( "//common:proto_json_adapter", "//common:runtime_exception", "//common/annotations", - "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_code_findbugs_annotations", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", diff --git a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel index 093d8d86..0ec16554 100644 --- a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel @@ -18,8 +18,6 @@ BASE_SOURCES = [ "Registrar.java", "ResolvedOverload.java", "StandardFunctions.java", - "StandardTypeResolver.java", - "TypeResolver.java", ] # keep sorted @@ -70,8 +68,6 @@ java_library( "//common/internal:default_message_factory", "//common/internal:dynamic_proto", "//common/internal:safe_string_formatter", - "//common/types", - "//common/types:type_providers", "//runtime:runtime_helper", "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_code_findbugs_annotations", @@ -111,7 +107,6 @@ java_library( "//common/types", "//common/types:cel_types", "//common/types:type_providers", - "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_code_findbugs_annotations", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", @@ -253,17 +248,13 @@ java_library( "//common/annotations", "//common/internal:cel_descriptor_pools", "//common/internal:dynamic_proto", - "//common/types", - "//common/types:type_providers", "//common/values", "//common/values:cel_value", "//common/values:cel_value_provider", "//common/values:proto_message_value", "//runtime:interpreter", - "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", - "@maven//:org_jspecify_jspecify", ], ) @@ -276,7 +267,6 @@ java_library( ":base", ":unknown_attributes", "//common/annotations", - "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:org_jspecify_jspecify", ], diff --git a/runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java b/runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java index f4fbbfe2..660fd75c 100644 --- a/runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java +++ b/runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java @@ -14,8 +14,6 @@ package dev.cel.runtime; -import dev.cel.expr.Type; -import dev.cel.expr.Value; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.Immutable; import com.google.protobuf.Descriptors.Descriptor; @@ -31,7 +29,6 @@ import dev.cel.common.internal.DynamicProto; import dev.cel.common.internal.ProtoAdapter; import dev.cel.common.internal.ProtoMessageFactory; -import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypes; import java.util.Map; import java.util.Optional; @@ -50,7 +47,6 @@ @Internal public final class DescriptorMessageProvider implements RuntimeTypeProvider { private final ProtoMessageFactory protoMessageFactory; - private final TypeResolver typeResolver; @SuppressWarnings("Immutable") private final ProtoAdapter protoAdapter; @@ -82,33 +78,12 @@ public DescriptorMessageProvider( * when adapting from proto to CEL and vice versa. */ public DescriptorMessageProvider(ProtoMessageFactory protoMessageFactory, CelOptions celOptions) { - this.typeResolver = StandardTypeResolver.getInstance(celOptions); this.protoMessageFactory = protoMessageFactory; this.protoAdapter = new ProtoAdapter( DynamicProto.create(protoMessageFactory), celOptions.enableUnsignedLongs()); } - @Override - @Nullable - public Value resolveObjectType(Object obj, @Nullable Value checkedTypeValue) { - return typeResolver.resolveObjectType(obj, checkedTypeValue); - } - - /** {@inheritDoc} */ - @Override - public Value adaptType(CelType type) { - return typeResolver.adaptType(type); - } - - @Nullable - @Override - @Deprecated - /** {@inheritDoc} */ - public Value adaptType(@Nullable Type type) { - return typeResolver.adaptType(type); - } - @Nullable @Override public Object createMessage(String messageName, Map values) { diff --git a/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProvider.java b/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProvider.java index 7df4e26a..97e4f3af 100644 --- a/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProvider.java +++ b/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProvider.java @@ -25,4 +25,4 @@ */ @Immutable @Internal -public interface RuntimeTypeProvider extends MessageProvider, TypeResolver {} +public interface RuntimeTypeProvider extends MessageProvider {} diff --git a/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProviderLegacyImpl.java b/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProviderLegacyImpl.java index dcb2b4ec..36bd054f 100644 --- a/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProviderLegacyImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/RuntimeTypeProviderLegacyImpl.java @@ -14,10 +14,7 @@ package dev.cel.runtime; -import dev.cel.expr.Type; -import dev.cel.expr.Value; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; import com.google.errorprone.annotations.Immutable; import dev.cel.common.CelErrorCode; import dev.cel.common.CelOptions; @@ -25,8 +22,6 @@ import dev.cel.common.annotations.Internal; import dev.cel.common.internal.CelDescriptorPool; import dev.cel.common.internal.DynamicProto; -import dev.cel.common.types.CelType; -import dev.cel.common.types.TypeType; import dev.cel.common.values.CelValue; import dev.cel.common.values.CelValueProvider; import dev.cel.common.values.ProtoCelValueConverter; @@ -34,7 +29,6 @@ import dev.cel.common.values.StringValue; import java.util.Map; import java.util.NoSuchElementException; -import org.jspecify.annotations.Nullable; /** Bridge between the old RuntimeTypeProvider and CelValueProvider APIs. */ @Internal @@ -43,7 +37,6 @@ public final class RuntimeTypeProviderLegacyImpl implements RuntimeTypeProvider private final CelValueProvider valueProvider; private final ProtoCelValueConverter protoCelValueConverter; - private final TypeResolver standardTypeResolver; @VisibleForTesting public RuntimeTypeProviderLegacyImpl( @@ -54,7 +47,6 @@ public RuntimeTypeProviderLegacyImpl( this.valueProvider = valueProvider; this.protoCelValueConverter = ProtoCelValueConverter.newInstance(celOptions, celDescriptorPool, dynamicProto); - this.standardTypeResolver = StandardTypeResolver.getInstance(celOptions); } @Override @@ -115,28 +107,6 @@ public Object adapt(Object message) { return unwrapCelValue(protoCelValueConverter.fromJavaObjectToCelValue(message)); } - @Override - public Value resolveObjectType(Object obj, Value checkedTypeValue) { - // Presently, Java only supports evaluation of checked-only expressions. - Preconditions.checkNotNull(checkedTypeValue); - return standardTypeResolver.resolveObjectType(obj, checkedTypeValue); - } - - @Override - public Value adaptType(CelType type) { - Preconditions.checkNotNull(type); - if (type instanceof TypeType) { - return createTypeValue(((TypeType) type).containingTypeName()); - } - - return createTypeValue(type.name()); - } - - @Override - public @Nullable Value adaptType(@Nullable Type type) { - throw new UnsupportedOperationException("This should only be called with native CelType."); - } - /** * DefaultInterpreter cannot handle CelValue and instead expects plain Java objects. * @@ -145,8 +115,4 @@ public Value adaptType(CelType type) { private Object unwrapCelValue(CelValue object) { return protoCelValueConverter.fromCelValueToJavaObject(object); } - - private static Value createTypeValue(String name) { - return Value.newBuilder().setTypeValue(name).build(); - } } diff --git a/runtime/src/main/java/dev/cel/runtime/StandardTypeResolver.java b/runtime/src/main/java/dev/cel/runtime/StandardTypeResolver.java deleted file mode 100644 index 88d00265..00000000 --- a/runtime/src/main/java/dev/cel/runtime/StandardTypeResolver.java +++ /dev/null @@ -1,242 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.runtime; - -import static com.google.common.base.Preconditions.checkNotNull; - -import dev.cel.expr.Type; -import dev.cel.expr.Type.PrimitiveType; -import dev.cel.expr.Type.TypeKindCase; -import dev.cel.expr.Value; -import dev.cel.expr.Value.KindCase; -import com.google.common.collect.ImmutableMap; -import com.google.common.primitives.UnsignedLong; -import com.google.errorprone.annotations.Immutable; -import com.google.protobuf.ByteString; -import com.google.protobuf.MessageOrBuilder; -import com.google.protobuf.NullValue; -import dev.cel.common.CelOptions; -import dev.cel.common.annotations.Internal; -import dev.cel.common.types.CelKind; -import dev.cel.common.types.CelType; -import dev.cel.common.types.TypeType; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; -import org.jspecify.annotations.Nullable; - -/** - * The {@code StandardTypeResolver} implements the {@link TypeResolver} and resolves types supported - * by the CEL standard environment. - * - *

CEL Library Internals. Do Not Use. - * - * @deprecated Use {@code CelTypeResolver} instead. - */ -@Immutable -@Internal -@Deprecated -public final class StandardTypeResolver implements TypeResolver { - - /** - * Obtain a singleton instance of the {@link StandardTypeResolver} appropriate for the {@code - * celOptions} provided. - */ - public static TypeResolver getInstance(CelOptions celOptions) { - return celOptions.enableUnsignedLongs() ? INSTANCE_WITH_UNSIGNED_LONGS : INSTANCE; - } - - private static final TypeResolver INSTANCE = - new StandardTypeResolver(commonTypes(/* unsignedLongs= */ false)); - - private static final TypeResolver INSTANCE_WITH_UNSIGNED_LONGS = - new StandardTypeResolver(commonTypes(/* unsignedLongs= */ true)); - - // Type of type which is modelled as a value instance rather than as a Java POJO. - private static final Value TYPE_VALUE = createType("type"); - - // Built-in types. - private static ImmutableMap> commonTypes(boolean unsignedLongs) { - return ImmutableMap.>builder() - .put(createType("bool"), Boolean.class) - .put(createType("bytes"), ByteString.class) - .put(createType("double"), Double.class) - .put(createType("int"), Long.class) - .put(createType("uint"), unsignedLongs ? UnsignedLong.class : Long.class) - .put(createType("string"), String.class) - .put(createType("null_type"), NullValue.class) - // Aggregate types. - .put(createType("list"), Collection.class) - .put(createType("map"), Map.class) - // Optional type - .put(createType("optional_type"), Optional.class) - .buildOrThrow(); - } - - private final ImmutableMap> types; - - private StandardTypeResolver(ImmutableMap> types) { - this.types = types; - } - - @Nullable - @Override - public Value resolveObjectType(Object obj, @Nullable Value checkedTypeValue) { - if (checkedTypeValue != null && (obj instanceof Long || obj instanceof NullValue)) { - return checkedTypeValue; - } - return resolveObjectType(obj); - } - - @Nullable - private Value resolveObjectType(Object obj) { - for (Value type : types.keySet()) { - Class impl = types.get(type); - // Generally, the type will be an instance of a class. - if (impl.isInstance(obj)) { - return type; - } - } - // In the case 'type' values, the obj will be a api.expr.Value. - if (obj instanceof Value) { - Value objVal = (Value) obj; - if (objVal.getKindCase() == KindCase.TYPE_VALUE) { - return TYPE_VALUE; - } - } - // Otherwise, this is a protobuf type. - if (obj instanceof MessageOrBuilder) { - MessageOrBuilder msg = (MessageOrBuilder) obj; - return createType(msg.getDescriptorForType().getFullName()); - } - return null; - } - - /** {@inheritDoc} */ - @Override - public @Nullable Value adaptType(CelType type) { - checkNotNull(type); - // TODO: Add enum type support here. - Value.Builder typeValue = Value.newBuilder(); - switch (type.kind()) { - case OPAQUE: - case STRUCT: - return typeValue.setTypeValue(type.name()).build(); - case LIST: - return typeValue.setTypeValue("list").build(); - case MAP: - return typeValue.setTypeValue("map").build(); - case TYPE: - CelType typeOfType = ((TypeType) type).type(); - if (typeOfType.kind() == CelKind.DYN) { - return typeValue.setTypeValue("type").build(); - } - return adaptType(typeOfType); - case NULL_TYPE: - return typeValue.setTypeValue("null_type").build(); - case DURATION: - return typeValue.setTypeValue("google.protobuf.Duration").build(); - case TIMESTAMP: - return typeValue.setTypeValue("google.protobuf.Timestamp").build(); - case BOOL: - return typeValue.setTypeValue("bool").build(); - case BYTES: - return typeValue.setTypeValue("bytes").build(); - case DOUBLE: - return typeValue.setTypeValue("double").build(); - case INT: - return typeValue.setTypeValue("int").build(); - case STRING: - return typeValue.setTypeValue("string").build(); - case UINT: - return typeValue.setTypeValue("uint").build(); - default: - break; - } - return null; - } - - /** {@inheritDoc} */ - @Override - @Deprecated - public @Nullable Value adaptType(@Nullable Type type) { - if (type == null) { - return null; - } - // TODO: Add enum type support here. - Value.Builder typeValue = Value.newBuilder(); - switch (type.getTypeKindCase()) { - case ABSTRACT_TYPE: - return typeValue.setTypeValue(type.getAbstractType().getName()).build(); - case MESSAGE_TYPE: - return typeValue.setTypeValue(type.getMessageType()).build(); - case LIST_TYPE: - return typeValue.setTypeValue("list").build(); - case MAP_TYPE: - return typeValue.setTypeValue("map").build(); - case TYPE: - Type typeOfType = type.getType(); - if (typeOfType.getTypeKindCase() == TypeKindCase.DYN) { - return typeValue.setTypeValue("type").build(); - } - return adaptType(typeOfType); - case NULL: - return typeValue.setTypeValue("null_type").build(); - case PRIMITIVE: - return adaptPrimitive(type.getPrimitive()); - case WRAPPER: - return adaptPrimitive(type.getWrapper()); - case WELL_KNOWN: - switch (type.getWellKnown()) { - case DURATION: - return typeValue.setTypeValue("google.protobuf.Duration").build(); - case TIMESTAMP: - return typeValue.setTypeValue("google.protobuf.Timestamp").build(); - default: - break; - } - break; - default: - break; - } - return null; - } - - @Nullable - private static Value adaptPrimitive(PrimitiveType primitiveType) { - Value.Builder typeValue = Value.newBuilder(); - switch (primitiveType) { - case BOOL: - return typeValue.setTypeValue("bool").build(); - case BYTES: - return typeValue.setTypeValue("bytes").build(); - case DOUBLE: - return typeValue.setTypeValue("double").build(); - case INT64: - return typeValue.setTypeValue("int").build(); - case STRING: - return typeValue.setTypeValue("string").build(); - case UINT64: - return typeValue.setTypeValue("uint").build(); - default: - break; - } - return null; - } - - private static Value createType(String name) { - return Value.newBuilder().setTypeValue(name).build(); - } -} diff --git a/runtime/src/main/java/dev/cel/runtime/TypeResolver.java b/runtime/src/main/java/dev/cel/runtime/TypeResolver.java deleted file mode 100644 index 4bfa9ae7..00000000 --- a/runtime/src/main/java/dev/cel/runtime/TypeResolver.java +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.runtime; - -import dev.cel.expr.Type; -import dev.cel.expr.Value; -import com.google.errorprone.annotations.Immutable; -import dev.cel.common.annotations.Internal; -import dev.cel.common.types.CelType; -import org.jspecify.annotations.Nullable; - -/** - * The {@code TypeResolver} determines the CEL type of Java-native values and assists with adapting - * check-time types to runtime values. - * - *

CEL Library Internals. Do Not Use. - */ -@Immutable -@Internal -public interface TypeResolver { - - /** - * Resolve the CEL type of the {@code obj}, using the {@code checkedTypeValue} as hint for type - * disambiguation. - * - *

The {@code checkedTypeValue} indicates the statically determined type of the object at - * check-time. Often, the check-time and runtime phases agree, but there are cases where the - * runtime type is ambiguous, as is the case when a {@code Long} value is supplied as this could - * either be an int, uint, or enum type. - * - *

Type resolution is biased toward the runtime value type, given the dynamically typed nature - * of CEL. - */ - @Nullable Value resolveObjectType(Object obj, @Nullable Value checkedTypeValue); - - /** - * Adapt the check-time {@code type} instance to a runtime {@code Value}. - * - *

When the checked {@code type} does not have a runtime equivalent, e.g. {@code Type#DYN}, the - * return value will be {@code null}. - */ - @Nullable Value adaptType(CelType type); - - /** - * Adapt the check-time {@code type} instance to a runtime {@code Value}. - * - *

When the checked {@code type} does not have a runtime equivalent, e.g. {@code Type#DYN}, the - * return value will be {@code null}. - * - * @deprecated use {@link #adaptType(CelType)} instead. This only exists to maintain compatibility - * with legacy async evaluator. - */ - @Deprecated - @Nullable Value adaptType(@Nullable Type type); -}