Skip to content

Commit 2c693a3

Browse files
l46kokcopybara-github
authored andcommitted
Introduce top-level runtime APIs based on Program Planner
PiperOrigin-RevId: 857407924
1 parent b8eafbf commit 2c693a3

24 files changed

+940
-104
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ java_library(
5757
tags = [
5858
],
5959
deps = [
60+
"//common/values",
6061
"@maven//:com_google_errorprone_error_prone_annotations",
6162
"@maven//:com_google_guava_guava",
6263
],
@@ -68,6 +69,7 @@ cel_android_library(
6869
tags = [
6970
],
7071
deps = [
72+
"//common/values:values_android",
7173
"@maven//:com_google_errorprone_error_prone_annotations",
7274
"@maven_android//:com_google_guava_guava",
7375
],
@@ -207,13 +209,13 @@ java_library(
207209
tags = [
208210
],
209211
deps = [
210-
":base_proto_message_value_provider",
211212
":proto_message_value",
212213
"//common:options",
213214
"//common/annotations",
214215
"//common/internal:dynamic_proto",
215216
"//common/internal:proto_message_factory",
216-
"//common/values:base_proto_cel_value_converter",
217+
"//common/values",
218+
"//common/values:cel_value_provider",
217219
"@maven//:com_google_errorprone_error_prone_annotations",
218220
"@maven//:com_google_protobuf_protobuf_java",
219221
],

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3434
@Internal
3535
@Immutable
36-
public abstract class CelValueConverter {
36+
public class CelValueConverter {
37+
38+
private static final CelValueConverter DEFAULT_INSTANCE = new CelValueConverter();
39+
40+
public static CelValueConverter getDefaultInstance() {
41+
return DEFAULT_INSTANCE;
42+
}
3743

3844
/** Adapts a {@link CelValue} to a plain old Java Object. */
3945
public Object unwrap(CelValue celValue) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public interface CelValueProvider {
2727
* a wrapper.
2828
*/
2929
Optional<Object> newValue(String structType, Map<String, Object> fields);
30+
31+
default CelValueConverter celValueConverter() {
32+
return CelValueConverter.getDefaultInstance();
33+
}
3034
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ public Object toRuntimeValue(Object value) {
8282
}
8383

8484
if (value instanceof MessageOrBuilder) {
85-
MessageOrBuilder message = (MessageOrBuilder) value;
85+
Message message;
86+
if (value instanceof Message.Builder) {
87+
message = ((Message.Builder) value).build();
88+
} else {
89+
message = (Message) value;
90+
}
91+
8692
// Attempt to convert the proto from a dynamic message into a concrete message if possible.
8793
if (message instanceof DynamicMessage) {
8894
message = dynamicProto.maybeAdaptDynamicMessage((DynamicMessage) message);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
*/
3535
@Immutable
3636
@Internal
37-
public class ProtoMessageValueProvider extends BaseProtoMessageValueProvider {
37+
public class ProtoMessageValueProvider implements CelValueProvider {
3838
private final ProtoAdapter protoAdapter;
3939
private final ProtoMessageFactory protoMessageFactory;
4040
private final ProtoCelValueConverter protoCelValueConverter;
4141

4242
@Override
43-
public BaseProtoCelValueConverter protoCelValueConverter() {
43+
public CelValueConverter celValueConverter() {
4444
return protoCelValueConverter;
4545
}
4646

common/src/test/java/dev/cel/common/values/StructValueTest.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import dev.cel.common.types.CelTypeProvider;
3232
import dev.cel.common.types.SimpleType;
3333
import dev.cel.common.types.StructType;
34-
import dev.cel.expr.conformance.proto3.TestAllTypes;
3534
import java.util.Map;
3635
import java.util.Optional;
3736
import org.junit.Test;
@@ -185,7 +184,7 @@ public void evaluate_usingMultipleProviders_selectFieldFromCustomClass() throws
185184
.setValueProvider(
186185
CombinedCelValueProvider.combine(
187186
ProtoMessageValueProvider.newInstance(
188-
CelOptions.DEFAULT, DynamicProto.create(typeName -> Optional.empty())),
187+
CelOptions.DEFAULT, DynamicProto.create(unused -> Optional.empty())),
189188
CUSTOM_STRUCT_VALUE_PROVIDER))
190189
.build();
191190
CelAbstractSyntaxTree ast = cel.compile("custom_struct{data: 5}.data").getAst();
@@ -195,36 +194,8 @@ public void evaluate_usingMultipleProviders_selectFieldFromCustomClass() throws
195194
assertThat(result).isEqualTo(5L);
196195
}
197196

198-
@Test
199-
public void evaluate_usingMultipleProviders_selectFieldFromProtobufMessage() throws Exception {
200-
Cel cel =
201-
CelFactory.standardCelBuilder()
202-
.setOptions(CelOptions.current().enableCelValue(true).build())
203-
.addMessageTypes(TestAllTypes.getDescriptor())
204-
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
205-
.setValueProvider(
206-
CombinedCelValueProvider.combine(
207-
ProtoMessageValueProvider.newInstance(
208-
CelOptions.DEFAULT,
209-
// Note: this is unideal. Future iterations should make DynamicProto
210-
// completely an internal concern, and not expose it at all.
211-
DynamicProto.create(
212-
typeName -> {
213-
if (typeName.equals(TestAllTypes.getDescriptor().getFullName())) {
214-
return Optional.of(TestAllTypes.newBuilder());
215-
}
216-
return Optional.empty();
217-
})),
218-
CUSTOM_STRUCT_VALUE_PROVIDER))
219-
.build();
220-
CelAbstractSyntaxTree ast =
221-
cel.compile("cel.expr.conformance.proto3.TestAllTypes{single_string: 'foo'}.single_string")
222-
.getAst();
223-
224-
String result = (String) cel.createProgram(ast).eval();
225-
226-
assertThat(result).isEqualTo("foo");
227-
}
197+
// TODO: Bring back evaluate_usingMultipleProviders_selectFieldFromProtobufMessage
198+
// once planner is exposed from factory
228199

229200
@SuppressWarnings("Immutable") // Test only
230201
private static class CelCustomStructValue extends StructValue<String> {

runtime/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,12 @@ java_library(
271271
"//runtime/src/main/java/dev/cel/runtime:variable_resolver",
272272
],
273273
)
274+
275+
java_library(
276+
name = "runtime_planner_impl",
277+
testonly = 1, # TODO: Move to factory when ready for exposure
278+
visibility = ["//:internal"],
279+
exports = [
280+
"//runtime/src/main/java/dev/cel/runtime:runtime_planner_impl",
281+
],
282+
)

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,48 @@ cel_android_library(
801801
],
802802
)
803803

804+
java_library(
805+
name = "runtime_planner_impl",
806+
testonly = 1,
807+
srcs = ["CelRuntimeImpl.java"],
808+
tags = [
809+
],
810+
deps = [
811+
"//:auto_value",
812+
"//common:cel_ast",
813+
"//common:cel_descriptor_util",
814+
"//common:cel_descriptors",
815+
"//common:container",
816+
"//common:options",
817+
"//common/annotations",
818+
"//common/internal:cel_descriptor_pools",
819+
"//common/internal:default_message_factory",
820+
"//common/internal:dynamic_proto",
821+
"//common/types:default_type_provider",
822+
"//common/types:message_type_provider",
823+
"//common/types:type_providers",
824+
"//common/values",
825+
"//common/values:cel_value_provider",
826+
"//common/values:combined_cel_value_provider",
827+
"//common/values:proto_message_value_provider",
828+
"//runtime",
829+
"//runtime:dispatcher",
830+
"//runtime:evaluation_listener",
831+
"//runtime:function_binding",
832+
"//runtime:function_resolver",
833+
"//runtime:program",
834+
"//runtime:proto_message_runtime_helpers",
835+
"//runtime:runtime_equality",
836+
"//runtime:standard_functions",
837+
"//runtime:variable_resolver",
838+
"//runtime/planner:program_planner",
839+
"@maven//:com_google_errorprone_error_prone_annotations",
840+
"@maven//:com_google_guava_guava",
841+
"@maven//:com_google_protobuf_protobuf_java",
842+
"@maven//:org_jspecify_jspecify",
843+
],
844+
)
845+
804846
java_library(
805847
name = "runtime",
806848
srcs = RUNTIME_SOURCES,
@@ -829,13 +871,15 @@ java_library(
829871
"//common:cel_ast",
830872
"//common:cel_descriptor_util",
831873
"//common:cel_descriptors",
874+
"//common:container",
832875
"//common:options",
833876
"//common/annotations",
834877
"//common/internal:cel_descriptor_pools",
835878
"//common/internal:default_message_factory",
836879
"//common/internal:dynamic_proto",
837880
"//common/internal:proto_message_factory",
838881
"//common/types:cel_types",
882+
"//common/types:type_providers",
839883
"//common/values:cel_value_provider",
840884
"//common/values:proto_message_value_provider",
841885
"//runtime:variable_resolver",

runtime/src/main/java/dev/cel/runtime/CelRuntimeBuilder.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.google.protobuf.Descriptors.FileDescriptor;
2222
import com.google.protobuf.ExtensionRegistry;
2323
import com.google.protobuf.Message;
24+
import dev.cel.common.CelContainer;
2425
import dev.cel.common.CelOptions;
26+
import dev.cel.common.types.CelTypeProvider;
2527
import dev.cel.common.values.CelValueProvider;
2628
import java.util.function.Function;
2729

@@ -48,6 +50,14 @@ public interface CelRuntimeBuilder {
4850
@CanIgnoreReturnValue
4951
CelRuntimeBuilder addFunctionBindings(Iterable<CelFunctionBinding> bindings);
5052

53+
/** Adds bindings for functions that are allowed to be late-bound (resolved at execution time). */
54+
@CanIgnoreReturnValue
55+
CelRuntimeBuilder addLateBoundFunctions(String... lateBoundFunctionNames);
56+
57+
/** Adds bindings for functions that are allowed to be late-bound (resolved at execution time). */
58+
@CanIgnoreReturnValue
59+
CelRuntimeBuilder addLateBoundFunctions(Iterable<String> lateBoundFunctionNames);
60+
5161
/**
5262
* Add message {@link Descriptor}s to the builder for type-checking and object creation at
5363
* interpretation time.
@@ -123,6 +133,13 @@ public interface CelRuntimeBuilder {
123133
@CanIgnoreReturnValue
124134
CelRuntimeBuilder addFileTypes(FileDescriptorSet fileDescriptorSet);
125135

136+
/**
137+
* Sets the {@link CelTypeProvider} for resolving CEL types during evaluation, such as a fully
138+
* qualified type name to a struct or an enum value.
139+
*/
140+
@CanIgnoreReturnValue
141+
CelRuntimeBuilder setTypeProvider(CelTypeProvider celTypeProvider);
142+
126143
/**
127144
* Set a custom type factory for the runtime.
128145
*
@@ -145,7 +162,7 @@ public interface CelRuntimeBuilder {
145162
* support proto messages in addition to custom struct values, protobuf value provider must be
146163
* configured first before the custom value provider.
147164
*
148-
* <p>Note {@link CelOptions#enableCelValue()} must be enabled or this method will be a no-op.
165+
* <p>Note that this option is only supported for planner-based runtime.
149166
*/
150167
@CanIgnoreReturnValue
151168
CelRuntimeBuilder setValueProvider(CelValueProvider celValueProvider);
@@ -179,6 +196,15 @@ public interface CelRuntimeBuilder {
179196
@CanIgnoreReturnValue
180197
CelRuntimeBuilder setExtensionRegistry(ExtensionRegistry extensionRegistry);
181198

199+
200+
/**
201+
* Set the {@link CelContainer} to use as the namespace for resolving CEL expression variables and
202+
* functions.
203+
*/
204+
@CanIgnoreReturnValue
205+
CelRuntimeBuilder setContainer(CelContainer container);
206+
207+
182208
/** Build a new instance of the {@code CelRuntime}. */
183209
@CheckReturnValue
184210
CelRuntime build();

0 commit comments

Comments
 (0)