Skip to content
Merged
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: 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
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);
}
23 changes: 6 additions & 17 deletions common/src/main/java/dev/cel/common/values/StructValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,11 @@
* <p>Users may extend from this class to provide a custom struct that CEL can understand (ex:
* POJOs). Custom struct implementations must provide all functionalities denoted in the CEL
* specification, such as field selection, presence testing and new object creation.
*
* <p>For an expression `e` selecting a field `f`, `e.f` must throw an exception if `f` does not
* exist in the struct (i.e: hasField returns false). If the field exists but is not set, the
* implementation should return an appropriate default value based on the struct's semantics.
*/
@Immutable
public abstract class StructValue extends CelValue {

/**
* Performs field selection. For an expression `e` selecting a field `f`, `e.f` must throw an
* exception if `f` does not exist in the struct (i.e: hasField returns false). If the field
* exists but is not set, the implementation should return an appropriate default value based on
* the struct's semantics.
*/
public abstract CelValue select(String fieldName);

/**
* Performs presence test on a field.
*
* @return true iff the field exists in the struct.
*/
public abstract boolean hasField(String fieldName);
}
public abstract class StructValue<T extends CelValue> extends CelValue
implements SelectableValue<T> {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.types.MapType;
import dev.cel.common.types.SimpleType;
import org.junit.Test;
Expand Down Expand Up @@ -65,21 +66,21 @@ public void get_nonExistentKey_throws() {
ImmutableMapValue<IntValue, StringValue> mapValue =
ImmutableMapValue.create(ImmutableMap.of(one, hello));

IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> mapValue.get(IntValue.create(100L)));
assertThat(exception).hasMessageThat().isEqualTo("key '100' is not present in map.");
CelRuntimeException exception =
assertThrows(CelRuntimeException.class, () -> mapValue.get(IntValue.create(100L)));
assertThat(exception).hasMessageThat().contains("key '100' is not present in map.");
}

@Test
@TestParameters("{key: 1, expectedResult: true}")
@TestParameters("{key: 100, expectedResult: false}")
public void has_success(long key, boolean expectedResult) {
public void find_success(long key, boolean expectedResult) {
IntValue one = IntValue.create(1L);
StringValue hello = StringValue.create("hello");
ImmutableMapValue<IntValue, StringValue> mapValue =
ImmutableMapValue.create(ImmutableMap.of(one, hello));

assertThat(mapValue.has(IntValue.create(key))).isEqualTo(expectedResult);
assertThat(mapValue.find(IntValue.create(key)).isPresent()).isEqualTo(expectedResult);
}

@Test
Expand Down
Loading