Skip to content
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
3 changes: 3 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ java_library(
":type_resolver",
"//common/annotations",
"//common/types",
"//common/types:default_type_provider",
"//common/types:type_providers",
"//common/values",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -665,6 +666,7 @@ java_library(
"//runtime/standard:string",
"//runtime/standard:subtract",
"//runtime/standard:timestamp",
"//runtime/standard:type",
"//runtime/standard:uint",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down Expand Up @@ -723,6 +725,7 @@ cel_android_library(
"//runtime/standard:string_android",
"//runtime/standard:subtract_android",
"//runtime/standard:timestamp_android",
"//runtime/standard:type_android",
"//runtime/standard:uint_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
27 changes: 14 additions & 13 deletions runtime/src/main/java/dev/cel/runtime/CelLiteRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,24 @@ public CelLiteRuntime build() {
}
}

CelTypeProvider celTypeProvider = DefaultTypeProvider.getInstance();
if (this.celTypeProvider != null) {
celTypeProvider =
new CelTypeProvider.CombinedCelTypeProvider(celTypeProvider, this.celTypeProvider);
}

ImmutableMap.Builder<String, CelFunctionBinding> functionBindingsBuilder =
ImmutableMap.builder();

ImmutableSet<CelStandardFunction> standardFunctions = standardFunctionBuilder.build();
if (!standardFunctions.isEmpty()) {
for (CelStandardFunction standardFunction : standardFunctions) {
ImmutableSet<CelFunctionBinding> standardFunctionBinding =
standardFunction.newFunctionBindings(celOptions, runtimeEquality);
for (CelFunctionBinding func : standardFunctionBinding) {
functionBindingsBuilder.put(func.getOverloadId(), func);
}
for (CelStandardFunction standardFunction : standardFunctions) {
for (CelFunctionBinding func :
standardFunction.newFunctionBindings(
celOptions,
runtimeEquality,
celTypeProvider,
celValueProvider.celValueConverter())) {
functionBindingsBuilder.put(func.getOverloadId(), func);
}
}

Expand All @@ -222,12 +229,6 @@ public CelLiteRuntime build() {
func.getDefinition());
});

CelTypeProvider celTypeProvider = DefaultTypeProvider.getInstance();
if (this.celTypeProvider != null) {
celTypeProvider =
new CelTypeProvider.CombinedCelTypeProvider(celTypeProvider, this.celTypeProvider);
}

ProgramPlanner planner =
ProgramPlanner.newPlanner(
celTypeProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import dev.cel.runtime.standard.SubtractOperator.SubtractOverload;
import dev.cel.runtime.standard.TimestampFunction;
import dev.cel.runtime.standard.TimestampFunction.TimestampOverload;
import dev.cel.runtime.standard.TypeFunction;
import dev.cel.runtime.standard.UintFunction;
import dev.cel.runtime.standard.UintFunction.UintOverload;
import java.util.Collection;
Expand Down Expand Up @@ -186,6 +187,7 @@ public final class CelStandardFunctions {
StringFunction.create(),
SubtractOperator.create(),
TimestampFunction.create(),
TypeFunction.create(),
UintFunction.create(),
NotStrictlyFalseFunction.create());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dev.cel.common.annotations.Internal;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.DefaultTypeProvider;
import dev.cel.common.types.StructTypeReference;
import dev.cel.common.types.TypeType;
import dev.cel.common.values.CelValueConverter;
Expand Down Expand Up @@ -89,7 +90,8 @@ public TypeType resolveObjectType(Object obj, CelType typeCheckedType) {

private DescriptorTypeResolver(
@Nullable CelTypeProvider typeProvider, CelValueConverter celValueConverter) {
super(celValueConverter);
super(
typeProvider != null ? typeProvider : DefaultTypeProvider.getInstance(), celValueConverter);
this.typeProvider = typeProvider;
}
}
27 changes: 13 additions & 14 deletions runtime/src/main/java/dev/cel/runtime/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.protobuf.Timestamp;
import dev.cel.common.annotations.Internal;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
Expand Down Expand Up @@ -55,10 +56,12 @@
@Internal
public class TypeResolver {

private final CelTypeProvider celTypeProvider;
private final CelValueConverter celValueConverter;

static TypeResolver create(CelValueConverter celValueConverter) {
return new TypeResolver(celValueConverter);
public static TypeResolver create(
CelTypeProvider celTypeProvider, CelValueConverter celValueConverter) {
return new TypeResolver(celTypeProvider, celValueConverter);
}

// Sentinel runtime value representing the special "type" ident. This ensures following to be
Expand Down Expand Up @@ -121,11 +124,7 @@ TypeType adaptType(CelType typeCheckedType) {
}
}

Optional<TypeType> resolveWellKnownObjectType(Object obj) {
return findWellKnownObjectType(obj);
}

private static Optional<TypeType> findWellKnownObjectType(Object obj) {
static Optional<TypeType> resolveWellKnownObjectType(Object obj) {
if (obj instanceof TypeType) {
return Optional.of(RUNTIME_TYPE_TYPE);
}
Expand Down Expand Up @@ -157,7 +156,7 @@ private static Optional<TypeType> findWellKnownObjectType(Object obj) {
*/
public static String resolveTypeName(Object obj) {
checkNotNull(obj);
Optional<TypeType> resolvedType = findWellKnownObjectType(obj);
Optional<TypeType> resolvedType = resolveWellKnownObjectType(obj);
if (!resolvedType.isPresent()) {
resolvedType = resolveExtendableObjectType(obj.getClass());
}
Expand Down Expand Up @@ -195,11 +194,10 @@ public TypeType resolveObjectType(Object obj, CelType typeCheckedType) {
return wellKnownTypeType.get();
}

if (celValueConverter != null) {
Object celVal = celValueConverter.toRuntimeValue(obj);
if (celVal instanceof CelValue) {
return TypeType.create(((CelValue) celVal).celType());
}
Object celVal = celValueConverter.toRuntimeValue(obj);
if (celVal instanceof CelValue) {
CelType celType = ((CelValue) celVal).celType();
return TypeType.create(celTypeProvider.findType(celType.name()).orElse(celType));
}

if (obj instanceof MessageLiteOrBuilder) {
Expand Down Expand Up @@ -234,7 +232,8 @@ private static CelType adaptStructType(StructType typeOfType) {
return newTypeOfType;
}

protected TypeResolver(CelValueConverter celValueConverter) {
TypeResolver(CelTypeProvider celTypeProvider, CelValueConverter celValueConverter) {
this.celTypeProvider = checkNotNull(celTypeProvider);
this.celValueConverter = checkNotNull(celValueConverter);
}
}
12 changes: 12 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/standard/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ java_library(
deps = [
":standard_overload",
"//common:options",
"//common/types:type_providers",
"//common/values",
"//runtime:function_binding",
"//runtime:runtime_equality",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand All @@ -32,6 +34,8 @@ cel_android_library(
deps = [
":standard_overload_android",
"//common:options",
"//common/types:type_providers_android",
"//common/values:values_android",
"//runtime:function_binding_android",
"//runtime:runtime_equality_android",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -1407,10 +1411,14 @@ java_library(
":standard_overload",
"//common:options",
"//common/types",
"//common/types:type_providers",
"//common/values",
"//runtime:function_binding",
"//runtime:runtime_equality",
"//runtime:type_resolver",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
],
)

Expand All @@ -1423,10 +1431,14 @@ cel_android_library(
":standard_function_android",
":standard_overload_android",
"//common:options",
"//common/types:type_providers_android",
"//common/types:types_android",
"//common/values:values_android",
"//runtime:function_binding_android",
"//runtime:runtime_equality_android",
"//runtime:type_resolver_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
"@maven_android//:com_google_guava_guava",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelOptions;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.values.CelValueConverter;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.RuntimeEquality;

Expand All @@ -43,6 +45,14 @@ public ImmutableSet<CelFunctionBinding> newFunctionBindings(
return CelFunctionBinding.fromOverloads(name, overloadBindings);
}

public ImmutableSet<CelFunctionBinding> newFunctionBindings(
CelOptions celOptions,
RuntimeEquality runtimeEquality,
CelTypeProvider celTypeProvider,
CelValueConverter celValueConverter) {
return newFunctionBindings(celOptions, runtimeEquality);
}

CelStandardFunction(String name, ImmutableSet<CelStandardOverload> overloads) {
checkArgument(!Strings.isNullOrEmpty(name), "Function name must be provided.");
checkArgument(!overloads.isEmpty(), "At least 1 overload must be provided.");
Expand Down
55 changes: 42 additions & 13 deletions runtime/src/main/java/dev/cel/runtime/standard/TypeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,42 @@

package dev.cel.runtime.standard;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelOptions;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.TypeType;
import dev.cel.common.values.CelValueConverter;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.RuntimeEquality;
import dev.cel.runtime.TypeResolver;
import java.util.Arrays;
import org.jspecify.annotations.Nullable;

/**
* Standard function for the {@code type} function.
*
* <p>The {@code type} function returns the CEL type of its argument. It accepts a
* {@link TypeResolver} so that different runtimes can supply the appropriate resolver (e.g. a
* descriptor-based resolver for full proto, or a base resolver for lite proto).
*/
/** Standard function for the {@code type} function. */
@Immutable
public final class TypeFunction extends CelStandardFunction {
private static final TypeFunction ALL_OVERLOADS = create(TypeOverload.values());

private final @Nullable TypeResolver typeResolver;

public static TypeFunction create() {
return ALL_OVERLOADS;
}

private final TypeResolver typeResolver;
public static TypeFunction create(TypeFunction.TypeOverload... overloads) {
return create(Arrays.asList(overloads));
}

public static TypeFunction create(Iterable<TypeFunction.TypeOverload> overloads) {
return new TypeFunction(ImmutableSet.copyOf(overloads), /* typeResolver= */ null);
}

public static TypeFunction create(TypeResolver typeResolver) {
return new TypeFunction(typeResolver);
return new TypeFunction(ImmutableSet.copyOf(TypeOverload.values()), checkNotNull(typeResolver));
}

/** Overloads for the standard {@code type} function. */
Expand All @@ -48,24 +63,38 @@ public CelFunctionBinding newFunctionBinding(
// TypeResolver instance.
// TODO: Instantiate from CelStandardFunctions.
throw new UnsupportedOperationException(
"TypeOverload bindings must be created through TypeFunction.create(TypeResolver)");
"TypeOverload bindings must be created through TypeFunction");
}
}

@Override
public ImmutableSet<CelFunctionBinding> newFunctionBindings(
CelOptions celOptions, RuntimeEquality runtimeEquality) {
return newFunctionBindings(checkNotNull(typeResolver));
}

@Override
public ImmutableSet<CelFunctionBinding> newFunctionBindings(
CelOptions celOptions,
RuntimeEquality runtimeEquality,
CelTypeProvider celTypeProvider,
CelValueConverter celValueConverter) {
return newFunctionBindings(TypeResolver.create(celTypeProvider, celValueConverter));
}

private static ImmutableSet<CelFunctionBinding> newFunctionBindings(TypeResolver typeResolver) {
CelFunctionBinding binding =
CelFunctionBinding.from(
"type",
Object.class,
arg -> typeResolver.resolveObjectType(arg, TypeType.create(SimpleType.DYN)));

return CelFunctionBinding.fromOverloads("type", ImmutableSet.of(binding));
return CelFunctionBinding.fromOverloads("type", binding);
}

private TypeFunction(TypeResolver typeResolver) {
super("type", ImmutableSet.copyOf(TypeOverload.values()));
private TypeFunction(
ImmutableSet<CelStandardOverload> overloads, @Nullable TypeResolver typeResolver) {
super("type", overloads);
this.typeResolver = typeResolver;
}
}
3 changes: 3 additions & 0 deletions runtime/src/test/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ java_library(
"//common/internal:well_known_proto",
"//common/types",
"//common/types:cel_v1alpha1_types",
"//common/types:default_type_provider",
"//common/types:message_type_provider",
"//common/values",
"//common/values:cel_byte_string",
Expand Down Expand Up @@ -101,9 +102,11 @@ java_library(
"//runtime:unknown_attributes",
"//runtime:unknown_options",
"//runtime/standard:add",
"//runtime/standard:equals",
"//runtime/standard:not_strictly_false",
"//runtime/standard:standard_overload",
"//runtime/standard:subtract",
"//runtime/standard:type",
"//testing:cel_runtime_flavor",
"//testing/protos:message_with_enum_cel_java_proto",
"//testing/protos:message_with_enum_java_proto",
Expand Down
Loading
Loading