Skip to content

Commit 739edae

Browse files
l46kokcopybara-github
authored andcommitted
Internal Changes
PiperOrigin-RevId: 834975953
1 parent 52b859c commit 739edae

File tree

109 files changed

+2434
-650
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+2434
-650
lines changed

common/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,8 @@ java_library(
126126
name = "operator",
127127
exports = ["//common/src/main/java/dev/cel/common:operator"],
128128
)
129+
130+
cel_android_library(
131+
name = "operator_android",
132+
exports = ["//common/src/main/java/dev/cel/common:operator_android"],
133+
)

common/exceptions/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ java_library(
4040
# used_by_android
4141
exports = ["//common/src/main/java/dev/cel/common/exceptions:invalid_argument"],
4242
)
43+
44+
java_library(
45+
name = "iteration_budget_exceeded",
46+
# used_by_android
47+
exports = ["//common/src/main/java/dev/cel/common/exceptions:iteration_budget_exceeded"],
48+
)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,11 @@ java_library(
360360
],
361361
deps = ["@maven//:com_google_guava_guava"],
362362
)
363+
364+
cel_android_library(
365+
name = "operator_android",
366+
srcs = ["Operator.java"],
367+
tags = [
368+
],
369+
deps = ["@maven_android//:com_google_guava_guava"],
370+
)

