Skip to content

Commit 6d822ef

Browse files
l46kokcopybara-github
authored andcommitted
Plan identifiers
PiperOrigin-RevId: 828205454
1 parent 2eed0fe commit 6d822ef

File tree

17 files changed

+920
-2
lines changed

17 files changed

+920
-2
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/types/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ java_library(
183183
],
184184
)
185185

186+
java_library(
187+
name = "default_type_provider",
188+
srcs = [
189+
"DefaultTypeProvider.java",
190+
],
191+
tags = [
192+
],
193+
deps = [
194+
":type_providers",
195+
":types",
196+
"@maven//:com_google_guava_guava",
197+
],
198+
)
199+
186200
cel_android_library(
187201
name = "cel_types_android",
188202
srcs = ["CelTypes.java"],
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.common.types;
16+
17+
import com.google.common.collect.ImmutableCollection;
18+
import com.google.common.collect.ImmutableMap;
19+
import java.util.Optional;
20+
21+
/** TODO */
22+
public class DefaultTypeProvider implements CelTypeProvider {
23+
24+
private static final DefaultTypeProvider INSTANCE = new DefaultTypeProvider();
25+
private final ImmutableMap<String, CelType> commonTypes;
26+
27+
@Override
28+
public ImmutableCollection<CelType> types() {
29+
return commonTypes.values();
30+
}
31+
32+
@Override
33+
public Optional<CelType> findType(String typeName) {
34+
return Optional.ofNullable(commonTypes.get(typeName));
35+
}
36+
37+
public static DefaultTypeProvider getInstance() {
38+
return INSTANCE;
39+
}
40+
41+
private DefaultTypeProvider() {
42+
ImmutableMap.Builder<String, CelType> typeMapBuilder = ImmutableMap.builder();
43+
typeMapBuilder.putAll(SimpleType.TYPE_MAP);
44+
typeMapBuilder.put("list", ListType.create(SimpleType.DYN));
45+
typeMapBuilder.put("map", MapType.create(SimpleType.DYN, SimpleType.DYN));
46+
typeMapBuilder.put(
47+
"optional_type",
48+
// TODO: Move to CelOptionalLibrary and register it on demand
49+
OptionalType.create(SimpleType.DYN));
50+
this.commonTypes = typeMapBuilder.buildOrThrow();
51+
}
52+
}

common/src/main/java/dev/cel/common/types/SimpleType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class SimpleType extends CelType {
4444
public static final CelType TIMESTAMP = create(CelKind.TIMESTAMP, "google.protobuf.Timestamp");
4545
public static final CelType UINT = create(CelKind.UINT, "uint");
4646

47-
private static final ImmutableMap<String, CelType> TYPE_MAP =
47+
public static final ImmutableMap<String, CelType> TYPE_MAP =
4848
ImmutableMap.of(
4949
DYN.name(), DYN,
5050
BOOL.name(), BOOL,

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) {

common/types/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ java_library(
5050
exports = ["//common/src/main/java/dev/cel/common/types:cel_proto_message_types"],
5151
)
5252

53+
java_library(
54+
name = "default_type_provider",
55+
visibility = ["//:internal"],
56+
exports = ["//common/src/main/java/dev/cel/common/types:default_type_provider"],
57+
)
58+
5359
java_library(
5460
name = "cel_v1alpha1_types",
5561
visibility = ["//:internal"],

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+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.planner;
16+
17+
import static java.util.stream.Collectors.joining;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.errorprone.annotations.Immutable;
21+
import dev.cel.common.types.CelTypeProvider;
22+
import dev.cel.common.types.TypeType;
23+
import dev.cel.runtime.GlobalResolver;
24+
25+
@Immutable
26+
interface Attribute {
27+
Object resolve(GlobalResolver ctx);
28+
29+
final class MaybeAttribute implements Attribute {
30+
private final ImmutableList<Attribute> attributes;
31+
32+
@Override
33+
public Object resolve(GlobalResolver ctx) {
34+
for (Attribute attr : attributes) {
35+
Object value = attr.resolve(ctx);
36+
if (value != null) {
37+
return value;
38+
}
39+
}
40+
41+
throw new IllegalArgumentException(
42+
String.format(
43+
"no such attribute(s): %s",
44+
attributes.stream().map(Attribute::toString).collect(joining(","))));
45+
}
46+
47+
MaybeAttribute(ImmutableList<Attribute> attributes) {
48+
this.attributes = attributes;
49+
}
50+
}
51+
52+
final class NamespacedAttribute implements Attribute {
53+
private final ImmutableList<String> namespacedNames;
54+
private final CelTypeProvider typeProvider;
55+
56+
@Override
57+
public Object resolve(GlobalResolver ctx) {
58+
for (String name : namespacedNames) {
59+
Object value = ctx.resolve(name);
60+
if (value != null) {
61+
// TODO: apply qualifiers
62+
return value;
63+
}
64+
65+
TypeType type = typeProvider.findType(name).map(TypeType::create).orElse(null);
66+
if (type != null) {
67+
return type;
68+
}
69+
}
70+
71+
throw new IllegalArgumentException(
72+
String.format("no such attribute(s): %s", String.join(",", namespacedNames)));
73+
}
74+
75+
NamespacedAttribute(CelTypeProvider typeProvider, ImmutableList<String> namespacedNames) {
76+
this.typeProvider = typeProvider;
77+
this.namespacedNames = namespacedNames;
78+
}
79+
}
80+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.planner;
16+
17+
import com.google.common.collect.ImmutableList;
18+
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.common.CelContainer;
20+
import dev.cel.common.types.CelTypeProvider;
21+
import dev.cel.runtime.planner.Attribute.MaybeAttribute;
22+
import dev.cel.runtime.planner.Attribute.NamespacedAttribute;
23+
24+
@Immutable
25+
final class AttributeFactory {
26+
27+
private final CelContainer container;
28+
private final CelTypeProvider typeProvider;
29+
30+
NamespacedAttribute newAbsoluteAttribute(String... names) {
31+
return new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names));
32+
}
33+
34+
MaybeAttribute newMaybeAttribute(String... names) {
35+
// TODO: Resolve container names
36+
return new MaybeAttribute(
37+
ImmutableList.of(new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names))));
38+
}
39+
40+
static AttributeFactory newAttributeFactory(
41+
CelContainer celContainer, CelTypeProvider typeProvider) {
42+
return new AttributeFactory(celContainer, typeProvider);
43+
}
44+
45+
private AttributeFactory(CelContainer container, CelTypeProvider typeProvider) {
46+
this.container = container;
47+
this.typeProvider = typeProvider;
48+
}
49+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_applicable_licenses = ["//:license"],
5+
default_visibility = [
6+
"//runtime/planner:__pkg__",
7+
],
8+
)
9+
10+
java_library(
11+
name = "program_planner",
12+
srcs = ["ProgramPlanner.java"],
13+
tags = [
14+
],
15+
deps = [
16+
":attribute_factory",
17+
":cel_value_interpretable",
18+
":cel_value_program",
19+
":eval_attribute",
20+
":eval_const",
21+
"//:auto_value",
22+
"//common:cel_ast",
23+
"//common:container",
24+
"//common/annotations",
25+
"//common/ast",
26+
"//common/types",
27+
"//common/types:type_providers",
28+
"//common/values",
29+
"//common/values:cel_value",
30+
"//runtime:evaluation_exception",
31+
"//runtime:evaluation_exception_builder",
32+
"//runtime:program",
33+
"@maven//:com_google_code_findbugs_annotations",
34+
"@maven//:com_google_errorprone_error_prone_annotations",
35+
"@maven//:com_google_guava_guava",
36+
],
37+
)
38+
39+
java_library(
40+
name = "cel_value_interpretable",
41+
srcs = ["CelValueInterpretable.java"],
42+
deps = [
43+
"//common/annotations",
44+
"//common/values:cel_value",
45+
"//runtime:evaluation_exception",
46+
"//runtime:interpretable",
47+
"@maven//:com_google_errorprone_error_prone_annotations",
48+
],
49+
)
50+
51+
java_library(
52+
name = "cel_value_program",
53+
srcs = ["CelValueProgram.java"],
54+
deps = [
55+
":cel_value_interpretable",
56+
"//:auto_value",
57+
"//common/values",
58+
"//common/values:cel_value",
59+
"//runtime:activation",
60+
"//runtime:evaluation_exception",
61+
"//runtime:function_resolver",
62+
"//runtime:interpretable",
63+
"//runtime:program",
64+
"@maven//:com_google_errorprone_error_prone_annotations",
65+
],
66+
)
67+
68+
java_library(
69+
name = "eval_const",
70+
srcs = ["EvalConstant.java"],
71+
deps = [
72+
":cel_value_interpretable",
73+
"//common/values:cel_value",
74+
"//runtime:interpretable",
75+
"@maven//:com_google_errorprone_error_prone_annotations",
76+
],
77+
)
78+
79+
java_library(
80+
name = "attribute",
81+
srcs = ["Attribute.java"],
82+
deps = [
83+
"//common/types",
84+
"//common/types:type_providers",
85+
"//runtime:interpretable",
86+
"@maven//:com_google_errorprone_error_prone_annotations",
87+
"@maven//:com_google_guava_guava",
88+
],
89+
)
90+
91+
java_library(
92+
name = "attribute_factory",
93+
srcs = ["AttributeFactory.java"],
94+
deps = [
95+
":attribute",
96+
"//common:container",
97+
"//common/types:type_providers",
98+
"//common/values",
99+
"@maven//:com_google_errorprone_error_prone_annotations",
100+
"@maven//:com_google_guava_guava",
101+
],
102+
)
103+
104+
java_library(
105+
name = "eval_attribute",
106+
srcs = ["EvalAttribute.java"],
107+
deps = [
108+
":attribute",
109+
":cel_value_interpretable",
110+
"//common/values",
111+
"//common/values:cel_value",
112+
"//runtime:interpretable",
113+
"@maven//:com_google_errorprone_error_prone_annotations",
114+
"@maven//:com_google_guava_guava",
115+
],
116+
)

0 commit comments

Comments
 (0)