Skip to content

Commit bb6a118

Browse files
l46kokcopybara-github
authored andcommitted
Modernize Env and ExprChecker to use CelTypeProvider
PiperOrigin-RevId: 983383517
1 parent b4977d9 commit bb6a118

9 files changed

Lines changed: 689 additions & 180 deletions

File tree

checker/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_java//java:defs.bzl", "java_library")
2+
load("//:cel_android_rules.bzl", "cel_android_library")
23

34
package(
45
default_applicable_licenses = ["//:license"],
@@ -35,7 +36,10 @@ java_library(
3536
java_library(
3637
name = "checker_legacy_environment",
3738
deprecation = "See go/cel-java-migration-guide. Please use CEL-Java Fluent APIs //compiler instead",
38-
exports = ["//checker/src/main/java/dev/cel/checker:checker_legacy_environment"],
39+
exports = [
40+
":type_provider_legacy",
41+
"//checker/src/main/java/dev/cel/checker:checker_legacy_environment",
42+
],
3943
)
4044

4145
java_library(
@@ -53,3 +57,8 @@ java_library(
5357
name = "standard_decl",
5458
exports = ["//checker/src/main/java/dev/cel/checker:standard_decl"],
5559
)
60+
61+
cel_android_library(
62+
name = "standard_decl_android",
63+
exports = ["//checker/src/main/java/dev/cel/checker:standard_decl_android"],
64+
)

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_java//java:defs.bzl", "java_library")
2+
load("//:cel_android_rules.bzl", "cel_android_library")
23

34
package(
45
default_applicable_licenses = [
@@ -25,14 +26,11 @@ CHECKER_BUILDER_SOURCES = [
2526

2627
# keep sorted
2728
CHECKER_LEGACY_ENV_SOURCES = [
28-
"DescriptorTypeProvider.java",
2929
"Env.java",
3030
"ExprChecker.java",
3131
"ExprVisitor.java",
3232
"InferenceContext.java",
3333
"TypeFormatter.java",
34-
"TypeProvider.java",
35-
"Types.java",
3634
]
3735

3836
java_library(
@@ -69,7 +67,7 @@ java_library(
6967
":checker_legacy_environment",
7068
":proto_type_mask",
7169
":standard_decl",
72-
":type_provider_legacy_impl",
70+
":type_provider_legacy",
7371
"//common:cel_ast",
7472
"//common:cel_descriptor_util",
7573
"//common:cel_function_decl",
@@ -102,9 +100,9 @@ java_library(
102100
tags = [
103101
],
104102
deps = [
105-
":checker_legacy_environment",
106103
":proto_type_mask",
107104
":standard_decl",
105+
":type_provider_legacy",
108106
"//common:cel_ast",
109107
"//common:cel_function_decl",
110108
"//common:cel_validation_result",
@@ -137,7 +135,7 @@ java_library(
137135
tags = [
138136
],
139137
deps = [
140-
":checker_legacy_environment",
138+
":type_provider_legacy",
141139
"//common/annotations",
142140
"//common/types",
143141
"//common/types:cel_proto_types",
@@ -156,6 +154,7 @@ java_library(
156154
],
157155
deps = [
158156
":standard_decl",
157+
":type_provider_legacy",
159158
"//:auto_value",
160159
"//common:cel_ast",
161160
"//common:cel_function_decl",
@@ -173,7 +172,6 @@ java_library(
173172
"//common/ast:expr_converter",
174173
"//common/ast:mutable_expr",
175174
"//common/internal:errors",
176-
"//common/internal:file_descriptor_converter",
177175
"//common/types",
178176
"//common/types:cel_proto_types",
179177
"//common/types:cel_types",
@@ -183,7 +181,6 @@ java_library(
183181
"@cel_spec//proto/cel/expr:syntax_java_proto",
184182
"@maven//:com_google_errorprone_error_prone_annotations",
185183
"@maven//:com_google_guava_guava",
186-
"@maven//:com_google_protobuf_protobuf_java",
187184
"@maven//:org_jspecify_jspecify",
188185
],
189186
)
@@ -235,3 +232,23 @@ java_library(
235232
"@maven//:com_google_guava_guava",
236233
],
237234
)
235+
236+
cel_android_library(
237+
name = "standard_decl_android",
238+
srcs = [
239+
"CelStandardDeclarations.java",
240+
],
241+
tags = [
242+
],
243+
deps = [
244+
"//common:cel_function_decl_android",
245+
"//common:cel_overload_decl_android",
246+
"//common:cel_var_decl_android",
247+
"//common:operator_android",
248+
"//common/types:cel_types_android",
249+
"//common/types:type_providers_android",
250+
"//common/types:types_android",
251+
"@maven//:com_google_errorprone_error_prone_annotations",
252+
"@maven_android//:com_google_guava_guava",
253+
],
254+
)

checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
7474
private final Optional<CelType> expectedResultType;
7575

7676
@SuppressWarnings("Immutable")
77-
private final TypeProvider typeProvider;
77+
private final @Nullable TypeProvider typeProvider;
7878

7979
private final CelTypeProvider celTypeProvider;
8080
private final boolean standardEnvironmentEnabled;
@@ -124,6 +124,10 @@ public CelCheckerBuilder toCheckerBuilder() {
124124
.addFileTypes(fileDescriptors)
125125
.addProtoTypeMasks(protoTypeMasks);
126126

127+
if (typeProvider != null) {
128+
builder.setTypeProvider(typeProvider);
129+
}
130+
127131
if (expectedResultType.isPresent()) {
128132
builder.setResultType(expectedResultType.get());
129133
}
@@ -162,11 +166,13 @@ public void accept(EnvVisitor envVisitor) {
162166
private Env getEnv(Errors errors) {
163167
Env env;
164168
if (overriddenStandardDeclarations != null) {
165-
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
169+
env =
170+
Env.standard(
171+
overriddenStandardDeclarations, errors, celTypeProvider, typeProvider, celOptions);
166172
} else if (standardEnvironmentEnabled) {
167-
env = Env.standard(errors, typeProvider, celOptions);
173+
env = Env.standard(errors, celTypeProvider, typeProvider, celOptions);
168174
} else {
169-
env = Env.unconfigured(errors, typeProvider, celOptions);
175+
env = Env.unconfigured(errors, celTypeProvider, typeProvider, celOptions);
170176
}
171177
identDeclarations.forEach(env::add);
172178
functionDeclarations.forEach(env::add);
@@ -190,7 +196,7 @@ public static final class Builder implements CelCheckerBuilder {
190196
private CelContainer container;
191197
private CelOptions celOptions;
192198
private CelType expectedResultType;
193-
private TypeProvider customTypeProvider;
199+
private @Nullable TypeProvider customTypeProvider;
194200
private CelTypeProvider celTypeProvider;
195201
private boolean standardEnvironmentEnabled;
196202
private CelStandardDeclarations standardDeclarations;
@@ -400,6 +406,11 @@ CelStandardDeclarations standardDeclarations() {
400406
return this.standardDeclarations;
401407
}
402408

409+
@VisibleForTesting
410+
@Nullable TypeProvider customTypeProvider() {
411+
return this.customTypeProvider;
412+
}
413+
403414
@VisibleForTesting
404415
CelTypeProvider celTypeProvider() {
405416
return this.celTypeProvider;
@@ -459,20 +470,13 @@ public CelCheckerLegacyImpl build() {
459470
messageTypeProvider = protoTypeMaskTypeProvider;
460471
}
461472

462-
TypeProvider legacyProvider = new TypeProviderLegacyImpl(messageTypeProvider);
463-
if (customTypeProvider != null) {
464-
legacyProvider =
465-
new TypeProvider.CombinedTypeProvider(
466-
ImmutableList.of(customTypeProvider, legacyProvider));
467-
}
468-
469473
return new CelCheckerLegacyImpl(
470474
celOptions,
471475
container,
472476
identDeclarationSet,
473477
functionDeclarations.build(),
474478
Optional.fromNullable(expectedResultType),
475-
legacyProvider,
479+
customTypeProvider,
476480
messageTypeProvider,
477481
standardEnvironmentEnabled,
478482
standardDeclarations,
@@ -499,7 +503,7 @@ private CelCheckerLegacyImpl(
499503
ImmutableSet<CelVarDecl> identDeclarations,
500504
ImmutableSet<CelFunctionDecl> functionDeclarations,
501505
Optional<CelType> expectedResultType,
502-
TypeProvider typeProvider,
506+
@Nullable TypeProvider typeProvider,
503507
CelTypeProvider celTypeProvider,
504508
boolean standardEnvironmentEnabled,
505509
@Nullable CelStandardDeclarations overriddenStandardDeclarations,

0 commit comments

Comments
 (0)