common/src/main/java/dev/cel/common/exceptions/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,16 @@ java_library(
8585
"//common/annotations",
8686
],
8787
)
88+
89+
java_library(
90+
name = "iteration_budget_exceeded",
91+
srcs = ["CelIterationLimitExceededException.java"],
92+
# used_by_android
93+
tags = [
94+
],
95+
deps = [
96+
"//common:error_codes",
97+
"//common:runtime_exception",
98+
"//common/annotations",
99+
],
100+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.CelRuntimeException;
19+
import dev.cel.common.annotations.Internal;
20+
import java.util.Locale;
21+
22+
/** Indicates that the iteration budget for a comprehension has been exceeded. */
23+
@Internal
24+
public final class CelIterationLimitExceededException extends CelRuntimeException {
25+
26+
public CelIterationLimitExceededException(int budget) {
27+
super(
28+
String.format(Locale.US, "Iteration budget exceeded: %d", budget),
29+
CelErrorCode.ITERATION_BUDGET_EXCEEDED);
30+
}
31+
}

common/src/main/java/dev/cel/common/types/BUILD.bazel

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

186+
java_library(
187+
name = "message_lite_type_provider",
188+
srcs = [
189+
"ProtoMessageLiteTypeProvider.java",
190+
],
191+
tags = [
192+
],
193+
deps = [
194+
"@maven//:com_google_errorprone_error_prone_annotations",
195+
"@maven//:com_google_guava_guava",
196+
],
197+
)
198+
186199
java_library(
187200
name = "default_type_provider",
188201
srcs = [
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 static com.google.common.collect.ImmutableMap.toImmutableMap;
18+
19+
import com.google.common.collect.ImmutableCollection;
20+
import com.google.common.collect.ImmutableMap;
21+
import dev.cel.protobuf.CelLiteDescriptor;
22+
import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor;
23+
import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor;
24+
import java.util.Map.Entry;
25+
import java.util.Optional;
26+
import java.util.Set;
27+
import java.util.function.Function;
28+
29+
/** TODO */
30+
public final class ProtoMessageLiteTypeProvider implements CelTypeProvider {
31+
private static final ImmutableMap<FieldLiteDescriptor.Type, CelType> PROTO_TYPE_TO_CEL_TYPE =
32+
ImmutableMap.<FieldLiteDescriptor.Type, CelType>builder()
33+
.put(FieldLiteDescriptor.Type.DOUBLE, SimpleType.DOUBLE)
34+
.put(FieldLiteDescriptor.Type.FLOAT, SimpleType.DOUBLE)
35+
.put(FieldLiteDescriptor.Type.INT64, SimpleType.INT)
36+
.put(FieldLiteDescriptor.Type.INT32, SimpleType.INT)
37+
.put(FieldLiteDescriptor.Type.SFIXED32, SimpleType.INT)
38+
.put(FieldLiteDescriptor.Type.SFIXED64, SimpleType.INT)
39+
.put(FieldLiteDescriptor.Type.SINT32, SimpleType.INT)
40+
.put(FieldLiteDescriptor.Type.SINT64, SimpleType.INT)
41+
.put(FieldLiteDescriptor.Type.BOOL, SimpleType.BOOL)
42+
.put(FieldLiteDescriptor.Type.STRING, SimpleType.STRING)
43+
.put(FieldLiteDescriptor.Type.BYTES, SimpleType.BYTES)
44+
.put(FieldLiteDescriptor.Type.FIXED32, SimpleType.UINT)
45+
.put(FieldLiteDescriptor.Type.FIXED64, SimpleType.UINT)
46+
.put(FieldLiteDescriptor.Type.UINT32, SimpleType.UINT)
47+
.put(FieldLiteDescriptor.Type.UINT64, SimpleType.UINT)
48+
.buildOrThrow();
49+
50+
private final ImmutableMap<String, CelType> allTypes;
51+
52+
@Override
53+
public ImmutableCollection<CelType> types() {
54+
return null;
55+
}
56+
57+
@Override
58+
public Optional<CelType> findType(String typeName) {
59+
return Optional.empty();
60+
}
61+
62+
public static ProtoMessageLiteTypeProvider newInstance(
63+
Set<CelLiteDescriptor> celLiteDescriptors) {
64+
return new ProtoMessageLiteTypeProvider(celLiteDescriptors);
65+
}
66+
67+
private ProtoMessageLiteTypeProvider(Set<CelLiteDescriptor> celLiteDescriptors) {
68+
ImmutableMap.Builder<String, CelType> builder = ImmutableMap.builder();
69+
for (CelLiteDescriptor descriptor : celLiteDescriptors) {
70+
for (Entry<String, MessageLiteDescriptor> entry :
71+
descriptor.getProtoTypeNamesToDescriptors().entrySet()) {
72+
builder.put(entry.getKey(), createMessageType(entry.getValue()));
73+
}
74+
}
75+
76+
this.allTypes = builder.buildOrThrow();
77+
}
78+
79+
private static ProtoMessageType createMessageType(MessageLiteDescriptor messageLiteDescriptor) {
80+
ImmutableMap<String, FieldLiteDescriptor> fields =
81+
messageLiteDescriptor.getFieldDescriptors().stream()
82+
.collect(toImmutableMap(FieldLiteDescriptor::getFieldName, Function.identity()));
83+
84+
return new ProtoMessageType(
85+
messageLiteDescriptor.getProtoTypeName(),
86+
fields.keySet(),
87+
new FieldResolver(fields),
88+
extensionFieldName -> {
89+
throw new UnsupportedOperationException(
90+
"Proto extensions are not yet supported in MessageLite.");
91+
});
92+
}
93+
94+
private static class FieldResolver implements StructType.FieldResolver {
95+
private final ImmutableMap<String, FieldLiteDescriptor> fields;
96+
97+
@Override
98+
public Optional<CelType> findField(String fieldName) {
99+
FieldLiteDescriptor fieldDescriptor = fields.get(fieldName);
100+
if (fieldDescriptor == null) {
101+
return Optional.empty();
102+
}
103+
104+
FieldLiteDescriptor.Type fieldType = fieldDescriptor.getProtoFieldType();
105+
switch (fieldDescriptor.getProtoFieldType()) {
106+
default:
107+
return Optional.of(PROTO_TYPE_TO_CEL_TYPE.get(fieldType));
108+
}
109+
}
110+
111+
private FieldResolver(ImmutableMap<String, FieldLiteDescriptor> fields) {
112+
this.fields = fields;
113+
}
114+
}
115+
}

common/src/main/java/dev/cel/common/values/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ java_library(
5757
tags = [
5858
],
5959
deps = [
60+
":values",
6061
"@maven//:com_google_errorprone_error_prone_annotations",
6162
"@maven//:com_google_guava_guava",
6263
],
@@ -68,6 +69,7 @@ cel_android_library(
6869
tags = [
6970
],
7071
deps = [
72+
"//common/values:values_android",
7173
"@maven//:com_google_errorprone_error_prone_annotations",
7274
"@maven_android//:com_google_guava_guava",
7375
],
@@ -79,6 +81,7 @@ java_library(
7981
tags = [
8082
],
8183
deps = [
84+
"//common/values",
8285
"//common/values:cel_value_provider",
8386
"@maven//:com_google_errorprone_error_prone_annotations",
8487
"@maven//:com_google_guava_guava",
@@ -92,6 +95,7 @@ cel_android_library(
9295
],
9396
deps = [
9497
"//common/values:cel_value_provider_android",
98+
"//common/values:values_android",
9599
"@maven//:com_google_errorprone_error_prone_annotations",
96100
"@maven_android//:com_google_guava_guava",
97101
],
@@ -213,7 +217,9 @@ java_library(
213217
"//common/annotations",
214218
"//common/internal:dynamic_proto",
215219
"//common/internal:proto_message_factory",
220+
"//common/values",
216221
"//common/values:base_proto_cel_value_converter",
222+
"//common/values:cel_value_provider",
217223
"@maven//:com_google_errorprone_error_prone_annotations",
218224
"@maven//:com_google_protobuf_protobuf_java",
219225
],
@@ -284,7 +290,9 @@ java_library(
284290
"//common/annotations",
285291
"//common/internal:cel_lite_descriptor_pool",
286292
"//common/internal:default_lite_descriptor_pool",
293+
"//common/values",
287294
"//common/values:base_proto_cel_value_converter",
295+
"//common/values:cel_value_provider",
288296
"//protobuf:cel_lite_descriptor",
289297
"@maven//:com_google_errorprone_error_prone_annotations",
290298
"@maven//:com_google_guava_guava",
@@ -304,6 +312,8 @@ cel_android_library(
304312
"//common/internal:cel_lite_descriptor_pool_android",
305313
"//common/internal:default_lite_descriptor_pool_android",
306314
"//common/values:base_proto_cel_value_converter_android",
315+
"//common/values:cel_value_provider_android",
316+
"//common/values:values_android",
307317
"//protobuf:cel_lite_descriptor",
308318
"@maven//:com_google_errorprone_error_prone_annotations",
309319
"@maven_android//:com_google_guava_guava",

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,4 @@
2525
*/
2626
@Internal
2727
@Immutable
28-
public abstract class BaseProtoMessageValueProvider implements CelValueProvider {
29-
30-
public abstract BaseProtoCelValueConverter protoCelValueConverter();
31-
}
28+
public abstract class BaseProtoMessageValueProvider implements CelValueProvider {}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3434
@Internal
3535
@Immutable
36-
abstract class CelValueConverter {
36+
public class CelValueConverter {
37+
38+
private static final CelValueConverter DEFAULT_INSTANCE = new CelValueConverter();
39+
40+
public static CelValueConverter getDefaultInstance() {
41+
return DEFAULT_INSTANCE;
42+
}
3743

3844
/** Adapts a {@link CelValue} to a plain old Java Object. */
3945
public Object unwrap(CelValue celValue) {

0 commit comments

Comments
 (0)