Skip to content

Commit b6ff96b

Browse files
l46kokcopybara-github
authored andcommitted
Internal Changes
PiperOrigin-RevId: 824719239
1 parent 9083e06 commit b6ff96b

33 files changed

+2704
-5
lines changed

common/src/main/java/dev/cel/common/ast/CelConstant.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,26 @@ public static CelConstant ofObjectValue(Object value) {
207207

208208
throw new IllegalArgumentException("Value is not a CelConstant: " + value);
209209
}
210+
211+
/** Gets the underlying value held by this constant. */
212+
public Object objectValue() {
213+
switch (getKind()) {
214+
case NULL_VALUE:
215+
return nullValue();
216+
case BOOLEAN_VALUE:
217+
return booleanValue();
218+
case INT64_VALUE:
219+
return int64Value();
220+
case UINT64_VALUE:
221+
return uint64Value();
222+
case DOUBLE_VALUE:
223+
return doubleValue();
224+
case STRING_VALUE:
225+
return stringValue();
226+
case BYTES_VALUE:
227+
return bytesValue();
228+
default:
229+
throw new IllegalStateException("Unsupported kind: " + getKind());
230+
}
231+
}
210232
}

common/src/main/java/dev/cel/common/types/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ java_library(
183183
],
184184
)
185185

