Skip to content

Commit

Permalink
Add CelType as a property to CelValue
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 584665130
  • Loading branch information
l46kok authored and copybara-github committed Nov 22, 2023
1 parent 1d38ec6 commit 86b38a5
Show file tree
Hide file tree
Showing 36 changed files with 272 additions and 6 deletions.
2 changes: 2 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 @@ -37,6 +37,7 @@ java_library(
],
deps = [
"//common/annotations",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)
Expand All @@ -51,6 +52,7 @@ java_library(
":cel_value",
"//:auto_value",
"//common/annotations",
"//common/types",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/BoolValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/** BoolValue is a simple CelValue wrapper around Java booleans. */
@AutoValue
Expand All @@ -30,6 +32,11 @@ public boolean isZeroValue() {
return !value();
}

@Override
public CelType celType() {
return SimpleType.BOOL;
}

public static BoolValue create(Boolean value) {
return new AutoValue_BoolValue(value);
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/BytesValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/** BytesValue is a simple CelValue wrapper around CelByteString (immutable byte string). */
@AutoValue
Expand All @@ -30,6 +32,11 @@ public boolean isZeroValue() {
return value().isEmpty();
}

@Override
public CelType celType() {
return SimpleType.BYTES;
}

public static BytesValue create(CelByteString value) {
return new AutoValue_BytesValue(value);
}
Expand Down
5 changes: 4 additions & 1 deletion common/src/main/java/dev/cel/common/values/CelValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;
import dev.cel.common.types.CelType;

/**
* A representation of a CEL value for the runtime. Clients should never directly extend from
Expand All @@ -35,8 +36,10 @@ public abstract class CelValue {
/** Returns true if the {@link #value()} is a zero value for its type. */
public abstract boolean isZeroValue();

/** The CelType that represents this value. */
public abstract CelType celType();

// TOOD(b/309695452): Add CelEquals method
// TODO: Add a getter for CelType

public CelValue() {}
}
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/DoubleValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package dev.cel.common.values;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/** DoubleValue is a simple CelValue wrapper around Java doubles. */
@Immutable
Expand All @@ -35,6 +37,11 @@ public boolean isZeroValue() {
return value() == 0;
}

@Override
public CelType celType() {
return SimpleType.DOUBLE;
}

public static DoubleValue create(double value) {
return new DoubleValue(value);
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/DurationValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;

/** DurationValue is a simple CelValue wrapper around {@link java.time.Duration} */
Expand All @@ -31,6 +33,11 @@ public boolean isZeroValue() {
return value().isZero();
}

@Override
public CelType celType() {
return SimpleType.DURATION;
}

public static DurationValue create(Duration value) {
return new AutoValue_DurationValue(value);
}
Expand Down
8 changes: 8 additions & 0 deletions common/src/main/java/dev/cel/common/values/EnumValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/**
* EnumValue is a simple CelValue wrapper around Java enums.
Expand All @@ -35,6 +37,12 @@ public boolean isZeroValue() {
return false;
}

@Override
public CelType celType() {
// (b/178627883) Strongly typed enum is not supported yet
return SimpleType.INT;
}

public static <E extends Enum<E>> EnumValue<E> create(Enum<E> value) {
return new AutoValue_EnumValue<>(value);
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/ErrorValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import dev.cel.common.annotations.Internal;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/**
* CelErrorValue represent the intermediate error that occurs during evaluation in the form of Java
Expand All @@ -39,6 +41,11 @@ public boolean isZeroValue() {
return false;
}

@Override
public CelType celType() {
return SimpleType.ERROR;
}

public static ErrorValue create(Exception value) {
return new AutoValue_ErrorValue(value);
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/IntValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package dev.cel.common.values;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/** IntValue is a simple CelValue wrapper around Java longs. */
@Immutable
Expand All @@ -35,6 +37,11 @@ public boolean isZeroValue() {
return value() == 0;
}

@Override
public CelType celType() {
return SimpleType.INT;
}

public static IntValue create(long value) {
return new IntValue(value);
}
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/dev/cel/common/values/ListValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.SimpleType;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
Expand All @@ -31,6 +34,7 @@
*/
@Immutable
public abstract class ListValue<E extends CelValue> extends CelValue implements List<E> {
private static final ListType LIST_TYPE = ListType.create(SimpleType.DYN);

@Override
@SuppressWarnings("Immutable") // ListValue APIs prohibit mutation.
Expand All @@ -41,6 +45,11 @@ public boolean isZeroValue() {
return isEmpty();
}

@Override
public CelType celType() {
return LIST_TYPE;
}

/**
* Guaranteed to throw an exception and leave the list unmodified.
*
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/dev/cel/common/values/MapValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.Immutable;
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.function.BiFunction;
import java.util.function.Function;
Expand All @@ -31,6 +34,8 @@
@Immutable(containerOf = {"K", "V"})
public abstract class MapValue<K extends CelValue, V extends CelValue> extends CelValue
implements Map<K, V> {

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

@Override
public abstract Map<K, V> value();
Expand All @@ -54,6 +59,11 @@ public V get(K key) {
return value().get(key);
}

@Override
public CelType celType() {
return MAP_TYPE;
}

public boolean has(K key) {
return value().containsKey(key);
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/NullValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package dev.cel.common.values;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/**
* NullValue represents the value 'null' of 'null_type' according to the CEL specification. One of
Expand All @@ -31,6 +33,11 @@ public NullValue value() {
return NULL_VALUE;
}

@Override
public CelType celType() {
return SimpleType.NULL_TYPE;
}

@Override
public boolean isZeroValue() {
return true;
Expand Down
8 changes: 6 additions & 2 deletions common/src/main/java/dev/cel/common/values/OpaqueValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dev.cel.common.values;

import com.google.auto.value.AutoValue;
import dev.cel.common.types.OpaqueType;

/** OpaqueValue is the value representation of OpaqueType. */
@AutoValue
Expand All @@ -27,7 +28,10 @@ public boolean isZeroValue() {
return false;
}

public static OpaqueValue create(Object value) {
return new AutoValue_OpaqueValue(value);
@Override
public abstract OpaqueType celType();

public static OpaqueValue create(String name, Object value) {
return new AutoValue_OpaqueValue(value, OpaqueType.create(name));
}
}
8 changes: 8 additions & 0 deletions common/src/main/java/dev/cel/common/values/OptionalValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.util.NoSuchElementException;
import org.jspecify.nullness.Nullable;

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

/** Sentinel value representing an empty optional ('optional.none()' in CEL) */
public static final OptionalValue<CelValue> EMPTY = empty();
Expand All @@ -49,6 +52,11 @@ public boolean isZeroValue() {
return innerValue() == null;
}

@Override
public OptionalType celType() {
return OPTIONAL_TYPE;
}

/**
* Optional field selection on maps or structs.
*
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/StringValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/** StringValue is a simple CelValue wrapper around Java strings. */
@AutoValue
Expand All @@ -30,6 +32,11 @@ public boolean isZeroValue() {
return value().isEmpty();
}

@Override
public CelType celType() {
return SimpleType.STRING;
}

public static StringValue create(String value) {
return new AutoValue_StringValue(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;
import java.time.Instant;

/** TimestampValue is a simple CelValue wrapper around {@link java.time.Instant} */
Expand All @@ -31,6 +33,11 @@ public boolean isZeroValue() {
return Instant.EPOCH.equals(value());
}

@Override
public CelType celType() {
return SimpleType.TIMESTAMP;
}

public static TimestampValue create(Instant value) {
return new AutoValue_TimestampValue(value);
}
Expand Down
6 changes: 5 additions & 1 deletion common/src/main/java/dev/cel/common/values/TypeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.TypeType;

/** TypeValue holds the CEL type information for the underlying CelValue. */
@AutoValue
Expand All @@ -31,7 +32,10 @@ public boolean isZeroValue() {
return false;
}

@Override
public abstract TypeType celType();

public static TypeValue create(CelType value) {
return new AutoValue_TypeValue(value);
return new AutoValue_TypeValue(value, TypeType.create(value));
}
}
7 changes: 7 additions & 0 deletions common/src/main/java/dev/cel/common/values/UintValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.auto.value.AutoValue;
import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;

/**
* UintValue represents CelValue for unsigned longs, leveraging Guava's implementation of {@link
Expand All @@ -37,6 +39,11 @@ public boolean isZeroValue() {
return UnsignedLong.ZERO.equals(value());
}

@Override
public CelType celType() {
return SimpleType.UINT;
}

public static UintValue create(UnsignedLong value) {
return new AutoValue_UintValue(value);
}
Expand Down
Loading

0 comments on commit 86b38a5

Please sign in to comment.