Skip to content
Closed
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
22 changes: 22 additions & 0 deletions common/src/main/java/dev/cel/common/ast/CelConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,26 @@ public static CelConstant ofObjectValue(Object value) {

throw new IllegalArgumentException("Value is not a CelConstant: " + value);
}

/** Gets the underlying value held by this constant. */
public Object objectValue() {
switch (getKind()) {
case NULL_VALUE:
return nullValue();
case BOOLEAN_VALUE:
return booleanValue();
case INT64_VALUE:
return int64Value();
case UINT64_VALUE:
return uint64Value();
case DOUBLE_VALUE:
return doubleValue();
case STRING_VALUE:
return stringValue();
case BYTES_VALUE:
return bytesValue();
default:
throw new IllegalStateException("Unsupported kind: " + getKind());
}
}
}
14 changes: 14 additions & 0 deletions common/src/main/java/dev/cel/common/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ java_library(
],
)

java_library(
name = "default_type_provider",
srcs = [
"DefaultTypeProvider.java",
],
tags = [
],
deps = [
":type_providers",
":types",
"@maven//:com_google_guava_guava",
],
)

cel_android_library(
name = "cel_types_android",
srcs = ["CelTypes.java"],
Expand Down
52 changes: 52 additions & 0 deletions common/src/main/java/dev/cel/common/types/DefaultTypeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2025 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.common.types;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import java.util.Optional;

/** {@code DefaultTypeProvider} is a registry of common CEL types. */
public class DefaultTypeProvider implements CelTypeProvider {

private static final DefaultTypeProvider INSTANCE = new DefaultTypeProvider();
private final ImmutableMap<String, CelType> commonTypes;

@Override
public ImmutableCollection<CelType> types() {
return commonTypes.values();
}

@Override
public Optional<CelType> findType(String typeName) {
return Optional.ofNullable(commonTypes.get(typeName));
}

public static DefaultTypeProvider getInstance() {
return INSTANCE;
}

private DefaultTypeProvider() {
ImmutableMap.Builder<String, CelType> typeMapBuilder = ImmutableMap.builder();
typeMapBuilder.putAll(SimpleType.TYPE_MAP);
typeMapBuilder.put("list", ListType.create(SimpleType.DYN));
typeMapBuilder.put("map", MapType.create(SimpleType.DYN, SimpleType.DYN));
typeMapBuilder.put(
"optional_type",
// TODO: Move to CelOptionalLibrary and register it on demand
OptionalType.create(SimpleType.DYN));
this.commonTypes = typeMapBuilder.buildOrThrow();
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/dev/cel/common/types/SimpleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class SimpleType extends CelType {
public static final CelType TIMESTAMP = create(CelKind.TIMESTAMP, "google.protobuf.Timestamp");
public static final CelType UINT = create(CelKind.UINT, "uint");

private static final ImmutableMap<String, CelType> TYPE_MAP =
public static final ImmutableMap<String, CelType> TYPE_MAP =
ImmutableMap.of(
DYN.name(), DYN,
BOOL.name(), BOOL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
@Internal
@Immutable
abstract class CelValueConverter {
public abstract class CelValueConverter {

/** Adapts a {@link CelValue} to a plain old Java Object. */
public Object fromCelValueToJavaObject(CelValue celValue) {
Preconditions.checkNotNull(celValue);

if (celValue instanceof MapValue) {
MapValue<CelValue, CelValue> mapValue = (MapValue<CelValue, CelValue>) celValue;
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
ImmutableMap.Builder<Object, Object> mapBuilder =
ImmutableMap.builderWithExpectedSize(mapValue.size());
for (Entry<CelValue, CelValue> entry : mapValue.value().entrySet()) {
Object key = fromCelValueToJavaObject(entry.getKey());
Object value = fromCelValueToJavaObject(entry.getValue());
Expand All @@ -51,7 +52,8 @@ public Object fromCelValueToJavaObject(CelValue celValue) {
return mapBuilder.buildOrThrow();
} else if (celValue instanceof ListValue) {
ListValue<CelValue> listValue = (ListValue<CelValue>) celValue;
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
ImmutableList.Builder<Object> listBuilder =
ImmutableList.builderWithExpectedSize(listValue.size());
for (CelValue element : listValue.value()) {
listBuilder.add(fromCelValueToJavaObject(element));
}
Expand Down
6 changes: 6 additions & 0 deletions common/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ java_library(
exports = ["//common/src/main/java/dev/cel/common/types:cel_proto_message_types"],
)

java_library(
name = "default_type_provider",
visibility = ["//:internal"],
exports = ["//common/src/main/java/dev/cel/common/types:default_type_provider"],
)

java_library(
name = "cel_v1alpha1_types",
visibility = ["//:internal"],
Expand Down
18 changes: 18 additions & 0 deletions runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ java_library(
],
)

java_library(
name = "cel_value_dispatcher",
visibility = ["//:internal"],
exports = [
"//runtime/src/main/java/dev/cel/runtime:cel_value_dispatcher",
],
)

java_library(
name = "dispatcher",
visibility = ["//:internal"],
Expand Down Expand Up @@ -233,6 +241,16 @@ java_library(
exports = ["//runtime/src/main/java/dev/cel/runtime:resolved_overload_internal"],
)

java_library(
name = "cel_value_function_binding",
exports = ["//runtime/src/main/java/dev/cel/runtime:cel_value_function_binding"],
)

java_library(
name = "cel_value_function_overload",
exports = ["//runtime/src/main/java/dev/cel/runtime:cel_value_function_overload"],
)

java_library(
name = "internal_function_binder",
visibility = ["//:internal"],
Expand Down
11 changes: 11 additions & 0 deletions runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_java//java:defs.bzl", "java_library")

package(
default_applicable_licenses = ["//:license"],
default_visibility = ["//:internal"],
)

java_library(
name = "program_planner",
exports = ["//runtime/src/main/java/dev/cel/runtime/planner:program_planner"],
)
49 changes: 48 additions & 1 deletion runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ java_library(
],
)

java_library(
name = "cel_value_dispatcher",
srcs = ["CelValueDispatcher.java"],
tags = [
],
deps = [
":cel_value_function_binding",
":cel_value_function_overload",
"//:auto_value",
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "dispatcher",
srcs = DISPATCHER_SOURCES,
Expand Down Expand Up @@ -583,8 +598,8 @@ java_library(
srcs = INTERPRABLE_SOURCES,
deps = [
":evaluation_exception",
":evaluation_listener",
"//common/annotations",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
Expand Down Expand Up @@ -804,6 +819,7 @@ java_library(
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_protobuf_protobuf_java",
"@maven_android//:com_google_protobuf_protobuf_javalite",
],
)

Expand Down Expand Up @@ -837,6 +853,7 @@ java_library(
":function_resolver",
":interpretable",
":interpreter",
":program",
":proto_message_activation_factory",
":proto_message_runtime_equality",
":runtime_equality",
Expand Down Expand Up @@ -873,6 +890,7 @@ java_library(
deps = [
":evaluation_exception",
":function_binding",
":program",
"//:auto_value",
"//common:cel_ast",
"//common:options",
Expand Down Expand Up @@ -908,6 +926,7 @@ java_library(
"//runtime:program",
"//runtime/standard:standard_function",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
Expand Down Expand Up @@ -1194,6 +1213,34 @@ cel_android_library(
],
)

java_library(
name = "cel_value_function_binding",
srcs = ["CelValueFunctionBinding.java"],
tags = [
],
deps = [
":cel_value_function_overload",
"//common/values:cel_value",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "cel_value_function_overload",
srcs = [
"CelValueFunctionOverload.java",
],
tags = [
],
deps = [
":evaluation_exception",
"//common/values:cel_value",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "resolved_overload",
srcs = ["CelResolvedOverload.java"],
Expand Down
67 changes: 67 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/CelValueDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2025 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 com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;
import java.util.Optional;

/** TODO */
@Internal
@AutoValue
@Immutable
public abstract class CelValueDispatcher {

abstract ImmutableMap<String, CelValueFunctionBinding> overloads();

public Optional<CelValueFunctionBinding> findOverload(String overloadId) {
return Optional.ofNullable(overloads().get(overloadId));
}

public static Builder newBuilder() {
return new AutoValue_CelValueDispatcher.Builder();
}

/** TODO */
@AutoValue.Builder
public abstract static class Builder {
public abstract ImmutableMap.Builder<String, CelValueFunctionBinding> overloadsBuilder();

@CanIgnoreReturnValue
public Builder addOverload(CelValueFunctionBinding functionBinding) {
overloadsBuilder().put(functionBinding.overloadId(), functionBinding);
return this;
}

@CanIgnoreReturnValue
public Builder addDynamicDispatchOverload(
String functionName, CelValueFunctionOverload definition) {
overloadsBuilder()
.put(
functionName,
CelValueFunctionBinding.from(functionName, ImmutableList.of(), definition));

return this;
}

@CheckReturnValue
public abstract CelValueDispatcher build();
}
}
Loading
Loading