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
3 changes: 1 addition & 2 deletions bundle/src/main/java/dev/cel/bundle/CelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public interface CelBuilder {
* provider will be used first before falling back to the built-in {@link
* dev.cel.common.values.ProtoMessageValueProvider} for resolving protobuf messages.
*
* <p>Note that {@link CelOptions#enableCelValue()} must be enabled or this method will be a
* no-op.
* <p>Note that this option is only supported for planner-based runtime.
*/
@CanIgnoreReturnValue
CelBuilder setValueProvider(CelValueProvider celValueProvider);
Expand Down
38 changes: 0 additions & 38 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,24 +556,6 @@ public void program_withVars() throws Exception {
assertThat(program.eval(ImmutableMap.of("variable", "hello"))).isEqualTo(true);
}

@Test
public void program_withCelValue() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setOptions(CelOptions.current().enableCelValue(true).build())
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();

CelRuntime.Program program = cel.createProgram(cel.compile("variable == 'hello'").getAst());

assertThat(program.eval(ImmutableMap.of("variable", "hello"))).isEqualTo(true);
}

@Test
public void program_withProtoVars() throws Exception {
Cel cel =
Expand Down Expand Up @@ -1419,26 +1401,6 @@ public void programAdvanceEvaluation_nestedSelect() throws Exception {
.isEqualTo(CelUnknownSet.create(CelAttribute.fromQualifiedIdentifier("com.google.a")));
}

@Test
public void programAdvanceEvaluation_nestedSelect_withCelValue() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setOptions(
CelOptions.current().enableUnknownTracking(true).enableCelValue(true).build())
.addVar("com", MapType.create(SimpleType.STRING, SimpleType.DYN))
.addFunctionBindings()
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("com.google.a || false").getAst());

assertThat(
program.advanceEvaluation(
UnknownContext.create(
fromMap(ImmutableMap.of()),
ImmutableList.of(CelAttributePattern.fromQualifiedIdentifier("com.google.a")))))
.isEqualTo(CelUnknownSet.create(CelAttribute.fromQualifiedIdentifier("com.google.a")));
}

@Test
public void programAdvanceEvaluation_argumentMergeErrorPriority() throws Exception {
Cel cel =
Expand Down
10 changes: 10 additions & 0 deletions common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ java_library(
exports = ["//common/src/main/java/dev/cel/common:container"],
)

cel_android_library(
name = "container_android",
exports = ["//common/src/main/java/dev/cel/common:container_android"],
)

java_library(
name = "proto_ast",
exports = ["//common/src/main/java/dev/cel/common:proto_ast"],
Expand Down Expand Up @@ -126,3 +131,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"],
)
20 changes: 20 additions & 0 deletions common/src/main/java/dev/cel/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,30 @@ java_library(
],
)

cel_android_library(
name = "container_android",
srcs = ["CelContainer.java"],
tags = [
],
deps = [
"//:auto_value",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
)

java_library(
name = "operator",
srcs = ["Operator.java"],
tags = [
],
deps = ["@maven//:com_google_guava_guava"],
)

cel_android_library(
name = "operator_android",
srcs = ["Operator.java"],
tags = [
],
deps = ["@maven_android//:com_google_guava_guava"],
)
14 changes: 0 additions & 14 deletions common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Beta;

/**
* Options to configure how the CEL parser, type-checker, and evaluator behave.
Expand Down Expand Up @@ -105,8 +104,6 @@ public enum ProtoUnsetFieldOptions {

public abstract boolean enableUnknownTracking();

public abstract boolean enableCelValue();

public abstract int comprehensionMaxIterations();

public abstract boolean evaluateCanonicalTypesToNativeValues();
Expand Down Expand Up @@ -162,7 +159,6 @@ public static Builder newBuilder() {
.errorOnDuplicateMapKeys(false)
.resolveTypeDependencies(true)
.enableUnknownTracking(false)
.enableCelValue(false)
.comprehensionMaxIterations(-1)
.unwrapWellKnownTypesOnFunctionDispatch(true)
.fromProtoUnsetFieldOption(ProtoUnsetFieldOptions.BIND_DEFAULT)
Expand Down Expand Up @@ -432,16 +428,6 @@ public abstract static class Builder {
*/
public abstract Builder enableUnknownTracking(boolean value);

/**
* Enables the usage of {@code CelValue} for the runtime. It is a native value representation of
* CEL that wraps Java native objects, and comes with extended capabilities, such as allowing
* value constructs not understood by CEL (ex: POJOs).
*
* <p>Warning: This option is experimental.
*/
@Beta
public abstract Builder enableCelValue(boolean value);

/**
* Limit the total number of iterations permitted within comprehension loops.
*
Expand Down
40 changes: 40 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,32 @@ java_library(
],
)

java_library(
name = "message_lite_type_provider",
srcs = [
"ProtoMessageLiteTypeProvider.java",
],
tags = [
],
deps = [
"//common/types",
"//common/types:type_providers",
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_guava_guava",
],
)

cel_android_library(
name = "message_lite_type_provider_android",
srcs = [
"ProtoMessageLiteTypeProvider.java",
],
tags = [
],
deps = [
],
)

java_library(
name = "default_type_provider",
srcs = [
Expand All @@ -197,6 +223,20 @@ java_library(
],
)

cel_android_library(
name = "default_type_provider_android",
srcs = [
"DefaultTypeProvider.java",
],
tags = [
],
deps = [
"//common/types:type_providers_android",
"//common/types:types_android",
"@maven_android//:com_google_guava_guava",
],
)

cel_android_library(
name = "cel_types_android",
srcs = ["CelTypes.java"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import java.util.Optional;

/** {@code DefaultTypeProvider} is a registry of common CEL types. */
@Immutable
public class DefaultTypeProvider implements CelTypeProvider {

private static final DefaultTypeProvider INSTANCE = new DefaultTypeProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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 com.google.common.collect.ImmutableSet;
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 allTypes.values();
}

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

public static ProtoMessageLiteTypeProvider newInstance(CelLiteDescriptor... celLiteDescriptors) {
return newInstance(ImmutableSet.copyOf(celLiteDescriptors));
}

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;
}
}
}
Loading
Loading