186+
java_library(
187+
name = "default_type_provider",
188+
srcs = [
189+
"DefaultTypeProvider.java",
190+
],
191+
tags = [
192+
],
193+
deps = [
194+
":type_providers",
195+
":types",
196+
"@maven//:com_google_guava_guava",
197+
],
198+
)
199+
186200
cel_android_library(
187201
name = "cel_types_android",
188202
srcs = ["CelTypes.java"],
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.types;
16+
17+
import com.google.common.collect.ImmutableCollection;
18+
import com.google.common.collect.ImmutableMap;
19+
import java.util.Optional;
20+
21+
/** {@code DefaultTypeProvider} is a registry of common CEL types. */
22+
public class DefaultTypeProvider implements CelTypeProvider {
23+
24+
private static final DefaultTypeProvider INSTANCE = new DefaultTypeProvider();
25+
private final ImmutableMap<String, CelType> commonTypes;
26+
27+
@Override
28+
public ImmutableCollection<CelType> types() {
29+
return commonTypes.values();
30+
}
31+
32+
@Override
33+
public Optional<CelType> findType(String typeName) {
34+
return Optional.ofNullable(commonTypes.get(typeName));
35+
}
36+
37+
public static DefaultTypeProvider getInstance() {
38+
return INSTANCE;
39+
}
40+
41+
private DefaultTypeProvider() {
42+
ImmutableMap.Builder<String, CelType> typeMapBuilder = ImmutableMap.builder();
43+
typeMapBuilder.putAll(SimpleType.TYPE_MAP);
44+
typeMapBuilder.put("list", ListType.create(SimpleType.DYN));
45+
typeMapBuilder.put("map", MapType.create(SimpleType.DYN, SimpleType.DYN));
46+
typeMapBuilder.put(
47+
"optional_type",
48+
// TODO: Move to CelOptionalLibrary and register it on demand
49+
OptionalType.create(SimpleType.DYN));
50+
this.commonTypes = typeMapBuilder.buildOrThrow();
51+
}
52+
}

common/src/main/java/dev/cel/common/types/SimpleType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class SimpleType extends CelType {
4444
public static final CelType TIMESTAMP = create(CelKind.TIMESTAMP, "google.protobuf.Timestamp");
4545
public static final CelType UINT = create(CelKind.UINT, "uint");
4646

47-
private static final ImmutableMap<String, CelType> TYPE_MAP =
47+
public static final ImmutableMap<String, CelType> TYPE_MAP =
4848
ImmutableMap.of(
4949
DYN.name(), DYN,
5050
BOOL.name(), BOOL,

common/src/main/java/dev/cel/common/values/CelValueConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3434
@Internal
3535
@Immutable
36-
abstract class CelValueConverter {
36+
public abstract class CelValueConverter {
3737

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

4242
if (celValue instanceof MapValue) {
4343
MapValue<CelValue, CelValue> mapValue = (MapValue<CelValue, CelValue>) celValue;
44-
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
44+
ImmutableMap.Builder<Object, Object> mapBuilder =
45+
ImmutableMap.builderWithExpectedSize(mapValue.size());
4546
for (Entry<CelValue, CelValue> entry : mapValue.value().entrySet()) {
4647
Object key = fromCelValueToJavaObject(entry.getKey());
4748
Object value = fromCelValueToJavaObject(entry.getValue());
@@ -51,7 +52,8 @@ public Object fromCelValueToJavaObject(CelValue celValue) {
5152
return mapBuilder.buildOrThrow();
5253
} else if (celValue instanceof ListValue) {
5354
ListValue<CelValue> listValue = (ListValue<CelValue>) celValue;
54-
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
55+
ImmutableList.Builder<Object> listBuilder =
56+
ImmutableList.builderWithExpectedSize(listValue.size());
5557
for (CelValue element : listValue.value()) {
5658
listBuilder.add(fromCelValueToJavaObject(element));
5759
}

common/types/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ java_library(
5050
exports = ["//common/src/main/java/dev/cel/common/types:cel_proto_message_types"],
5151
)
5252

53+
java_library(
54+
name = "default_type_provider",
55+
visibility = ["//:internal"],
56+
exports = ["//common/src/main/java/dev/cel/common/types:default_type_provider"],
57+
)
58+
5359
java_library(
5460
name = "cel_v1alpha1_types",
5561
visibility = ["//:internal"],

runtime/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ java_library(
1919
],
2020
)
2121

22+
java_library(
23+
name = "cel_value_dispatcher",
24+
visibility = ["//:internal"],
25+
exports = [
26+
"//runtime/src/main/java/dev/cel/runtime:cel_value_dispatcher",
27+
],
28+
)
29+
2230
java_library(
2331
name = "dispatcher",
2432
visibility = ["//:internal"],
@@ -233,6 +241,16 @@ java_library(
233241
exports = ["//runtime/src/main/java/dev/cel/runtime:resolved_overload_internal"],
234242
)
235243

244+
java_library(
245+
name = "cel_value_function_binding",
246+
exports = ["//runtime/src/main/java/dev/cel/runtime:cel_value_function_binding"],
247+
)
248+
249+
java_library(
250+
name = "cel_value_function_overload",
251+
exports = ["//runtime/src/main/java/dev/cel/runtime:cel_value_function_overload"],
252+
)
253+
236254
java_library(
237255
name = "internal_function_binder",
238256
visibility = ["//:internal"],

runtime/planner/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_applicable_licenses = ["//:license"],
5+
default_visibility = ["//:internal"],
6+
)
7+
8+
java_library(
9+
name = "program_planner",
10+
exports = ["//runtime/src/main/java/dev/cel/runtime/planner:program_planner"],
11+
)

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ java_library(
115115
],
116116
)
117117

118+
java_library(
119+
name = "cel_value_dispatcher",
120+
srcs = ["CelValueDispatcher.java"],
121+
tags = [
122+
],
123+
deps = [
124+
":cel_value_function_binding",
125+
":cel_value_function_overload",
126+
"//:auto_value",
127+
"//common/annotations",
128+
"@maven//:com_google_errorprone_error_prone_annotations",
129+
"@maven//:com_google_guava_guava",
130+
],
131+
)
132+
118133
java_library(
119134
name = "dispatcher",
120135
srcs = DISPATCHER_SOURCES,
@@ -583,8 +598,8 @@ java_library(
583598
srcs = INTERPRABLE_SOURCES,
584599
deps = [
585600
":evaluation_exception",
586-
":evaluation_listener",
587601
"//common/annotations",
602+
"//runtime:evaluation_listener",
588603
"//runtime:function_resolver",
589604
"@maven//:com_google_errorprone_error_prone_annotations",
590605
"@maven//:org_jspecify_jspecify",
@@ -804,6 +819,7 @@ java_library(
804819
"@maven//:com_google_code_findbugs_annotations",
805820
"@maven//:com_google_errorprone_error_prone_annotations",
806821
"@maven//:com_google_protobuf_protobuf_java",
822+
"@maven_android//:com_google_protobuf_protobuf_javalite",
807823
],
808824
)
809825

@@ -837,6 +853,7 @@ java_library(
837853
":function_resolver",
838854
":interpretable",
839855
":interpreter",
856+
":program",
840857
":proto_message_activation_factory",
841858
":proto_message_runtime_equality",
842859
":runtime_equality",
@@ -873,6 +890,7 @@ java_library(
873890
deps = [
874891
":evaluation_exception",
875892
":function_binding",
893+
":program",
876894
"//:auto_value",
877895
"//common:cel_ast",
878896
"//common:options",
@@ -908,6 +926,7 @@ java_library(
908926
"//runtime:program",
909927
"//runtime/standard:standard_function",
910928
"@maven//:com_google_code_findbugs_annotations",
929+
"@maven//:com_google_errorprone_error_prone_annotations",
911930
"@maven//:com_google_guava_guava",
912931
],
913932
)
@@ -1194,6 +1213,34 @@ cel_android_library(
11941213
],
11951214
)
11961215

1216+
java_library(
1217+
name = "cel_value_function_binding",
1218+
srcs = ["CelValueFunctionBinding.java"],
1219+
tags = [
1220+
],
1221+
deps = [
1222+
":cel_value_function_overload",
1223+
"//common/values:cel_value",
1224+
"@maven//:com_google_errorprone_error_prone_annotations",
1225+
"@maven//:com_google_guava_guava",
1226+
],
1227+
)
1228+
1229+
java_library(
1230+
name = "cel_value_function_overload",
1231+
srcs = [
1232+
"CelValueFunctionOverload.java",
1233+
],
1234+
tags = [
1235+
],
1236+
deps = [
1237+
":evaluation_exception",
1238+
"//common/values:cel_value",
1239+
"@maven//:com_google_errorprone_error_prone_annotations",
1240+
"@maven//:com_google_guava_guava",
1241+
],
1242+
)
1243+
11971244
java_library(
11981245
name = "resolved_overload",
11991246
srcs = ["CelResolvedOverload.java"],
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.runtime;
16+
17+
import com.google.auto.value.AutoValue;
18+
import com.google.common.collect.ImmutableList;
19+
import com.google.common.collect.ImmutableMap;
20+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21+
import com.google.errorprone.annotations.CheckReturnValue;
22+
import com.google.errorprone.annotations.Immutable;
23+
import dev.cel.common.annotations.Internal;
24+
import java.util.Optional;
25+
26+
/** TODO */
27+
@Internal
28+
@AutoValue
29+
@Immutable
30+
public abstract class CelValueDispatcher {
31+
32+
abstract ImmutableMap<String, CelValueFunctionBinding> overloads();
33+
34+
public Optional<CelValueFunctionBinding> findOverload(String overloadId) {
35+
return Optional.ofNullable(overloads().get(overloadId));
36+
}
37+
38+
public static Builder newBuilder() {
39+
return new AutoValue_CelValueDispatcher.Builder();
40+
}
41+
42+
/** TODO */
43+
@AutoValue.Builder
44+
public abstract static class Builder {
45+
public abstract ImmutableMap.Builder<String, CelValueFunctionBinding> overloadsBuilder();
46+
47+
@CanIgnoreReturnValue
48+
public Builder addOverload(CelValueFunctionBinding functionBinding) {
49+
overloadsBuilder().put(functionBinding.overloadId(), functionBinding);
50+
return this;
51+
}
52+
53+
@CanIgnoreReturnValue
54+
public Builder addDynamicDispatchOverload(
55+
String functionName, CelValueFunctionOverload definition) {
56+
overloadsBuilder()
57+
.put(
58+
functionName,
59+
CelValueFunctionBinding.from(functionName, ImmutableList.of(), definition));
60+
61+
return this;
62+
}
63+
64+
@CheckReturnValue
65+
public abstract CelValueDispatcher build();
66+
}
67+
}

0 commit comments

Comments
 (0)