Skip to content

Commit 87ccdfe

Browse files
l46kokcopybara-github
authored andcommitted
Decouple CelTypeResolver from full protobuf implementation
PiperOrigin-RevId: 725790812
1 parent f619759 commit 87ccdfe

22 files changed

Lines changed: 1400 additions & 1004 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ java_library(
147147
"//common/types",
148148
"//compiler:compiler_builder",
149149
"//runtime",
150-
"//runtime:runtime_helper",
150+
"//runtime:proto_message_runtime_equality",
151151
"@maven//:com_google_errorprone_error_prone_annotations",
152152
"@maven//:com_google_guava_guava",
153153
],

extensions/src/main/java/dev/cel/extensions/CelSetsExtensions.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import dev.cel.runtime.CelRuntime;
3030
import dev.cel.runtime.CelRuntimeBuilder;
3131
import dev.cel.runtime.CelRuntimeLibrary;
32-
import dev.cel.runtime.RuntimeEquality;
32+
import dev.cel.runtime.ProtoMessageRuntimeEquality;
3333
import java.util.Collection;
3434
import java.util.Iterator;
3535
import java.util.Set;
@@ -66,9 +66,6 @@ public final class CelSetsExtensions implements CelCompilerLibrary, CelRuntimeLi
6666
+ " are unique, so size does not factor into the computation. If either list is empty,"
6767
+ " the result will be false.";
6868

