Skip to content

Commit 5b134f8

Browse files
l46kokcopybara-github
authored andcommitted
Add value type parameter in StructValue
PiperOrigin-RevId: 904602834
1 parent 1e1d8ea commit 5b134f8

29 files changed

Lines changed: 424 additions & 250 deletions

bundle/BUILD.bazel

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ java_library(
1313
],
1414
)
1515

16-
java_library(
17-
name = "cel_experimental_factory",
18-
visibility = ["//:internal"],
19-
exports = [
20-
"//bundle/src/main/java/dev/cel/bundle:cel_experimental_factory",
21-
],
22-
)
23-
2416
java_library(
2517
name = "environment",
2618
exports = ["//bundle/src/main/java/dev/cel/bundle:environment"],

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ java_library(
3535
"@cel_spec//proto/cel/expr:checked_java_proto",
3636
"@maven//:com_google_code_findbugs_annotations",
3737
"@maven//:com_google_errorprone_error_prone_annotations",
38-
"@maven//:com_google_guava_guava",
3938
"@maven//:com_google_protobuf_protobuf_java",
4039
],
4140
)
@@ -54,22 +53,6 @@ java_library(
5453
"//compiler:compiler_builder",
5554
"//parser",
5655
"//runtime",
57-
],
58-
)
59-
60-
java_library(
61-
name = "cel_experimental_factory",
62-
srcs = ["CelExperimentalFactory.java"],
63-
tags = [
64-
],
65-
deps = [
66-
":cel",
67-
":cel_impl",
68-
"//checker",
69-
"//common:options",
70-
"//common/annotations",
71-
"//compiler",
72-
"//parser",
7356
"//runtime:runtime_planner_impl",
7457
],
7558
)
@@ -117,7 +100,6 @@ java_library(
117100
tags = [
118101
],
119102
deps = [
120-
":cel_factory",
121103
":environment_exception",
122104
":required_fields_checker",
123105
"//:auto_value",
@@ -190,7 +172,6 @@ java_library(
190172
"//common:options",
191173
"//common/internal:env_visitor",
192174
"//common/types:cel_proto_types",
193-
"//common/types:cel_types",
194175
"//common/types:type_providers",
195176
"//compiler:compiler_builder",
196177
"//extensions",

bundle/src/main/java/dev/cel/bundle/CelExperimentalFactory.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

bundle/src/main/java/dev/cel/bundle/CelFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import dev.cel.compiler.CelCompilerImpl;
2121
import dev.cel.parser.CelParserImpl;
2222
import dev.cel.runtime.CelRuntime;
23+
import dev.cel.runtime.CelRuntimeImpl;
2324
import dev.cel.runtime.CelRuntimeLegacyImpl;
2425

2526
/** Helper class to configure the entire CEL stack in a common interface. */
@@ -44,6 +45,30 @@ public static CelBuilder standardCelBuilder() {
4445
.setStandardEnvironmentEnabled(true);
4546
}
4647

48+
/**
49+
* Creates a builder for configuring CEL for the parsing, optional type-checking, and evaluation
50+
* of expressions using the Program Planner.
51+
*
52+
* <p>The {@code ProgramPlanner} architecture provides key benefits over the {@link
53+
* #standardCelBuilder()}:
54+
*
55+
* <ul>
56+
* <li><b>Performance:</b> Programs can be cached for improving evaluation speed.
57+
* <li><b>Parsed-only expression evaluation:</b> Unlike the traditional stack which required
58+
* supplying type-checked expressions, this architecture handles both parsed-only and
59+
* type-checked expressions.
60+
* </ul>
61+
*/
62+
public static CelBuilder plannerCelBuilder() {
63+
return CelImpl.newBuilder(
64+
CelCompilerImpl.newBuilder(
65+
CelParserImpl.newBuilder(),
66+
CelCheckerLegacyImpl.newBuilder().setStandardEnvironmentEnabled(true)),
67+
CelRuntimeImpl.newBuilder())
68+
// CEL-Internal-2
69+
.setOptions(CelOptions.current().enableHeterogeneousNumericComparisons(true).build());
70+
}
71+
4772
/** Combines a prebuilt {@link CelCompiler} and {@link CelRuntime} into {@link Cel}. */
4873
public static Cel combine(CelCompiler celCompiler, CelRuntime celRuntime) {
4974
return CelImpl.combine(celCompiler, celRuntime);

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ cel_android_library(
7878

7979
java_library(
8080
name = "combined_cel_value_provider",
81-
srcs = ["CombinedCelValueProvider.java"],
81+
srcs = [
82+
"CombinedCelValueProvider.java",
83+
],
8284
tags = [
8385
],
8486
deps = [
87+
":combined_cel_value_converter",
88+
":values",
8589
"//common/values:cel_value_provider",
8690
"@maven//:com_google_errorprone_error_prone_annotations",
8791
"@maven//:com_google_guava_guava",
@@ -90,16 +94,52 @@ java_library(
9094

9195
cel_android_library(
9296
name = "combined_cel_value_provider_android",
93-
srcs = ["CombinedCelValueProvider.java"],
97+
srcs = [
98+
"CombinedCelValueProvider.java",
99+
],
94100
tags = [
95101
],
96102
deps = [
103+
":combined_cel_value_converter_android",
104+
":values_android",
97105
"//common/values:cel_value_provider_android",
98106
"@maven//:com_google_errorprone_error_prone_annotations",
99107
"@maven_android//:com_google_guava_guava",
100108
],
101109
)
102110

111+
java_library(
112+
name = "combined_cel_value_converter",
113+
srcs = [
114+
"CombinedCelValueConverter.java",
115+
],
116+
tags = [
117+
],
118+
deps = [
119+
":values",
120+
"//common/annotations",
121+
"@maven//:com_google_errorprone_error_prone_annotations",
122+
"@maven//:com_google_guava_guava",
123+
"@maven//:org_jspecify_jspecify",
124+
],
125+
)
126+
127+
cel_android_library(
128+
name = "combined_cel_value_converter_android",
129+
srcs = [
130+
"CombinedCelValueConverter.java",
131+
],
132+
tags = [
133+
],
134+
deps = [
135+
":values_android",
136+
"//common/annotations",
137+
"@maven//:com_google_errorprone_error_prone_annotations",
138+
"@maven//:org_jspecify_jspecify",
139+
"@maven_android//:com_google_guava_guava",
140+
],
141+
)
142+
103143
java_library(
104144
name = "values",
105145
srcs = CEL_VALUES_SOURCES,

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

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import dev.cel.common.annotations.Internal;
2222
import java.util.Collection;
2323
import java.util.Map;
24-
import java.util.Map.Entry;
2524
import java.util.Optional;
25+
import java.util.function.Function;
2626

2727
/**
2828
* {@code CelValueConverter} handles bidirectional conversion between native Java objects to {@link
@@ -37,6 +37,12 @@ public class CelValueConverter {
3737

3838
private static final CelValueConverter DEFAULT_INSTANCE = new CelValueConverter();
3939

40+
@SuppressWarnings("Immutable") // Method reference is immutable
41+
private final Function<Object, Object> maybeUnwrapFunction;
42+
43+
@SuppressWarnings("Immutable") // Method reference is immutable
44+
private final Function<Object, Object> toRuntimeValueFunction;
45+
4046
public static CelValueConverter getDefaultInstance() {
4147
return DEFAULT_INSTANCE;
4248
}
@@ -51,14 +57,26 @@ public Object maybeUnwrap(Object value) {
5157
return unwrap((CelValue) value);
5258
}
5359

60+
Object mapped = mapContainer(value, maybeUnwrapFunction);
61+
if (mapped != value) {
62+
return mapped;
63+
}
64+
65+
return value;
66+
}
67+
68+
/**
69+
* Maps a container (Collection or Map) by applying the provided mapper function to its elements.
70+
* Returns the original value if it's not a supported container.
71+
*/
72+
protected Object mapContainer(Object value, Function<Object, Object> mapper) {
5473
if (value instanceof Collection) {
5574
Collection<Object> collection = (Collection<Object>) value;
5675
ImmutableList.Builder<Object> builder =
5776
ImmutableList.builderWithExpectedSize(collection.size());
5877
for (Object element : collection) {
59-
builder.add(maybeUnwrap(element));
78+
builder.add(mapper.apply(element));
6079
}
61-
6280
return builder.build();
6381
}
6482

@@ -67,34 +85,30 @@ public Object maybeUnwrap(Object value) {
6785
ImmutableMap.Builder<Object, Object> builder =
6886
ImmutableMap.builderWithExpectedSize(map.size());
6987
for (Map.Entry<Object, Object> entry : map.entrySet()) {
70-
builder.put(maybeUnwrap(entry.getKey()), maybeUnwrap(entry.getValue()));
88+
builder.put(mapper.apply(entry.getKey()), mapper.apply(entry.getValue()));
7189
}
72-
7390
return builder.buildOrThrow();
7491
}
7592

7693
return value;
7794
}
7895

79-
/**
80-
* Canonicalizes an inbound {@code value} into a suitable Java object representation for
81-
* evaluation.
82-
*/
8396
public Object toRuntimeValue(Object value) {
8497
Preconditions.checkNotNull(value);
8598

8699
if (value instanceof CelValue) {
87100
return value;
88101
}
89102

90-
if (value instanceof Collection) {
91-
return toListValue((Collection<Object>) value);
92-
} else if (value instanceof Map) {
93-
return toMapValue((Map<Object, Object>) value);
94-
} else if (value instanceof Optional) {
103+
Object mapped = mapContainer(value, toRuntimeValueFunction);
104+
if (mapped != value) {
105+
return mapped;
106+
}
107+
108+
if (value instanceof Optional) {
95109
Optional<Object> optionalValue = (Optional<Object>) value;
96110
return optionalValue
97-
.map(this::toRuntimeValue)
111+
.map(toRuntimeValueFunction)
98112
.map(OptionalValue::create)
99113
.orElse(OptionalValue.EMPTY);
100114
}
@@ -136,31 +150,9 @@ private Object unwrap(CelValue celValue) {
136150
return celValue.value();
137151
}
138152

139-
private ImmutableList<Object> toListValue(Collection<Object> iterable) {
140-
Preconditions.checkNotNull(iterable);
141-
142-
ImmutableList.Builder<Object> listBuilder =
143-
ImmutableList.builderWithExpectedSize(iterable.size());
144-
for (Object entry : iterable) {
145-
listBuilder.add(toRuntimeValue(entry));
146-
}
147-
148-
return listBuilder.build();
149-
}
150-
151-
private ImmutableMap<Object, Object> toMapValue(Map<Object, Object> map) {
152-
Preconditions.checkNotNull(map);
153153

154-
ImmutableMap.Builder<Object, Object> mapBuilder =
155-
ImmutableMap.builderWithExpectedSize(map.size());
156-
for (Entry<Object, Object> entry : map.entrySet()) {
157-
Object mapKey = toRuntimeValue(entry.getKey());
158-
Object mapValue = toRuntimeValue(entry.getValue());
159-
mapBuilder.put(mapKey, mapValue);
160-
}
161-
162-
return mapBuilder.buildOrThrow();
154+
protected CelValueConverter() {
155+
this.maybeUnwrapFunction = this::maybeUnwrap;
156+
this.toRuntimeValueFunction = this::toRuntimeValue;
163157
}
164-
165-
protected CelValueConverter() {}
166158
}

0 commit comments

Comments
 (0)