Skip to content

Commit 86b38a5

Browse files
l46kokcopybara-github
authored andcommitted
Add CelType as a property to CelValue
PiperOrigin-RevId: 584665130
1 parent 1d38ec6 commit 86b38a5

36 files changed

+272
-6
lines changed

common/src/main/java/dev/cel/common/values/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ java_library(
3737
],
3838
deps = [
3939
"//common/annotations",
40+
"//common/types:type_providers",
4041
"@maven//:com_google_errorprone_error_prone_annotations",
4142
],
4243
)
@@ -51,6 +52,7 @@ java_library(
5152
":cel_value",
5253
"//:auto_value",
5354
"//common/annotations",
55+
"//common/types",
5456
"//common/types:type_providers",
5557
"@maven//:com_google_errorprone_error_prone_annotations",
5658
"@maven//:com_google_guava_guava",

common/src/main/java/dev/cel/common/values/BoolValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.common.types.CelType;
20+
import dev.cel.common.types.SimpleType;
1921

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

35+
@Override
36+
public CelType celType() {
37+
return SimpleType.BOOL;
38+
}
39+
3340
public static BoolValue create(Boolean value) {
3441
return new AutoValue_BoolValue(value);
3542
}

common/src/main/java/dev/cel/common/values/BytesValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.common.types.CelType;
20+
import dev.cel.common.types.SimpleType;
1921

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

35+
@Override
36+
public CelType celType() {
37+
return SimpleType.BYTES;
38+
}
39+
3340
public static BytesValue create(CelByteString value) {
3441
return new AutoValue_BytesValue(value);
3542
}

common/src/main/java/dev/cel/common/values/CelValue.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.errorprone.annotations.Immutable;
1818
import dev.cel.common.annotations.Internal;
19+
import dev.cel.common.types.CelType;
1920

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

39+
/** The CelType that represents this value. */
40+
public abstract CelType celType();
41+
3842
// TOOD(b/309695452): Add CelEquals method
39-
// TODO: Add a getter for CelType
4043

4144
public CelValue() {}
4245
}

common/src/main/java/dev/cel/common/values/DoubleValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package dev.cel.common.values;
1616

1717
import com.google.errorprone.annotations.Immutable;
18+
import dev.cel.common.types.CelType;
19+
import dev.cel.common.types.SimpleType;
1820

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

40+
@Override
41+
public CelType celType() {
42+
return SimpleType.DOUBLE;
43+
}
44+
3845
public static DoubleValue create(double value) {
3946
return new DoubleValue(value);
4047
}

common/src/main/java/dev/cel/common/values/DurationValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.common.types.CelType;
20+
import dev.cel.common.types.SimpleType;
1921
import java.time.Duration;
2022

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

36+
@Override
37+
public CelType celType() {
38+
return SimpleType.DURATION;
39+
}
40+
3441
public static DurationValue create(Duration value) {
3542
return new AutoValue_DurationValue(value);
3643
}

common/src/main/java/dev/cel/common/values/EnumValue.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.common.types.CelType;
20+
import dev.cel.common.types.SimpleType;
1921

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

40+
@Override
41+
public CelType celType() {
42+
// (b/178627883) Strongly typed enum is not supported yet
43+
return SimpleType.INT;
44+
}
45+
3846
public static <E extends Enum<E>> EnumValue<E> create(Enum<E> value) {
3947
return new AutoValue_EnumValue<>(value);
4048
}

common/src/main/java/dev/cel/common/values/ErrorValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import dev.cel.common.annotations.Internal;
19+
import dev.cel.common.types.CelType;
20+
import dev.cel.common.types.SimpleType;
1921

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

44+
@Override
45+
public CelType celType() {
46+
return SimpleType.ERROR;
47+
}
48+
4249
public static ErrorValue create(Exception value) {
4350
return new AutoValue_ErrorValue(value);
4451
}

common/src/main/java/dev/cel/common/values/IntValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package dev.cel.common.values;
1616

1717
import com.google.errorprone.annotations.Immutable;
18+
import dev.cel.common.types.CelType;
19+
import dev.cel.common.types.SimpleType;
1820

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

40+
@Override
41+
public CelType celType() {
42+
return SimpleType.INT;
43+
}
44+
3845
public static IntValue create(long value) {
3946
return new IntValue(value);
4047
}

common/src/main/java/dev/cel/common/values/ListValue.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1818
import com.google.errorprone.annotations.DoNotCall;
1919
import com.google.errorprone.annotations.Immutable;
20+
import dev.cel.common.types.CelType;
21+
import dev.cel.common.types.ListType;
22+
import dev.cel.common.types.SimpleType;
2023
import java.util.Collection;
2124
import java.util.Comparator;
2225
import java.util.List;
@@ -31,6 +34,7 @@
3134
*/
3235
@Immutable
3336
public abstract class ListValue<E extends CelValue> extends CelValue implements List<E> {
37+
private static final ListType LIST_TYPE = ListType.create(SimpleType.DYN);
3438

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

48+
@Override
49+
public CelType celType() {
50+
return LIST_TYPE;
51+
}
52+
4453
/**
4554
* Guaranteed to throw an exception and leave the list unmodified.
4655
*

0 commit comments

Comments
 (0)