69-
private static final RuntimeEquality RUNTIME_EQUALITY =
70-
new RuntimeEquality(DynamicProto.create(DefaultMessageFactory.INSTANCE));
71-
7269
/** Denotes the set extension function. */
7370
public enum Function {
7471
CONTAINS(
@@ -111,15 +108,17 @@ String getFunction() {
111108
}
112109

113110
private final ImmutableSet<Function> functions;
114-
private final CelOptions celOptions;
111+
private final ProtoMessageRuntimeEquality runtimeEquality;
115112

116113
CelSetsExtensions(CelOptions celOptions) {
117114
this(celOptions, ImmutableSet.copyOf(Function.values()));
118115
}
119116

120117
CelSetsExtensions(CelOptions celOptions, Set<Function> functions) {
121118
this.functions = ImmutableSet.copyOf(functions);
122-
this.celOptions = celOptions;
119+
this.runtimeEquality =
120+
ProtoMessageRuntimeEquality.create(
121+
DynamicProto.create(DefaultMessageFactory.INSTANCE), celOptions);
123122
}
124123

125124
@Override
@@ -208,7 +207,7 @@ private <T> boolean contains(Object o, Collection<T> list) {
208207
}
209208

210209
private boolean objectsEquals(Object o1, Object o2) {
211-
return RUNTIME_EQUALITY.objectEquals(o1, o2, celOptions);
210+
return runtimeEquality.objectEquals(o1, o2);
212211
}
213212

214213
private <T> boolean setIntersects(Collection<T> listA, Collection<T> listB) {

publish/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUNTIME_TARGETS = [
99
"//runtime/src/main/java/dev/cel/runtime",
1010
"//runtime/src/main/java/dev/cel/runtime:base",
1111
"//runtime/src/main/java/dev/cel/runtime:interpreter",
12-
"//runtime/src/main/java/dev/cel/runtime:runtime_helper",
12+
"//runtime/src/main/java/dev/cel/runtime:runtime_helpers",
1313
"//runtime/src/main/java/dev/cel/runtime:unknown_attributes",
1414
]
1515

runtime/BUILD.bazel

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,33 @@ java_library(
2525
)
2626

2727
java_library(
28-
name = "runtime_helper",
28+
name = "runtime_helpers",
2929
visibility = ["//visibility:public"],
30-
exports = ["//runtime/src/main/java/dev/cel/runtime:runtime_helper"],
30+
exports = ["//runtime/src/main/java/dev/cel/runtime:runtime_helpers"],
31+
)
32+
33+
java_library(
34+
name = "proto_message_runtime_helpers",
35+
visibility = ["//visibility:public"],
36+
exports = ["//runtime/src/main/java/dev/cel/runtime:proto_message_runtime_helpers"],
37+
)
38+
39+
java_library(
40+
name = "runtime_equality",
41+
visibility = ["//visibility:public"],
42+
exports = ["//runtime/src/main/java/dev/cel/runtime:runtime_equality"],
43+
)
44+
45+
java_library(
46+
name = "proto_message_runtime_equality",
47+
visibility = ["//visibility:public"],
48+
exports = ["//runtime/src/main/java/dev/cel/runtime:proto_message_runtime_equality"],
49+
)
50+
51+
java_library(
52+
name = "type_resolver",
53+
visibility = ["//visibility:public"],
54+
exports = ["//runtime/src/main/java/dev/cel/runtime:type_resolver"],
3155
)
3256

3357
java_library(

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

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,25 @@ INTERPRETER_SOURCES = [
3737
]
3838

3939
java_library(
40-
name = "cel_type_resolver",
41-
srcs = ["CelTypeResolver.java"],
40+
name = "type_resolver",
41+
srcs = ["TypeResolver.java"],
42+
tags = [
43+
],
44+
deps = [
45+
"//common/types",
46+
"//common/types:type_providers",
47+
"@maven//:com_google_errorprone_error_prone_annotations",
48+
"@maven//:com_google_guava_guava",
49+
"@maven//:com_google_protobuf_protobuf_java",
50+
],
51+
)
52+
53+
java_library(
54+
name = "descriptor_type_resolver",
55+
srcs = ["DescriptorTypeResolver.java"],
56+
visibility = ["//visibility:private"],
4257
deps = [
58+
":type_resolver",
4359
"//common/types",
4460
"//common/types:type_providers",
4561
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -56,8 +72,7 @@ java_library(
5672
deps = [
5773
":evaluation_exception",
5874
":metadata",
59-
"//:auto_value",
60-
"//common",
75+
"//common:cel_ast",
6176
"//common/annotations",
6277
"@maven//:com_google_code_findbugs_annotations",
6378
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -75,12 +90,12 @@ java_library(
7590
exports = [":base"],
7691
deps = [
7792
":base",
78-
":cel_type_resolver",
7993
":evaluation_exception",
8094
":evaluation_exception_builder",
8195
":evaluation_listener",
8296
":metadata",
83-
":runtime_helper",
97+
":runtime_helpers",
98+
":type_resolver",
8499
":unknown_attributes",
85100
"//:auto_value",
86101
"//common",
@@ -105,31 +120,80 @@ java_library(
105120
)
106121

107122
java_library(
108-
name = "runtime_helper",
123+
name = "runtime_equality",
109124
srcs = [
110125
"RuntimeEquality.java",
111-
"RuntimeHelpers.java",
112126
],
113127
tags = [
114128
],
115-
# NOTE: do not grow this dependencies arbitrarily
116129
deps = [
130+
":runtime_helpers",
117131
"//common:error_codes",
118132
"//common:options",
119133
"//common:runtime_exception",
120-
"//common/annotations",
121134
"//common/internal:comparison_functions",
122-
"//common/internal:converter",
135+
"@maven//:com_google_errorprone_error_prone_annotations",
136+
"@maven//:com_google_guava_guava",
137+
"@maven//:com_google_protobuf_protobuf_java",
138+
],
139+
)
140+
141+
java_library(
142+
name = "proto_message_runtime_equality",
143+
srcs = [
144+
"ProtoMessageRuntimeEquality.java",
145+
],
146+
tags = [
147+
],
148+
deps = [
149+
":proto_message_runtime_helpers",
150+
":runtime_equality",
151+
"//common:options",
152+
"//common/annotations",
123153
"//common/internal:dynamic_proto",
124154
"//common/internal:proto_equality",
125155
"@maven//:com_google_errorprone_error_prone_annotations",
126156
"@maven//:com_google_guava_guava",
127157
"@maven//:com_google_protobuf_protobuf_java",
158+
],
159+
)
160+
161+
java_library(
162+
name = "runtime_helpers",
163+
srcs = [
164+
"RuntimeHelpers.java",
165+
],
166+
tags = [
167+
],
168+
deps = [
169+
"//common:error_codes",
170+
"//common:options",
171+
"//common:runtime_exception",
172+
"//common/internal:converter",
173+
"@maven//:com_google_errorprone_error_prone_annotations",
174+
"@maven//:com_google_guava_guava",
175+
"@maven//:com_google_protobuf_protobuf_java",
128176
"@maven//:com_google_re2j_re2j",
129177
"@maven//:org_threeten_threeten_extra",
130178
],
131179
)
132180

181+
java_library(
182+
name = "proto_message_runtime_helpers",
183+
srcs = [
184+
"ProtoMessageRuntimeHelpers.java",
185+
],
186+
tags = [
187+
],
188+
deps = [
189+
":runtime_helpers",
190+
"//common:options",
191+
"//common/annotations",
192+
"//common/internal:dynamic_proto",
193+
"@maven//:com_google_protobuf_protobuf_java",
194+
],
195+
)
196+
133197
# keep sorted
134198
RUNTIME_SOURCES = [
135199
"CelFunctionOverload.java",
@@ -194,9 +258,13 @@ java_library(
194258
tags = [
195259
],
196260
deps = [
261+
":descriptor_type_resolver",
197262
":evaluation_exception",
198263
":evaluation_listener",
199-
":runtime_helper",
264+
":interpreter",
265+
":proto_message_runtime_equality",
266+
":runtime_equality",
267+
":runtime_helpers",
200268
":runtime_type_provider_legacy",
201269
":unknown_attributes",
202270
"//:auto_value",
@@ -214,7 +282,6 @@ java_library(
214282
"//common/types:cel_types",
215283
"//common/values:cel_value_provider",
216284
"//common/values:proto_message_value_provider",
217-
"//runtime:interpreter",
218285
"@maven//:com_google_code_findbugs_annotations",
219286
"@maven//:com_google_errorprone_error_prone_annotations",
220287
"@maven//:com_google_guava_guava",
@@ -271,6 +338,7 @@ java_library(
271338
name = "runtime_type_provider_legacy",
272339
srcs = ["RuntimeTypeProviderLegacyImpl.java"],
273340
deps = [
341+
":interpreter",
274342
":unknown_attributes",
275343
"//common:error_codes",
276344
"//common:options",
@@ -282,7 +350,6 @@ java_library(
282350
"//common/values:cel_value",
283351
"//common/values:cel_value_provider",
284352
"//common/values:proto_message_value",
285-
"//runtime:interpreter",
286353
"@maven//:com_google_errorprone_error_prone_annotations",
287354
"@maven//:com_google_guava_guava",
288355
],

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,12 @@ public CelRuntimeLegacyImpl build() {
241241
runtimeTypeFactory, DefaultMessageFactory.create(celDescriptorPool));
242242

243243
DynamicProto dynamicProto = DynamicProto.create(runtimeTypeFactory);
244+
RuntimeEquality runtimeEquality = ProtoMessageRuntimeEquality.create(dynamicProto, options);
244245

245246
ImmutableMap.Builder<String, CelFunctionBinding> functionBindingsBuilder =
246247
ImmutableMap.builder();
247-
for (CelFunctionBinding standardFunctionBinding : newStandardFunctionBindings(dynamicProto)) {
248+
for (CelFunctionBinding standardFunctionBinding :
249+
newStandardFunctionBindings(runtimeEquality)) {
248250
functionBindingsBuilder.put(
249251
standardFunctionBinding.getOverloadId(), standardFunctionBinding);
250252
}
@@ -277,13 +279,17 @@ public CelRuntimeLegacyImpl build() {
277279
}
278280

279281
return new CelRuntimeLegacyImpl(
280-
new DefaultInterpreter(runtimeTypeProvider, dispatcher.immutableCopy(), options),
282+
new DefaultInterpreter(
283+
DescriptorTypeResolver.create(),
284+
runtimeTypeProvider,
285+
dispatcher.immutableCopy(),
286+
options),
281287
options,
282288
this);
283289
}
284290

285291
private ImmutableSet<CelFunctionBinding> newStandardFunctionBindings(
286-
DynamicProto dynamicProto) {
292+
RuntimeEquality runtimeEquality) {
287293
CelStandardFunctions celStandardFunctions;
288294
if (standardEnvironmentEnabled) {
289295
celStandardFunctions =
@@ -336,7 +342,7 @@ private ImmutableSet<CelFunctionBinding> newStandardFunctionBindings(
336342
return ImmutableSet.of();
337343
}
338344

339-
return celStandardFunctions.newFunctionBindings(dynamicProto, options);
345+
return celStandardFunctions.newFunctionBindings(runtimeEquality, options);
340346
}
341347

342348
private static CelDescriptorPool newDescriptorPool(

0 commit comments

Comments
 (0)