Skip to content

Commit f5add50

Browse files
l46kokcopybara-github
authored andcommitted
Add new interfaces for ProgramPlanner, plan constants
PiperOrigin-RevId: 827675090
1 parent c0dbe7e commit f5add50

File tree

18 files changed

+594
-38
lines changed

18 files changed

+594
-38
lines changed

common/src/main/java/dev/cel/common/ast/CelConstant.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,26 @@ public static CelConstant ofObjectValue(Object value) {
207207

208208
throw new IllegalArgumentException("Value is not a CelConstant: " + value);
209209
}
210+
211+
/** Gets the underlying value held by this constant. */
212+
public Object objectValue() {
213+
switch (getKind()) {
214+
case NULL_VALUE:
215+
return nullValue();
216+
case BOOLEAN_VALUE:
217+
return booleanValue();
218+
case INT64_VALUE:
219+
return int64Value();
220+
case UINT64_VALUE:
221+
return uint64Value();
222+
case DOUBLE_VALUE:
223+
return doubleValue();
224+
case STRING_VALUE:
225+
return stringValue();
226+
case BYTES_VALUE:
227+
return bytesValue();
228+
default:
229+
throw new IllegalStateException("Unsupported kind: " + getKind());
230+
}
231+
}
210232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3434
@Internal
3535
@Immutable
36-
abstract class CelValueConverter {
36+
public abstract class CelValueConverter {
3737

3838
/** Adapts a {@link CelValue} to a plain old Java Object. */
3939
public Object fromCelValueToJavaObject(CelValue celValue) {

runtime/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,14 @@ cel_android_library(
244244
visibility = ["//:internal"],
245245
exports = ["//runtime/src/main/java/dev/cel/runtime:internal_function_binder_andriod"],
246246
)
247+
248+
java_library(
249+
name = "program",
250+
visibility = ["//:internal"],
251+
exports = ["//runtime/src/main/java/dev/cel/runtime:program"],
252+
)
253+
254+
cel_android_library(
255+
name = "program_android",
256+
exports = ["//runtime/src/main/java/dev/cel/runtime:program_android"],
257+
)

runtime/planner/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_applicable_licenses = ["//:license"],
5+
default_visibility = ["//:internal"],
6+
)
7+
8+
java_library(
9+
name = "program_planner",
10+
exports = ["//runtime/src/main/java/dev/cel/runtime/planner:program_planner"],
11+
)

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

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ LITE_RUNTIME_SOURCES = [
4141

4242
# keep sorted
4343
LITE_RUNTIME_IMPL_SOURCES = [
44-
"LiteProgramImpl.java",
4544
"LiteRuntimeImpl.java",
4645
]
4746

47+
# keep sorted
48+
LITE_PROGRAM_IMPL_SOURCES = [
49+
"LiteProgramImpl.java",
50+
]
51+
4852
# keep sorted
4953
FUNCTION_BINDING_SOURCES = [
5054
"CelFunctionBinding.java",
@@ -833,7 +837,6 @@ java_library(
833837
":function_resolver",
834838
":interpretable",
835839
":interpreter",
836-
":lite_runtime",
837840
":proto_message_activation_factory",
838841
":proto_message_runtime_equality",
839842
":runtime_equality",
@@ -853,6 +856,7 @@ java_library(
853856
"//common/types:cel_types",
854857
"//common/values:cel_value_provider",
855858
"//common/values:proto_message_value_provider",
859+
"//runtime:program",
856860
"@maven//:com_google_code_findbugs_annotations",
857861
"@maven//:com_google_errorprone_error_prone_annotations",
858862
"@maven//:com_google_guava_guava",
@@ -869,12 +873,12 @@ java_library(
869873
deps = [
870874
":evaluation_exception",
871875
":function_binding",
872-
":function_resolver",
873876
"//:auto_value",
874877
"//common:cel_ast",
875878
"//common:options",
876879
"//common/annotations",
877880
"//common/values:cel_value_provider",
881+
"//runtime:program",
878882
"//runtime/standard:standard_function",
879883
"@maven//:com_google_code_findbugs_annotations",
880884
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -888,14 +892,11 @@ java_library(
888892
tags = [
889893
],
890894
deps = [
891-
":activation",
892895
":cel_value_runtime_type_provider",
893896
":dispatcher",
894-
":evaluation_exception",
895897
":function_binding",
896-
":function_resolver",
897-
":interpretable",
898898
":interpreter",
899+
":lite_program_impl",
899900
":lite_runtime",
900901
":runtime_equality",
901902
":runtime_helpers",
@@ -904,28 +905,58 @@ java_library(
904905
"//common:cel_ast",
905906
"//common:options",
906907
"//common/values:cel_value_provider",
908+
"//runtime:program",
907909
"//runtime/standard:standard_function",
908910
"@maven//:com_google_code_findbugs_annotations",
909-
"@maven//:com_google_errorprone_error_prone_annotations",
910911
"@maven//:com_google_guava_guava",
911912
],
912913
)
913914

915+
java_library(
916+
name = "lite_program_impl",
917+
srcs = LITE_PROGRAM_IMPL_SOURCES,
918+
tags = [
919+
],
920+
deps = [
921+
":activation",
922+
":evaluation_exception",
923+
":function_resolver",
924+
":interpretable",
925+
":program",
926+
"//:auto_value",
927+
"@maven//:com_google_errorprone_error_prone_annotations",
928+
],
929+
)
930+
931+
cel_android_library(
932+
name = "lite_program_impl_android",
933+
srcs = LITE_PROGRAM_IMPL_SOURCES,
934+
tags = [
935+
],
936+
deps = [
937+
":activation_android",
938+
":evaluation_exception",
939+
":function_resolver_android",
940+
":interpretable_android",
941+
":program_android",
942+
"//:auto_value",
943+
"@maven//:com_google_errorprone_error_prone_annotations",
944+
],
945+
)
946+
914947
cel_android_library(
915948
name = "lite_runtime_impl_android",
916949
srcs = LITE_RUNTIME_IMPL_SOURCES,
917950
tags = [
918951
],
919952
deps = [
920-
":activation_android",
921953
":cel_value_runtime_type_provider_android",
922954
":dispatcher_android",
923-
":evaluation_exception",
924955
":function_binding_android",
925-
":function_resolver_android",
926-
":interpretable_android",
927956
":interpreter_android",
957+
":lite_program_impl_android",
928958
":lite_runtime_android",
959+
":program_android",
929960
":runtime_equality_android",
930961
":runtime_helpers_android",
931962
":type_resolver_android",
@@ -1127,7 +1158,7 @@ cel_android_library(
11271158
deps = [
11281159
":evaluation_exception",
11291160
":function_binding_android",
1130-
":function_resolver_android",
1161+
":program_android",
11311162
"//:auto_value",
11321163
"//common:cel_ast_android",
11331164
"//common:options",
@@ -1195,6 +1226,31 @@ cel_android_library(
11951226
],
11961227
)
11971228

1229+
java_library(
1230+
name = "program",
1231+
srcs = ["Program.java"],
1232+
tags = [
1233+
],
1234+
deps = [
1235+
":evaluation_exception",
1236+
":function_resolver",
1237+
"@maven//:com_google_errorprone_error_prone_annotations",
1238+
],
1239+
)
1240+
1241+
cel_android_library(
1242+
name = "program_android",
1243+
srcs = ["Program.java"],
1244+
tags = [
1245+
],
1246+
deps = [
1247+
":evaluation_exception",
1248+
":function_resolver_android",
1249+
"//:auto_value",
1250+
"@maven//:com_google_errorprone_error_prone_annotations",
1251+
],
1252+
)
1253+
11981254
java_library(
11991255
name = "internal_function_binder",
12001256
srcs = ["InternalFunctionBinder.java"],

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414

1515
package dev.cel.runtime;
1616

17-
import com.google.errorprone.annotations.Immutable;
1817
import javax.annotation.concurrent.ThreadSafe;
1918
import dev.cel.common.CelAbstractSyntaxTree;
2019
import dev.cel.common.annotations.Beta;
21-
import java.util.Map;
2220

2321
/**
2422
* CelLiteRuntime creates executable {@link Program} instances from {@link CelAbstractSyntaxTree}
@@ -34,22 +32,4 @@ public interface CelLiteRuntime {
3432
Program createProgram(CelAbstractSyntaxTree ast) throws CelEvaluationException;
3533

3634
CelLiteRuntimeBuilder toRuntimeBuilder();
37-
38-
/** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
39-
@Immutable
40-
interface Program {
41-
42-
/** Evaluate the expression without any variables. */
43-
Object eval() throws CelEvaluationException;
44-
45-
/** Evaluate the expression using a {@code mapValue} as the source of input variables. */
46-
Object eval(Map<String, ?> mapValue) throws CelEvaluationException;
47-
48-
/**
49-
* Evaluate a compiled program with {@code mapValue} and late-bound functions {@code
50-
* lateBoundFunctionResolver}.
51-
*/
52-
Object eval(Map<String, ?> mapValue, CelFunctionResolver lateBoundFunctionResolver)
53-
throws CelEvaluationException;
54-
}
5535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface CelRuntime {
3737

3838
/** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
3939
@Immutable
40-
interface Program extends CelLiteRuntime.Program {
40+
interface Program extends dev.cel.runtime.Program {
4141

4242
/** Evaluate the expression using {@code message} fields as the source of input variables. */
4343
Object eval(Message message) throws CelEvaluationException;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@Immutable
2222
@AutoValue
23-
abstract class LiteProgramImpl implements CelLiteRuntime.Program {
23+
abstract class LiteProgramImpl implements Program {
2424

2525
abstract Interpretable interpretable();
2626

@@ -40,7 +40,7 @@ public Object eval(Map<String, ?> mapValue, CelFunctionResolver lateBoundFunctio
4040
return interpretable().eval(Activation.copyOf(mapValue), lateBoundFunctionResolver);
4141
}
4242

43-
static CelLiteRuntime.Program plan(Interpretable interpretable) {
43+
static Program plan(Interpretable interpretable) {
4444
return new AutoValue_LiteProgramImpl(interpretable);
4545
}
4646
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.runtime;
16+
17+
import com.google.errorprone.annotations.Immutable;
18+
import java.util.Map;
19+
20+
/** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
21+
@Immutable
22+
public interface Program {
23+
24+
/** Evaluate the expression without any variables. */
25+
Object eval() throws CelEvaluationException;
26+
27+
/** Evaluate the expression using a {@code mapValue} as the source of input variables. */
28+
Object eval(Map<String, ?> mapValue) throws CelEvaluationException;
29+
30+
/**
31+
* Evaluate a compiled program with {@code mapValue} and late-bound functions {@code
32+
* lateBoundFunctionResolver}.
33+
*/
34+
Object eval(Map<String, ?> mapValue, CelFunctionResolver lateBoundFunctionResolver)
35+
throws CelEvaluationException;
36+
}

0 commit comments

Comments
 (0)