Skip to content

Commit

Permalink
Add an adapter for legacy runtime to consume CelValueProvider. Add in…
Browse files Browse the repository at this point in the history
…terpreter test for CelValues.

PiperOrigin-RevId: 563598718
  • Loading branch information
l46kok authored and copybara-github committed Dec 6, 2023
1 parent 5cf5755 commit 034657d
Show file tree
Hide file tree
Showing 32 changed files with 739 additions and 202 deletions.
38 changes: 38 additions & 0 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ 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(CelTypes.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 @@ -1285,6 +1303,26 @@ 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
12 changes: 12 additions & 0 deletions common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public abstract class CelOptions {

public abstract boolean enableUnknownTracking();

public abstract boolean enableCelValue();

public abstract int comprehensionMaxIterations();

public abstract Builder toBuilder();
Expand Down Expand Up @@ -176,6 +178,7 @@ public static Builder newBuilder() {
.errorOnDuplicateMapKeys(false)
.resolveTypeDependencies(true)
.enableUnknownTracking(false)
.enableCelValue(false)
.comprehensionMaxIterations(-1);
}

Expand Down Expand Up @@ -428,6 +431,15 @@ 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.
*/
public abstract Builder enableCelValue(boolean value);

/**
* Limit the total number of iterations permitted within comprehension loops.
*
Expand Down
12 changes: 10 additions & 2 deletions common/src/main/java/dev/cel/common/types/TypeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
@Immutable
public abstract class TypeType extends CelType {

static final TypeType TYPE = create(SimpleType.DYN);

@Override
public CelKind kind() {
return CelKind.TYPE;
Expand All @@ -38,6 +36,16 @@ public String name() {
return "type";
}

/** Retrieves the underlying type name of the type-kind held. */
public String containingTypeName() {
CelType containingType = type();
if (containingType.kind() == CelKind.DYN) {
return "type";
}

return containingType.name();
}

@Override
public abstract ImmutableList<CelType> parameters();

Expand Down
3 changes: 3 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 @@ -24,6 +24,7 @@ CEL_VALUES_SOURCES = [
"NullValue.java",
"OpaqueValue.java",
"OptionalValue.java",
"SelectableValue.java",
"StringValue.java",
"StructValue.java",
"TimestampValue.java",
Expand Down Expand Up @@ -72,7 +73,9 @@ java_library(
":cel_byte_string",
":cel_value",
"//:auto_value",
"//common:error_codes",
"//common:options",
"//common:runtime_exception",
"//common/annotations",
"//common/types",
"//common/types:type_providers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface CelValueProvider {
final class CombinedCelValueProvider implements CelValueProvider {
private final ImmutableList<CelValueProvider> celValueProviders;

CombinedCelValueProvider(CelValueProvider first, CelValueProvider second) {
public CombinedCelValueProvider(CelValueProvider first, CelValueProvider second) {
Preconditions.checkNotNull(first);
Preconditions.checkNotNull(second);
celValueProviders = ImmutableList.of(first, second);
Expand Down
35 changes: 22 additions & 13 deletions common/src/main/java/dev/cel/common/values/MapValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelErrorCode;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.types.CelType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.SimpleType;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.jspecify.nullness.Nullable;
Expand All @@ -33,8 +36,8 @@
*/
@Immutable(containerOf = {"K", "V"})
public abstract class MapValue<K extends CelValue, V extends CelValue> extends CelValue
implements Map<K, V> {
implements Map<K, V>, SelectableValue<K> {

private static final MapType MAP_TYPE = MapType.create(SimpleType.DYN, SimpleType.DYN);

@Override
Expand All @@ -48,24 +51,30 @@ public boolean isZeroValue() {
@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
return get((K) key);
return select((K) key);
}

public V get(K key) {
if (!has(key)) {
throw new IllegalArgumentException(
String.format("key '%s' is not present in map.", key.value()));
}
return value().get(key);
@Override
@SuppressWarnings("unchecked")
public V select(K field) {
return (V)
find(field)
.orElseThrow(
() ->
new CelRuntimeException(
new IllegalArgumentException(
String.format("key '%s' is not present in map.", field.value())),
CelErrorCode.ATTRIBUTE_NOT_FOUND));
}

@Override
public CelType celType() {
return MAP_TYPE;
public Optional<CelValue> find(K field) {
return value().containsKey(field) ? Optional.of(value().get(field)) : Optional.empty();
}

public boolean has(K key) {
return value().containsKey(key);
@Override
public CelType celType() {
return MAP_TYPE;
}

/**
Expand Down
54 changes: 13 additions & 41 deletions common/src/main/java/dev/cel/common/values/OptionalValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.jspecify.nullness.Nullable;

/**
Expand All @@ -29,7 +30,8 @@
*/
@AutoValue
@Immutable(containerOf = "E")
public abstract class OptionalValue<E extends CelValue> extends CelValue {
public abstract class OptionalValue<E extends CelValue> extends CelValue
implements SelectableValue<CelValue> {
private static final OptionalType OPTIONAL_TYPE = OptionalType.create(SimpleType.DYN);

/** Sentinel value representing an empty optional ('optional.none()' in CEL) */
Expand Down Expand Up @@ -65,51 +67,21 @@ public OptionalType celType() {
* <li>map[?key] -> key in map ? optional{map[key]} : optional.none()
* </ol>
*/
@SuppressWarnings("unchecked")
public OptionalValue<CelValue> select(CelValue field) {
if (isZeroValue()) {
return EMPTY;
}

CelValue celValue = value();
if (celValue instanceof MapValue) {
MapValue<CelValue, CelValue> mapValue = (MapValue<CelValue, CelValue>) celValue;
if (!mapValue.has(field)) {
return EMPTY;
}

return OptionalValue.create(mapValue.get(field));
} else if (celValue instanceof StructValue) {
StructValue structValue = (StructValue) celValue;
StringValue stringField = (StringValue) field;
if (!structValue.hasField(stringField.value())) {
return EMPTY;
}

return OptionalValue.create(structValue.select(stringField.value()));
}

throw new UnsupportedOperationException("Unsupported select on: " + celValue);
@Override
public CelValue select(CelValue field) {
return find(field).orElse(EMPTY);
}

/** Presence test with optional semantics on maps and structs. */
@SuppressWarnings("unchecked") // Unchecked cast of MapValue flagged due to type erasure.
public boolean hasField(CelValue field) {
@Override
@SuppressWarnings("unchecked")
public Optional<CelValue> find(CelValue field) {
if (isZeroValue()) {
return false;
}

CelValue celValue = value();
if (celValue instanceof MapValue) {
MapValue<CelValue, CelValue> mapValue = (MapValue<CelValue, CelValue>) celValue;
return mapValue.has(field);
} else if (celValue instanceof StructValue) {
StructValue structValue = (StructValue) celValue;
StringValue stringField = (StringValue) field;
return structValue.hasField(stringField.value());
return Optional.empty();
}

throw new UnsupportedOperationException("Unsupported presence test on: " + celValue);
SelectableValue<CelValue> selectableValue = (SelectableValue<CelValue>) value();
Optional<CelValue> selectedField = selectableValue.find(field);
return selectedField.map(OptionalValue::create);
}

public static <E extends CelValue> OptionalValue<E> create(E value) {
Expand Down
30 changes: 19 additions & 11 deletions common/src/main/java/dev/cel/common/values/ProtoMessageValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.types.CelType;
import dev.cel.common.types.StructTypeReference;
import java.util.Optional;

/** ProtoMessageValue is a struct value with protobuf support. */
@AutoValue
@Immutable
public abstract class ProtoMessageValue extends StructValue {
public abstract class ProtoMessageValue extends StructValue<StringValue> {

@Override
public abstract Message value();
Expand All @@ -45,23 +46,30 @@ public boolean isZeroValue() {
}

@Override
public boolean hasField(String fieldName) {
public CelValue select(StringValue field) {
FieldDescriptor fieldDescriptor =
findField(celDescriptorPool(), value().getDescriptorForType(), fieldName);
findField(celDescriptorPool(), value().getDescriptorForType(), field.value());

if (fieldDescriptor.isRepeated()) {
return value().getRepeatedFieldCount(fieldDescriptor) > 0;
}

return value().hasField(fieldDescriptor);
return protoCelValueConverter().fromProtoMessageFieldToCelValue(value(), fieldDescriptor);
}

@Override
public CelValue select(String fieldName) {
public Optional<CelValue> find(StringValue field) {
FieldDescriptor fieldDescriptor =
findField(celDescriptorPool(), value().getDescriptorForType(), fieldName);
findField(celDescriptorPool(), value().getDescriptorForType(), field.value());

return protoCelValueConverter().fromProtoMessageFieldToCelValue(value(), fieldDescriptor);
// Selecting a field on a protobuf message yields a default value even if the field is not
// declared. Therefore, we must exhaustively test whether they are actually declared.
if (fieldDescriptor.isRepeated()) {
if (value().getRepeatedFieldCount(fieldDescriptor) == 0) {
return Optional.empty();
}
} else if (!value().hasField(fieldDescriptor)) {
return Optional.empty();
}

return Optional.of(
protoCelValueConverter().fromProtoMessageFieldToCelValue(value(), fieldDescriptor));
}

public static ProtoMessageValue create(
Expand Down
37 changes: 37 additions & 0 deletions common/src/main/java/dev/cel/common/values/SelectableValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 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.values;

import java.util.Optional;

/**
* SelectableValue is an interface for representing a value that supports field selection and
* presence tests. Few examples are structs, protobuf messages, maps and optional values.
*/
public interface SelectableValue<T extends CelValue> {

/**
* Performs field selection. The behavior depends on the concrete implementation of the value
* being selected. For structs and maps, this must throw an exception if the field does not exist.
* For optional values, this will return an {@code optional.none()}.
*/
CelValue select(T field);

/**
* Finds the field. This will return an {@link Optional#empty()} if the field does not exist. This
* can be used for presence testing.
*/
Optional<CelValue> find(T field);
}
Loading

0 comments on commit 034657d

Please sign in to comment.