Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,8 @@ java_library(
name = "operator",
exports = ["//common/src/main/java/dev/cel/common:operator"],
)

cel_android_library(
name = "operator_android",
exports = ["//common/src/main/java/dev/cel/common:operator_android"],
)
6 changes: 6 additions & 0 deletions common/exceptions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ java_library(
# used_by_android
exports = ["//common/src/main/java/dev/cel/common/exceptions:invalid_argument"],
)

java_library(
name = "iteration_budget_exceeded",
# used_by_android
exports = ["//common/src/main/java/dev/cel/common/exceptions:iteration_budget_exceeded"],
)
8 changes: 8 additions & 0 deletions common/src/main/java/dev/cel/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,11 @@ java_library(
],
deps = ["@maven//:com_google_guava_guava"],
)

cel_android_library(
name = "operator_android",
srcs = ["Operator.java"],
tags = [
],
deps = ["@maven_android//:com_google_guava_guava"],
)
13 changes: 13 additions & 0 deletions common/src/main/java/dev/cel/common/exceptions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,16 @@ java_library(
"//common/annotations",
],
)

java_library(
name = "iteration_budget_exceeded",
srcs = ["CelIterationLimitExceededException.java"],
# used_by_android
tags = [
],
deps = [
"//common:error_codes",
"//common:runtime_exception",
"//common/annotations",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.exceptions;

import dev.cel.common.CelErrorCode;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.annotations.Internal;
import java.util.Locale;

/** Indicates that the iteration budget for a comprehension has been exceeded. */
@Internal
public final class CelIterationLimitExceededException extends CelRuntimeException {

public CelIterationLimitExceededException(int budget) {
super(
String.format(Locale.US, "Iteration budget exceeded: %d", budget),
CelErrorCode.ITERATION_BUDGET_EXCEEDED);
}
}
13 changes: 13 additions & 0 deletions common/src/main/java/dev/cel/common/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ java_library(
],
)

java_library(
name = "message_lite_type_provider",
srcs = [
"ProtoMessageLiteTypeProvider.java",
],
tags = [
],
deps = [
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "default_type_provider",
srcs = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.types;

import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import dev.cel.protobuf.CelLiteDescriptor;
import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor;
import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

/** TODO */
public final class ProtoMessageLiteTypeProvider implements CelTypeProvider {
private static final ImmutableMap<FieldLiteDescriptor.Type, CelType> PROTO_TYPE_TO_CEL_TYPE =
ImmutableMap.<FieldLiteDescriptor.Type, CelType>builder()
.put(FieldLiteDescriptor.Type.DOUBLE, SimpleType.DOUBLE)
.put(FieldLiteDescriptor.Type.FLOAT, SimpleType.DOUBLE)
.put(FieldLiteDescriptor.Type.INT64, SimpleType.INT)
.put(FieldLiteDescriptor.Type.INT32, SimpleType.INT)
.put(FieldLiteDescriptor.Type.SFIXED32, SimpleType.INT)
.put(FieldLiteDescriptor.Type.SFIXED64, SimpleType.INT)
.put(FieldLiteDescriptor.Type.SINT32, SimpleType.INT)
.put(FieldLiteDescriptor.Type.SINT64, SimpleType.INT)
.put(FieldLiteDescriptor.Type.BOOL, SimpleType.BOOL)
.put(FieldLiteDescriptor.Type.STRING, SimpleType.STRING)
.put(FieldLiteDescriptor.Type.BYTES, SimpleType.BYTES)
.put(FieldLiteDescriptor.Type.FIXED32, SimpleType.UINT)
.put(FieldLiteDescriptor.Type.FIXED64, SimpleType.UINT)
.put(FieldLiteDescriptor.Type.UINT32, SimpleType.UINT)
.put(FieldLiteDescriptor.Type.UINT64, SimpleType.UINT)
.buildOrThrow();

private final ImmutableMap<String, CelType> allTypes;

@Override
public ImmutableCollection<CelType> types() {
return null;
}

@Override
public Optional<CelType> findType(String typeName) {
return Optional.empty();
}

public static ProtoMessageLiteTypeProvider newInstance(
Set<CelLiteDescriptor> celLiteDescriptors) {
return new ProtoMessageLiteTypeProvider(celLiteDescriptors);
}

private ProtoMessageLiteTypeProvider(Set<CelLiteDescriptor> celLiteDescriptors) {
ImmutableMap.Builder<String, CelType> builder = ImmutableMap.builder();
for (CelLiteDescriptor descriptor : celLiteDescriptors) {
for (Entry<String, MessageLiteDescriptor> entry :
descriptor.getProtoTypeNamesToDescriptors().entrySet()) {
builder.put(entry.getKey(), createMessageType(entry.getValue()));
}
}

this.allTypes = builder.buildOrThrow();
}

private static ProtoMessageType createMessageType(MessageLiteDescriptor messageLiteDescriptor) {
ImmutableMap<String, FieldLiteDescriptor> fields =
messageLiteDescriptor.getFieldDescriptors().stream()
.collect(toImmutableMap(FieldLiteDescriptor::getFieldName, Function.identity()));

return new ProtoMessageType(
messageLiteDescriptor.getProtoTypeName(),
fields.keySet(),
new FieldResolver(fields),
extensionFieldName -> {
throw new UnsupportedOperationException(
"Proto extensions are not yet supported in MessageLite.");
});
}

private static class FieldResolver implements StructType.FieldResolver {
private final ImmutableMap<String, FieldLiteDescriptor> fields;

@Override
public Optional<CelType> findField(String fieldName) {
FieldLiteDescriptor fieldDescriptor = fields.get(fieldName);
if (fieldDescriptor == null) {
return Optional.empty();
}

FieldLiteDescriptor.Type fieldType = fieldDescriptor.getProtoFieldType();
switch (fieldDescriptor.getProtoFieldType()) {
default:
return Optional.of(PROTO_TYPE_TO_CEL_TYPE.get(fieldType));
}
}

private FieldResolver(ImmutableMap<String, FieldLiteDescriptor> fields) {
this.fields = fields;
}
}
}
10 changes: 10 additions & 0 deletions common/src/main/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ java_library(
tags = [
],
deps = [
":values",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand All @@ -68,6 +69,7 @@ cel_android_library(
tags = [
],
deps = [
"//common/values:values_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
Expand All @@ -79,6 +81,7 @@ java_library(
tags = [
],
deps = [
"//common/values",
"//common/values:cel_value_provider",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand All @@ -92,6 +95,7 @@ cel_android_library(
],
deps = [
"//common/values:cel_value_provider_android",
"//common/values:values_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
Expand Down Expand Up @@ -213,7 +217,9 @@ java_library(
"//common/annotations",
"//common/internal:dynamic_proto",
"//common/internal:proto_message_factory",
"//common/values",
"//common/values:base_proto_cel_value_converter",
"//common/values:cel_value_provider",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_protobuf_protobuf_java",
],
Expand Down Expand Up @@ -284,7 +290,9 @@ java_library(
"//common/annotations",
"//common/internal:cel_lite_descriptor_pool",
"//common/internal:default_lite_descriptor_pool",
"//common/values",
"//common/values:base_proto_cel_value_converter",
"//common/values:cel_value_provider",
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand All @@ -304,6 +312,8 @@ cel_android_library(
"//common/internal:cel_lite_descriptor_pool_android",
"//common/internal:default_lite_descriptor_pool_android",
"//common/values:base_proto_cel_value_converter_android",
"//common/values:cel_value_provider_android",
"//common/values:values_android",
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@
*/
@Internal
@Immutable
public abstract class BaseProtoMessageValueProvider implements CelValueProvider {

public abstract BaseProtoCelValueConverter protoCelValueConverter();
}
public abstract class BaseProtoMessageValueProvider implements CelValueProvider {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
@Internal
@Immutable
abstract class CelValueConverter {
public class CelValueConverter {

private static final CelValueConverter DEFAULT_INSTANCE = new CelValueConverter();

public static CelValueConverter getDefaultInstance() {
return DEFAULT_INSTANCE;
}

/** Adapts a {@link CelValue} to a plain old Java Object. */
public Object unwrap(CelValue celValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public interface CelValueProvider {
* a wrapper.
*/
Optional<Object> newValue(String structType, Map<String, Object> fields);

default CelValueConverter celValueConverter() {
return CelValueConverter.getDefaultInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@Immutable
public final class CombinedCelValueProvider implements CelValueProvider {
private final ImmutableList<CelValueProvider> celValueProviders;
private final CelValueConverter celValueConverter;

/** Combines the provided first and second {@link CelValueProvider}. */
public static CombinedCelValueProvider combine(CelValueProvider... providers) {
Expand All @@ -49,12 +50,18 @@ public Optional<Object> newValue(String structType, Map<String, Object> fields)
return Optional.empty();
}

@Override
public CelValueConverter celValueConverter() {
return celValueConverter;
}

/** Returns the underlying {@link CelValueProvider}s in order. */
public ImmutableList<CelValueProvider> valueProviders() {
return celValueProviders;
}

private CombinedCelValueProvider(ImmutableList<CelValueProvider> providers) {
celValueProviders = checkNotNull(providers);
this.celValueProviders = checkNotNull(providers);
this.celValueConverter = providers.get(0).celValueConverter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ public Object toRuntimeValue(Object value) {
}

if (value instanceof MessageOrBuilder) {
MessageOrBuilder message = (MessageOrBuilder) value;
Message message;
if (value instanceof Message.Builder) {
message = ((Message.Builder) value).build();
} else {
message = (Message) value;
}

// Attempt to convert the proto from a dynamic message into a concrete message if possible.
if (message instanceof DynamicMessage) {
message = dynamicProto.maybeAdaptDynamicMessage((DynamicMessage) message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.MessageLite;
import com.google.protobuf.MessageLiteOrBuilder;
import com.google.protobuf.WireFormat;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.CelLiteDescriptorPool;
Expand Down Expand Up @@ -163,8 +164,13 @@ Object getDefaultCelValue(String protoTypeName, String fieldName) {
@SuppressWarnings("LiteProtoToString") // No alternative identifier to use. Debug only info is OK.
public Object toRuntimeValue(Object value) {
checkNotNull(value);
if (value instanceof MessageLite) {
MessageLite msg = (MessageLite) value;
if (value instanceof MessageLiteOrBuilder) {
MessageLite msg;
if (value instanceof MessageLite.Builder) {
msg = ((MessageLite.Builder) value).build();
} else {
msg = (MessageLite) value;
}

MessageLiteDescriptor descriptor =
descriptorPool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
*/
@Immutable
@Beta
public class ProtoMessageLiteValueProvider extends BaseProtoMessageValueProvider {
public class ProtoMessageLiteValueProvider implements CelValueProvider {
private final CelLiteDescriptorPool descriptorPool;
private final ProtoLiteCelValueConverter protoLiteCelValueConverter;

@Override
public BaseProtoCelValueConverter protoCelValueConverter() {
public CelValueConverter celValueConverter() {
return protoLiteCelValueConverter;
}

Expand Down
Loading
Loading