Skip to content

Commit a65a1a2

Browse files
l46kokcopybara-github
authored andcommitted
Plan CreateMap
PiperOrigin-RevId: 828705611
1 parent 9083e06 commit a65a1a2

File tree

19 files changed

+1106
-4
lines changed

19 files changed

+1106
-4
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+
/** {@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/src/main/java/dev/cel/common/values/CelValueConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
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) {
4040
Preconditions.checkNotNull(celValue);
4141

4242
if (celValue instanceof MapValue) {
4343
MapValue<CelValue, CelValue> mapValue = (MapValue<CelValue, CelValue>) celValue;
44-
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
44+
ImmutableMap.Builder<Object, Object> mapBuilder =
45+
ImmutableMap.builderWithExpectedSize(mapValue.size());
4546
for (Entry<CelValue, CelValue> entry : mapValue.value().entrySet()) {
4647
Object key = fromCelValueToJavaObject(entry.getKey());
4748
Object value = fromCelValueToJavaObject(entry.getValue());
@@ -51,7 +52,8 @@ public Object fromCelValueToJavaObject(CelValue celValue) {
5152
return mapBuilder.buildOrThrow();
5253
} else if (celValue instanceof ListValue) {
5354
ListValue<CelValue> listValue = (ListValue<CelValue>) celValue;
54-
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
55+
ImmutableList.Builder<Object> listBuilder =
56+
ImmutableList.builderWithExpectedSize(listValue.size());
5557
for (CelValue element : listValue.value()) {
5658
listBuilder.add(fromCelValueToJavaObject(element));
5759
}

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: 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+
}

0 commit comments

Comments
 (0)