Skip to content

Commit

Permalink
Decouple RuntimeHelper/RuntimeEquality from full protobuf implementation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 726776440
  • Loading branch information
l46kok authored and copybara-github committed Feb 14, 2025
1 parent f619759 commit a99f209
Show file tree
Hide file tree
Showing 18 changed files with 1,165 additions and 971 deletions.
2 changes: 1 addition & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ java_library(
"//common/types",
"//compiler:compiler_builder",
"//runtime",
"//runtime:runtime_helper",
"//runtime:proto_message_runtime_equality",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLibrary;
import dev.cel.runtime.RuntimeEquality;
import dev.cel.runtime.ProtoMessageRuntimeEquality;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
Expand Down Expand Up @@ -66,9 +66,6 @@ public final class CelSetsExtensions implements CelCompilerLibrary, CelRuntimeLi
+ " are unique, so size does not factor into the computation. If either list is empty,"
+ " the result will be false.";

private static final RuntimeEquality RUNTIME_EQUALITY =
new RuntimeEquality(DynamicProto.create(DefaultMessageFactory.INSTANCE));

/** Denotes the set extension function. */
public enum Function {
CONTAINS(
Expand Down Expand Up @@ -111,15 +108,17 @@ String getFunction() {
}

private final ImmutableSet<Function> functions;
private final CelOptions celOptions;
private final ProtoMessageRuntimeEquality runtimeEquality;

CelSetsExtensions(CelOptions celOptions) {
this(celOptions, ImmutableSet.copyOf(Function.values()));
}

CelSetsExtensions(CelOptions celOptions, Set<Function> functions) {
this.functions = ImmutableSet.copyOf(functions);
this.celOptions = celOptions;
this.runtimeEquality =
ProtoMessageRuntimeEquality.create(
DynamicProto.create(DefaultMessageFactory.INSTANCE), celOptions);
}

@Override
Expand Down Expand Up @@ -208,7 +207,7 @@ private <T> boolean contains(Object o, Collection<T> list) {
}

private boolean objectsEquals(Object o1, Object o2) {
return RUNTIME_EQUALITY.objectEquals(o1, o2, celOptions);
return runtimeEquality.objectEquals(o1, o2);
}

private <T> boolean setIntersects(Collection<T> listA, Collection<T> listB) {
Expand Down
2 changes: 1 addition & 1 deletion publish/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUNTIME_TARGETS = [
"//runtime/src/main/java/dev/cel/runtime",
"//runtime/src/main/java/dev/cel/runtime:base",
"//runtime/src/main/java/dev/cel/runtime:interpreter",
"//runtime/src/main/java/dev/cel/runtime:runtime_helper",
"//runtime/src/main/java/dev/cel/runtime:runtime_helpers",
"//runtime/src/main/java/dev/cel/runtime:unknown_attributes",
]

Expand Down
22 changes: 20 additions & 2 deletions runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,27 @@ java_library(
)

java_library(
name = "runtime_helper",
name = "runtime_helpers",
visibility = ["//visibility:public"],
exports = ["//runtime/src/main/java/dev/cel/runtime:runtime_helper"],
exports = ["//runtime/src/main/java/dev/cel/runtime:runtime_helpers"],
)

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

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

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

java_library(
Expand Down
69 changes: 60 additions & 9 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ java_library(
":evaluation_exception_builder",
":evaluation_listener",
":metadata",
":runtime_helper",
":runtime_helpers",
":unknown_attributes",
"//:auto_value",
"//common",
Expand All @@ -105,31 +105,80 @@ java_library(
)

java_library(
name = "runtime_helper",
name = "runtime_equality",
srcs = [
"RuntimeEquality.java",
"RuntimeHelpers.java",
],
tags = [
],
# NOTE: do not grow this dependencies arbitrarily
deps = [
":runtime_helpers",
"//common:error_codes",
"//common:options",
"//common:runtime_exception",
"//common/annotations",
"//common/internal:comparison_functions",
"//common/internal:converter",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

java_library(
name = "proto_message_runtime_equality",
srcs = [
"ProtoMessageRuntimeEquality.java",
],
tags = [
],
deps = [
":proto_message_runtime_helpers",
":runtime_equality",
"//common:options",
"//common/annotations",
"//common/internal:dynamic_proto",
"//common/internal:proto_equality",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

java_library(
name = "runtime_helpers",
srcs = [
"RuntimeHelpers.java",
],
tags = [
],
deps = [
"//common:error_codes",
"//common:options",
"//common:runtime_exception",
"//common/internal:converter",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_re2j_re2j",
"@maven//:org_threeten_threeten_extra",
],
)

java_library(
name = "proto_message_runtime_helpers",
srcs = [
"ProtoMessageRuntimeHelpers.java",
],
tags = [
],
deps = [
":runtime_helpers",
"//common:options",
"//common/annotations",
"//common/internal:dynamic_proto",
"@maven//:com_google_protobuf_protobuf_java",
],
)

# keep sorted
RUNTIME_SOURCES = [
"CelFunctionOverload.java",
Expand Down Expand Up @@ -196,7 +245,10 @@ java_library(
deps = [
":evaluation_exception",
":evaluation_listener",
":runtime_helper",
":interpreter",
":proto_message_runtime_equality",
":runtime_equality",
":runtime_helpers",
":runtime_type_provider_legacy",
":unknown_attributes",
"//:auto_value",
Expand All @@ -214,7 +266,6 @@ java_library(
"//common/types:cel_types",
"//common/values:cel_value_provider",
"//common/values:proto_message_value_provider",
"//runtime:interpreter",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down Expand Up @@ -271,6 +322,7 @@ java_library(
name = "runtime_type_provider_legacy",
srcs = ["RuntimeTypeProviderLegacyImpl.java"],
deps = [
":interpreter",
":unknown_attributes",
"//common:error_codes",
"//common:options",
Expand All @@ -282,7 +334,6 @@ java_library(
"//common/values:cel_value",
"//common/values:cel_value_provider",
"//common/values:proto_message_value",
"//runtime:interpreter",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ public CelRuntimeLegacyImpl build() {
runtimeTypeFactory, DefaultMessageFactory.create(celDescriptorPool));

DynamicProto dynamicProto = DynamicProto.create(runtimeTypeFactory);
RuntimeEquality runtimeEquality = ProtoMessageRuntimeEquality.create(dynamicProto, options);

ImmutableMap.Builder<String, CelFunctionBinding> functionBindingsBuilder =
ImmutableMap.builder();
for (CelFunctionBinding standardFunctionBinding : newStandardFunctionBindings(dynamicProto)) {
for (CelFunctionBinding standardFunctionBinding :
newStandardFunctionBindings(runtimeEquality)) {
functionBindingsBuilder.put(
standardFunctionBinding.getOverloadId(), standardFunctionBinding);
}
Expand Down Expand Up @@ -283,7 +285,7 @@ public CelRuntimeLegacyImpl build() {
}

private ImmutableSet<CelFunctionBinding> newStandardFunctionBindings(
DynamicProto dynamicProto) {
RuntimeEquality runtimeEquality) {
CelStandardFunctions celStandardFunctions;
if (standardEnvironmentEnabled) {
celStandardFunctions =
Expand Down Expand Up @@ -336,7 +338,7 @@ private ImmutableSet<CelFunctionBinding> newStandardFunctionBindings(
return ImmutableSet.of();
}

return celStandardFunctions.newFunctionBindings(dynamicProto, options);
return celStandardFunctions.newFunctionBindings(runtimeEquality, options);
}

private static CelDescriptorPool newDescriptorPool(
Expand Down
Loading

0 comments on commit a99f209

Please sign in to comment.