diff --git a/common/src/main/java/dev/cel/common/values/BUILD.bazel b/common/src/main/java/dev/cel/common/values/BUILD.bazel index 8fda75ac..c744d69d 100644 --- a/common/src/main/java/dev/cel/common/values/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/values/BUILD.bazel @@ -37,6 +37,7 @@ java_library( ], deps = [ "//common/annotations", + "//common/types:type_providers", "@maven//:com_google_errorprone_error_prone_annotations", ], ) @@ -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", diff --git a/common/src/main/java/dev/cel/common/values/BoolValue.java b/common/src/main/java/dev/cel/common/values/BoolValue.java index 0fe868cc..1327e569 100644 --- a/common/src/main/java/dev/cel/common/values/BoolValue.java +++ b/common/src/main/java/dev/cel/common/values/BoolValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/BytesValue.java b/common/src/main/java/dev/cel/common/values/BytesValue.java index dad78736..d4c30936 100644 --- a/common/src/main/java/dev/cel/common/values/BytesValue.java +++ b/common/src/main/java/dev/cel/common/values/BytesValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/CelValue.java b/common/src/main/java/dev/cel/common/values/CelValue.java index 582338d9..3a3ea6f2 100644 --- a/common/src/main/java/dev/cel/common/values/CelValue.java +++ b/common/src/main/java/dev/cel/common/values/CelValue.java @@ -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 @@ -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() {} } diff --git a/common/src/main/java/dev/cel/common/values/DoubleValue.java b/common/src/main/java/dev/cel/common/values/DoubleValue.java index 952d27c3..97e0c86b 100644 --- a/common/src/main/java/dev/cel/common/values/DoubleValue.java +++ b/common/src/main/java/dev/cel/common/values/DoubleValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/DurationValue.java b/common/src/main/java/dev/cel/common/values/DurationValue.java index f706d30a..702109de 100644 --- a/common/src/main/java/dev/cel/common/values/DurationValue.java +++ b/common/src/main/java/dev/cel/common/values/DurationValue.java @@ -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} */ @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/EnumValue.java b/common/src/main/java/dev/cel/common/values/EnumValue.java index 9aeaacd7..1a8ea3e7 100644 --- a/common/src/main/java/dev/cel/common/values/EnumValue.java +++ b/common/src/main/java/dev/cel/common/values/EnumValue.java @@ -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. @@ -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 > EnumValue create(Enum value) { return new AutoValue_EnumValue<>(value); } diff --git a/common/src/main/java/dev/cel/common/values/ErrorValue.java b/common/src/main/java/dev/cel/common/values/ErrorValue.java index 33807caf..818f8685 100644 --- a/common/src/main/java/dev/cel/common/values/ErrorValue.java +++ b/common/src/main/java/dev/cel/common/values/ErrorValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/IntValue.java b/common/src/main/java/dev/cel/common/values/IntValue.java index c094826f..b756804f 100644 --- a/common/src/main/java/dev/cel/common/values/IntValue.java +++ b/common/src/main/java/dev/cel/common/values/IntValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/ListValue.java b/common/src/main/java/dev/cel/common/values/ListValue.java index 3548aa5d..9cf6aa80 100644 --- a/common/src/main/java/dev/cel/common/values/ListValue.java +++ b/common/src/main/java/dev/cel/common/values/ListValue.java @@ -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; @@ -31,6 +34,7 @@ */ @Immutable public abstract class ListValue extends CelValue implements List { + private static final ListType LIST_TYPE = ListType.create(SimpleType.DYN); @Override @SuppressWarnings("Immutable") // ListValue APIs prohibit mutation. @@ -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. * diff --git a/common/src/main/java/dev/cel/common/values/MapValue.java b/common/src/main/java/dev/cel/common/values/MapValue.java index d6cc4f06..84a913dc 100644 --- a/common/src/main/java/dev/cel/common/values/MapValue.java +++ b/common/src/main/java/dev/cel/common/values/MapValue.java @@ -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; @@ -31,6 +34,8 @@ @Immutable(containerOf = {"K", "V"}) public abstract class MapValue extends CelValue implements Map { + + private static final MapType MAP_TYPE = MapType.create(SimpleType.DYN, SimpleType.DYN); @Override public abstract Map value(); @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/NullValue.java b/common/src/main/java/dev/cel/common/values/NullValue.java index 5fa7924a..75fc25aa 100644 --- a/common/src/main/java/dev/cel/common/values/NullValue.java +++ b/common/src/main/java/dev/cel/common/values/NullValue.java @@ -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 @@ -31,6 +33,11 @@ public NullValue value() { return NULL_VALUE; } + @Override + public CelType celType() { + return SimpleType.NULL_TYPE; + } + @Override public boolean isZeroValue() { return true; diff --git a/common/src/main/java/dev/cel/common/values/OpaqueValue.java b/common/src/main/java/dev/cel/common/values/OpaqueValue.java index a5c4a417..3350d05d 100644 --- a/common/src/main/java/dev/cel/common/values/OpaqueValue.java +++ b/common/src/main/java/dev/cel/common/values/OpaqueValue.java @@ -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 @@ -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)); } } diff --git a/common/src/main/java/dev/cel/common/values/OptionalValue.java b/common/src/main/java/dev/cel/common/values/OptionalValue.java index 8f722958..cf6247a9 100644 --- a/common/src/main/java/dev/cel/common/values/OptionalValue.java +++ b/common/src/main/java/dev/cel/common/values/OptionalValue.java @@ -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; @@ -28,6 +30,7 @@ @AutoValue @Immutable(containerOf = "E") public abstract class OptionalValue 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 EMPTY = empty(); @@ -49,6 +52,11 @@ public boolean isZeroValue() { return innerValue() == null; } + @Override + public OptionalType celType() { + return OPTIONAL_TYPE; + } + /** * Optional field selection on maps or structs. * diff --git a/common/src/main/java/dev/cel/common/values/StringValue.java b/common/src/main/java/dev/cel/common/values/StringValue.java index 217f2cd5..e14c8979 100644 --- a/common/src/main/java/dev/cel/common/values/StringValue.java +++ b/common/src/main/java/dev/cel/common/values/StringValue.java @@ -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 @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/TimestampValue.java b/common/src/main/java/dev/cel/common/values/TimestampValue.java index 34f36604..a03ee2ed 100644 --- a/common/src/main/java/dev/cel/common/values/TimestampValue.java +++ b/common/src/main/java/dev/cel/common/values/TimestampValue.java @@ -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} */ @@ -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); } diff --git a/common/src/main/java/dev/cel/common/values/TypeValue.java b/common/src/main/java/dev/cel/common/values/TypeValue.java index 16f80345..cacffcd4 100644 --- a/common/src/main/java/dev/cel/common/values/TypeValue.java +++ b/common/src/main/java/dev/cel/common/values/TypeValue.java @@ -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 @@ -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)); } } diff --git a/common/src/main/java/dev/cel/common/values/UintValue.java b/common/src/main/java/dev/cel/common/values/UintValue.java index 0252373d..c376c76e 100644 --- a/common/src/main/java/dev/cel/common/values/UintValue.java +++ b/common/src/main/java/dev/cel/common/values/UintValue.java @@ -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 @@ -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); } diff --git a/common/src/test/java/dev/cel/common/values/BUILD.bazel b/common/src/test/java/dev/cel/common/values/BUILD.bazel index 57c07aca..5f95307a 100644 --- a/common/src/test/java/dev/cel/common/values/BUILD.bazel +++ b/common/src/test/java/dev/cel/common/values/BUILD.bazel @@ -9,6 +9,7 @@ java_library( deps = [ "//:java_truth", "//common/types", + "//common/types:type_providers", "//common/values", "//common/values:cel_byte_string", "//common/values:cel_value", diff --git a/common/src/test/java/dev/cel/common/values/BoolValueTest.java b/common/src/test/java/dev/cel/common/values/BoolValueTest.java index ada0e442..dbe9ab57 100644 --- a/common/src/test/java/dev/cel/common/values/BoolValueTest.java +++ b/common/src/test/java/dev/cel/common/values/BoolValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -40,6 +41,13 @@ public void trueBool() { assertThat(boolValue.isZeroValue()).isFalse(); } + @Test + public void celTypeTest() { + BoolValue value = BoolValue.create(true); + + assertThat(value.celType()).isEqualTo(SimpleType.BOOL); + } + @Test public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> BoolValue.create(null)); diff --git a/common/src/test/java/dev/cel/common/values/BytesValueTest.java b/common/src/test/java/dev/cel/common/values/BytesValueTest.java index 019dfc8e..115838a0 100644 --- a/common/src/test/java/dev/cel/common/values/BytesValueTest.java +++ b/common/src/test/java/dev/cel/common/values/BytesValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -40,6 +41,13 @@ public void constructBytes() { assertThat(bytesValue.isZeroValue()).isFalse(); } + @Test + public void celTypeTest() { + BytesValue value = BytesValue.create(CelByteString.EMPTY); + + assertThat(value.celType()).isEqualTo(SimpleType.BYTES); + } + @Test public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> BytesValue.create(null)); diff --git a/common/src/test/java/dev/cel/common/values/DoubleValueTest.java b/common/src/test/java/dev/cel/common/values/DoubleValueTest.java index 9d0f3263..f2c8374a 100644 --- a/common/src/test/java/dev/cel/common/values/DoubleValueTest.java +++ b/common/src/test/java/dev/cel/common/values/DoubleValueTest.java @@ -18,6 +18,7 @@ import com.google.common.testing.ClassSanityTester; import com.google.common.testing.EqualsTester; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -41,6 +42,13 @@ public void constructDouble() { assertThat(doubleValue.isZeroValue()).isFalse(); } + @Test + public void celTypeTest() { + DoubleValue value = DoubleValue.create(0.0d); + + assertThat(value.celType()).isEqualTo(SimpleType.DOUBLE); + } + @Test public void equalityTest() { new EqualsTester() diff --git a/common/src/test/java/dev/cel/common/values/DurationValueTest.java b/common/src/test/java/dev/cel/common/values/DurationValueTest.java index 8ad721cb..e53e7537 100644 --- a/common/src/test/java/dev/cel/common/values/DurationValueTest.java +++ b/common/src/test/java/dev/cel/common/values/DurationValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,6 +42,13 @@ public void constructDuration() { assertThat(durationValue.isZeroValue()).isFalse(); } + @Test + public void celTypeTest() { + DurationValue value = DurationValue.create(Duration.ZERO); + + assertThat(value.celType()).isEqualTo(SimpleType.DURATION); + } + @Test public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> DurationValue.create(null)); diff --git a/common/src/test/java/dev/cel/common/values/EnumValueTest.java b/common/src/test/java/dev/cel/common/values/EnumValueTest.java index d10c6cf3..d9fde4b7 100644 --- a/common/src/test/java/dev/cel/common/values/EnumValueTest.java +++ b/common/src/test/java/dev/cel/common/values/EnumValueTest.java @@ -16,6 +16,7 @@ import static com.google.common.truth.Truth.assertThat; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,4 +43,11 @@ public void enumValue_isZeroValue_returnsFalse() { assertThat(EnumValue.create(TestKind.ONE).isZeroValue()).isFalse(); assertThat(EnumValue.create(TestKind.TWO).isZeroValue()).isFalse(); } + + @Test + public void celTypeTest() { + EnumValue value = EnumValue.create(TestKind.ONE); + + assertThat(value.celType()).isEqualTo(SimpleType.INT); + } } diff --git a/common/src/test/java/dev/cel/common/values/ErrorValueTest.java b/common/src/test/java/dev/cel/common/values/ErrorValueTest.java index ce536b83..0db711f7 100644 --- a/common/src/test/java/dev/cel/common/values/ErrorValueTest.java +++ b/common/src/test/java/dev/cel/common/values/ErrorValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -36,4 +37,11 @@ public void errorValue_construct() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> ErrorValue.create(null)); } + + @Test + public void celTypeTest() { + ErrorValue value = ErrorValue.create(new IllegalArgumentException("test")); + + assertThat(value.celType()).isEqualTo(SimpleType.ERROR); + } } diff --git a/common/src/test/java/dev/cel/common/values/ImmutableListValueTest.java b/common/src/test/java/dev/cel/common/values/ImmutableListValueTest.java index 6b8915a0..1d5099ec 100644 --- a/common/src/test/java/dev/cel/common/values/ImmutableListValueTest.java +++ b/common/src/test/java/dev/cel/common/values/ImmutableListValueTest.java @@ -18,6 +18,8 @@ import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; +import dev.cel.common.types.ListType; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -61,4 +63,11 @@ public void listValue_mixedTypes() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> ImmutableListValue.create(null)); } + + @Test + public void celTypeTest() { + ListValue value = ImmutableListValue.create(ImmutableList.of()); + + assertThat(value.celType()).isEqualTo(ListType.create(SimpleType.DYN)); + } } diff --git a/common/src/test/java/dev/cel/common/values/ImmutableMapValueTest.java b/common/src/test/java/dev/cel/common/values/ImmutableMapValueTest.java index c2409776..fe3c1138 100644 --- a/common/src/test/java/dev/cel/common/values/ImmutableMapValueTest.java +++ b/common/src/test/java/dev/cel/common/values/ImmutableMapValueTest.java @@ -20,6 +20,8 @@ import com.google.common.collect.ImmutableMap; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; +import dev.cel.common.types.MapType; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; @@ -96,4 +98,11 @@ public void mapValue_mixedTypes() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> ImmutableMapValue.create(null)); } + + @Test + public void celTypeTest() { + MapValue value = ImmutableMapValue.create(ImmutableMap.of()); + + assertThat(value.celType()).isEqualTo(MapType.create(SimpleType.DYN, SimpleType.DYN)); + } } diff --git a/common/src/test/java/dev/cel/common/values/IntValueTest.java b/common/src/test/java/dev/cel/common/values/IntValueTest.java index 404a2a06..fa2b1559 100644 --- a/common/src/test/java/dev/cel/common/values/IntValueTest.java +++ b/common/src/test/java/dev/cel/common/values/IntValueTest.java @@ -18,6 +18,7 @@ import com.google.common.testing.ClassSanityTester; import com.google.common.testing.EqualsTester; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -70,4 +71,11 @@ public void hashCode_smokeTest() { assertThat(IntValue.create(Long.MAX_VALUE).hashCode()).isEqualTo(-2146483645); assertThat(IntValue.create(Long.MIN_VALUE).hashCode()).isEqualTo(-2146483645); } + + @Test + public void celTypeTest() { + IntValue value = IntValue.create(0); + + assertThat(value.celType()).isEqualTo(SimpleType.INT); + } } diff --git a/common/src/test/java/dev/cel/common/values/NullValueTest.java b/common/src/test/java/dev/cel/common/values/NullValueTest.java index 2f05aca0..a5bafe83 100644 --- a/common/src/test/java/dev/cel/common/values/NullValueTest.java +++ b/common/src/test/java/dev/cel/common/values/NullValueTest.java @@ -16,6 +16,7 @@ import static com.google.common.truth.Truth.assertThat; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -28,4 +29,11 @@ public void nullValueTest() { assertThat(NullValue.NULL_VALUE.value()).isEqualTo(NullValue.NULL_VALUE); assertThat(NullValue.NULL_VALUE.isZeroValue()).isTrue(); } + + @Test + public void celTypeTest() { + NullValue value = NullValue.NULL_VALUE; + + assertThat(value.celType()).isEqualTo(SimpleType.NULL_TYPE); + } } diff --git a/common/src/test/java/dev/cel/common/values/OpaqueValueTest.java b/common/src/test/java/dev/cel/common/values/OpaqueValueTest.java index 68f39fab..d97bcd28 100644 --- a/common/src/test/java/dev/cel/common/values/OpaqueValueTest.java +++ b/common/src/test/java/dev/cel/common/values/OpaqueValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.OpaqueType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -25,14 +26,15 @@ public class OpaqueValueTest { @Test public void opaqueValue_construct() { - OpaqueValue opaqueValue = OpaqueValue.create("test"); + OpaqueValue opaqueValue = OpaqueValue.create("opaque_type_name", "test"); assertThat(opaqueValue.value()).isEqualTo("test"); assertThat(opaqueValue.isZeroValue()).isFalse(); + assertThat(opaqueValue.celType()).isEqualTo(OpaqueType.create("opaque_type_name")); } @Test public void create_nullValue_throws() { - assertThrows(NullPointerException.class, () -> OpaqueValue.create(null)); + assertThrows(NullPointerException.class, () -> OpaqueValue.create("opaque_type_name", null)); } } diff --git a/common/src/test/java/dev/cel/common/values/OptionalValueTest.java b/common/src/test/java/dev/cel/common/values/OptionalValueTest.java index 452ff047..26af72bc 100644 --- a/common/src/test/java/dev/cel/common/values/OptionalValueTest.java +++ b/common/src/test/java/dev/cel/common/values/OptionalValueTest.java @@ -20,6 +20,10 @@ import com.google.common.collect.ImmutableMap; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; +import dev.cel.common.types.CelType; +import dev.cel.common.types.OptionalType; +import dev.cel.common.types.SimpleType; +import dev.cel.common.types.StructTypeReference; import java.util.NoSuchElementException; import org.junit.Test; import org.junit.runner.RunWith; @@ -126,6 +130,13 @@ public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> OptionalValue.create(null)); } + @Test + public void celTypeTest() { + OptionalValue value = OptionalValue.EMPTY; + + assertThat(value.celType()).isEqualTo(OptionalType.create(SimpleType.DYN)); + } + @SuppressWarnings("Immutable") // Test only private static class CelCustomStruct extends StructValue { private final long data; @@ -140,6 +151,11 @@ public boolean isZeroValue() { return data == 0; } + @Override + public CelType celType() { + return StructTypeReference.create("customStruct"); + } + @Override public CelValue select(String fieldName) { if (fieldName.equals("data")) { diff --git a/common/src/test/java/dev/cel/common/values/StringValueTest.java b/common/src/test/java/dev/cel/common/values/StringValueTest.java index 6cae3f62..6a0a8113 100644 --- a/common/src/test/java/dev/cel/common/values/StringValueTest.java +++ b/common/src/test/java/dev/cel/common/values/StringValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -52,4 +53,11 @@ public void constructString() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> StringValue.create(null)); } + + @Test + public void celTypeTest() { + StringValue value = StringValue.create(""); + + assertThat(value.celType()).isEqualTo(SimpleType.STRING); + } } diff --git a/common/src/test/java/dev/cel/common/values/StructValueTest.java b/common/src/test/java/dev/cel/common/values/StructValueTest.java index c3a181b3..98a1a560 100644 --- a/common/src/test/java/dev/cel/common/values/StructValueTest.java +++ b/common/src/test/java/dev/cel/common/values/StructValueTest.java @@ -19,6 +19,8 @@ import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; +import dev.cel.common.types.CelType; +import dev.cel.common.types.StructTypeReference; import org.junit.Test; import org.junit.runner.RunWith; @@ -69,6 +71,14 @@ public void hasField_success(String fieldName, boolean expectedResult) { assertThat(celCustomStruct.hasField(fieldName)).isEqualTo(expectedResult); } + @Test + public void celTypeTest() { + UserDefinedClass userDefinedPojo = new UserDefinedClass(0); + CelCustomStruct value = new CelCustomStruct(userDefinedPojo); + + assertThat(value.celType()).isEqualTo(StructTypeReference.create("customStruct")); + } + private static class UserDefinedClass { private final long data; @@ -91,6 +101,11 @@ public boolean isZeroValue() { return userDefinedClass.data == 0; } + @Override + public CelType celType() { + return StructTypeReference.create("customStruct"); + } + @Override public CelValue select(String fieldName) { if (fieldName.equals("data")) { diff --git a/common/src/test/java/dev/cel/common/values/TimestampValueTest.java b/common/src/test/java/dev/cel/common/values/TimestampValueTest.java index ad82bad6..c2046653 100644 --- a/common/src/test/java/dev/cel/common/values/TimestampValueTest.java +++ b/common/src/test/java/dev/cel/common/values/TimestampValueTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import dev.cel.common.types.SimpleType; import java.time.Instant; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,4 +46,11 @@ public void constructTimestamp() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> TimestampValue.create(null)); } + + @Test + public void celTypeTest() { + TimestampValue value = TimestampValue.create(Instant.ofEpochSecond(0)); + + assertThat(value.celType()).isEqualTo(SimpleType.TIMESTAMP); + } } diff --git a/common/src/test/java/dev/cel/common/values/TypeValueTest.java b/common/src/test/java/dev/cel/common/values/TypeValueTest.java index 070f5368..7f8ab3eb 100644 --- a/common/src/test/java/dev/cel/common/values/TypeValueTest.java +++ b/common/src/test/java/dev/cel/common/values/TypeValueTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertThrows; import dev.cel.common.types.SimpleType; +import dev.cel.common.types.TypeType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -37,4 +38,11 @@ public void constructTypeValue() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> TypeValue.create(null)); } + + @Test + public void celTypeTest() { + TypeValue value = TypeValue.create(SimpleType.INT); + + assertThat(value.celType()).isEqualTo(TypeType.create(SimpleType.INT)); + } } diff --git a/common/src/test/java/dev/cel/common/values/UintValueTest.java b/common/src/test/java/dev/cel/common/values/UintValueTest.java index e3ddc96d..2d073b46 100644 --- a/common/src/test/java/dev/cel/common/values/UintValueTest.java +++ b/common/src/test/java/dev/cel/common/values/UintValueTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertThrows; import com.google.common.primitives.UnsignedLong; +import dev.cel.common.types.SimpleType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -45,4 +46,11 @@ public void constructUint() { public void create_nullValue_throws() { assertThrows(NullPointerException.class, () -> UintValue.create(null)); } + + @Test + public void celTypeTest() { + UintValue value = UintValue.create(UnsignedLong.valueOf(0L)); + + assertThat(value.celType()).isEqualTo(SimpleType.UINT); + } }