Skip to content

Commit 3213375

Browse files
l46kokcopybara-github
authored andcommitted
Plan identifiers
PiperOrigin-RevId: 828205454
1 parent 5ee0d7b commit 3213375

File tree

13 files changed

+454
-8
lines changed

13 files changed

+454
-8
lines changed

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+
/** {@code DefaultTypeProvider} is a registry of common CEL types. */
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/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"],
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
18+
import com.google.common.collect.ImmutableList;
19+
import com.google.errorprone.annotations.Immutable;
20+
import dev.cel.common.types.CelTypeProvider;
21+
import dev.cel.common.types.TypeType;
22+
import dev.cel.runtime.GlobalResolver;
23+
24+
@Immutable
25+
interface Attribute {
26+
Object resolve(GlobalResolver ctx);
27+
28+
final class MaybeAttribute implements Attribute {
29+
private final ImmutableList<Attribute> attributes;
30+
31+
@Override
32+
public Object resolve(GlobalResolver ctx) {
33+
for (Attribute attr : attributes) {
34+
Object value = attr.resolve(ctx);
35+
if (value != null) {
36+
return value;
37+
}
38+
}
39+
40+
// TODO: Handle unknowns
41+
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
42+
}
43+
44+
MaybeAttribute(ImmutableList<Attribute> attributes) {
45+
this.attributes = attributes;
46+
}
47+
}
48+
49+
final class NamespacedAttribute implements Attribute {
50+
private final ImmutableList<String> namespacedNames;
51+
private final CelTypeProvider typeProvider;
52+
53+
@Override
54+
public Object resolve(GlobalResolver ctx) {
55+
for (String name : namespacedNames) {
56+
Object value = ctx.resolve(name);
57+
if (value != null) {
58+
// TODO: apply qualifiers
59+
return value;
60+
}
61+
62+
TypeType type = typeProvider.findType(name).map(TypeType::create).orElse(null);
63+
if (type != null) {
64+
return type;
65+
}
66+
}
67+
68+
// TODO: Handle unknowns
69+
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
70+
}
71+
72+
NamespacedAttribute(CelTypeProvider typeProvider, ImmutableList<String> namespacedNames) {
73+
this.typeProvider = typeProvider;
74+
this.namespacedNames = namespacedNames;
75+
}
76+
}
77+
}
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 unusedContainer;
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.unusedContainer = container;
47+
this.typeProvider = typeProvider;
48+
}
49+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ java_library(
1313
tags = [
1414
],
1515
deps = [
16+
":attribute",
17+
":eval_attribute",
1618
":eval_const",
1719
":planned_program",
1820
"//:auto_value",
1921
"//common:cel_ast",
22+
"//common:container",
2023
"//common/annotations",
2124
"//common/ast",
25+
"//common/types",
2226
"//common/types:type_providers",
2327
"//runtime:evaluation_exception",
2428
"//runtime:evaluation_exception_builder",
@@ -35,6 +39,7 @@ java_library(
3539
srcs = ["PlannedProgram.java"],
3640
deps = [
3741
"//:auto_value",
42+
"//runtime:activation",
3843
"//runtime:evaluation_exception",
3944
"//runtime:function_resolver",
4045
"//runtime:interpretable",
@@ -56,3 +61,32 @@ java_library(
5661
"@maven//:com_google_guava_guava",
5762
],
5863
)
64+
65+
java_library(
66+
name = "attribute",
67+
srcs = [
68+
"Attribute.java",
69+
"AttributeFactory.java",
70+
],
71+
deps = [
72+
"//common:container",
73+
"//common/types",
74+
"//common/types:type_providers",
75+
"//runtime:interpretable",
76+
"@maven//:com_google_errorprone_error_prone_annotations",
77+
"@maven//:com_google_guava_guava",
78+
],
79+
)
80+
81+
java_library(
82+
name = "eval_attribute",
83+
srcs = ["EvalAttribute.java"],
84+
deps = [
85+
":attribute",
86+
"//runtime:evaluation_listener",
87+
"//runtime:function_resolver",
88+
"//runtime:interpretable",
89+
"@maven//:com_google_errorprone_error_prone_annotations",
90+
"@maven//:com_google_guava_guava",
91+
],
92+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.errorprone.annotations.Immutable;
18+
import dev.cel.runtime.CelEvaluationListener;
19+
import dev.cel.runtime.CelFunctionResolver;
20+
import dev.cel.runtime.GlobalResolver;
21+
import dev.cel.runtime.Interpretable;
22+
23+
@Immutable
24+
final class EvalAttribute implements Interpretable {
25+
26+
private final Attribute attr;
27+
28+
@Override
29+
public Object eval(GlobalResolver resolver) {
30+
return attr.resolve(resolver);
31+
}
32+
33+
@Override
34+
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
35+
// TODO: Implement support
36+
throw new UnsupportedOperationException("Not yet supported");
37+
}
38+
39+
@Override
40+
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
41+
// TODO: Implement support
42+
throw new UnsupportedOperationException("Not yet supported");
43+
}
44+
45+
@Override
46+
public Object eval(
47+
GlobalResolver resolver,
48+
CelFunctionResolver lateBoundFunctionResolver,
49+
CelEvaluationListener listener) {
50+
// TODO: Implement support
51+
throw new UnsupportedOperationException("Not yet supported");
52+
}
53+
54+
static EvalAttribute create(Attribute attr) {
55+
return new EvalAttribute(attr);
56+
}
57+
58+
private EvalAttribute(Attribute attr) {
59+
this.attr = attr;
60+
}
61+
}

runtime/src/main/java/dev/cel/runtime/planner/EvalConstant.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ static EvalConstant create(CelByteString byteString) {
110110
return new EvalConstant(byteString);
111111
}
112112

113+
static EvalConstant create(Object value) {
114+
return new EvalConstant(value);
115+
}
116+
113117
private EvalConstant(Object constant) {
114118
this.constant = constant;
115119
}

runtime/src/main/java/dev/cel/runtime/planner/PlannedProgram.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.runtime.Activation;
1920
import dev.cel.runtime.CelEvaluationException;
2021
import dev.cel.runtime.CelFunctionResolver;
2122
import dev.cel.runtime.GlobalResolver;
@@ -35,7 +36,7 @@ public Object eval() throws CelEvaluationException {
3536

3637
@Override
3738
public Object eval(Map<String, ?> mapValue) throws CelEvaluationException {
38-
throw new UnsupportedOperationException("Not yet implemented");
39+
return interpretable().eval(Activation.copyOf(mapValue));
3940
}
4041

4142
@Override

0 commit comments

Comments
 (0)