Skip to content

Commit be94585

Browse files
l46kokcopybara-github
authored andcommitted
Evaluate CEL's null and bytes to their native equivalent types
PiperOrigin-RevId: 793812620
1 parent 5b06cc0 commit be94585

60 files changed

Lines changed: 337 additions & 172 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bundle/src/test/java/dev/cel/bundle/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ java_library(
4141
"//common/types:cel_proto_types",
4242
"//common/types:message_type_provider",
4343
"//common/types:type_providers",
44+
"//common/values",
45+
"//common/values:cel_byte_string",
4446
"//compiler",
4547
"//compiler:compiler_builder",
4648
"//extensions",

bundle/src/test/java/dev/cel/bundle/CelImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@
4040
import com.google.common.collect.ImmutableSet;
4141
import com.google.common.util.concurrent.MoreExecutors;
4242
import com.google.protobuf.Any;
43-
import com.google.protobuf.ByteString;
4443
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
4544
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
4645
import com.google.protobuf.Descriptors.FileDescriptor;
4746
import com.google.protobuf.Duration;
4847
import com.google.protobuf.Empty;
4948
import com.google.protobuf.FieldMask;
5049
import com.google.protobuf.Message;
51-
import com.google.protobuf.NullValue;
5250
import com.google.protobuf.Struct;
5351
import com.google.protobuf.TextFormat;
5452
import com.google.protobuf.Timestamp;
@@ -86,6 +84,8 @@
8684
import dev.cel.common.types.ProtoMessageTypeProvider;
8785
import dev.cel.common.types.SimpleType;
8886
import dev.cel.common.types.StructTypeReference;
87+
import dev.cel.common.values.CelByteString;
88+
import dev.cel.common.values.NullValue;
8989
import dev.cel.compiler.CelCompiler;
9090
import dev.cel.compiler.CelCompilerFactory;
9191
import dev.cel.compiler.CelCompilerImpl;
@@ -1654,7 +1654,7 @@ public void programAdvanceEvaluation_unsupportedIndexIgnored() throws Exception
16541654
UnknownContext.create(
16551655
fromMap(
16561656
ImmutableMap.of(
1657-
"unk", ImmutableMap.of(ByteString.copyFromUtf8("a"), false))),
1657+
"unk", ImmutableMap.of(CelByteString.copyFromUtf8("a"), false))),
16581658
ImmutableList.of())))
16591659
.isEqualTo(false);
16601660
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ java_library(
206206
],
207207
deps = [
208208
"//common/internal:proto_time_utils",
209+
"//common/values",
210+
"//common/values:cel_byte_string",
209211
"@maven//:com_google_errorprone_error_prone_annotations",
210212
"@maven//:com_google_guava_guava",
211213
"@maven//:com_google_protobuf_protobuf_java",

common/src/main/java/dev/cel/common/CelProtoJsonAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.base.Joiner;
1919
import com.google.common.primitives.UnsignedLong;
2020
import com.google.errorprone.annotations.Immutable;
21-
import com.google.protobuf.ByteString;
2221
import com.google.protobuf.Duration;
2322
import com.google.protobuf.Empty;
2423
import com.google.protobuf.FieldMask;
@@ -28,6 +27,7 @@
2827
import com.google.protobuf.Timestamp;
2928
import com.google.protobuf.Value;
3029
import dev.cel.common.internal.ProtoTimeUtils;
30+
import dev.cel.common.values.CelByteString;
3131
import java.util.ArrayList;
3232
import java.util.Base64;
3333
import java.util.List;
@@ -71,7 +71,7 @@ public static <K extends String, V> Struct adaptToJsonStructValue(Map<K, V> map)
7171
@SuppressWarnings("unchecked")
7272
public static Value adaptValueToJsonValue(Object value) {
7373
Value.Builder json = Value.newBuilder();
74-
if (value == null || value instanceof NullValue) {
74+
if (value == null || value instanceof dev.cel.common.values.NullValue) {
7575
return json.setNullValue(NullValue.NULL_VALUE).build();
7676
}
7777
if (value instanceof Boolean) {
@@ -93,9 +93,9 @@ public static Value adaptValueToJsonValue(Object value) {
9393
if (value instanceof Float || value instanceof Double) {
9494
return json.setNumberValue(((Number) value).doubleValue()).build();
9595
}
96-
if (value instanceof ByteString) {
96+
if (value instanceof CelByteString) {
9797
return json.setStringValue(
98-
Base64.getEncoder().encodeToString(((ByteString) value).toByteArray()))
98+
Base64.getEncoder().encodeToString(((CelByteString) value).toByteArray()))
9999
.build();
100100
}
101101
if (value instanceof String) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ java_library(
4949
deps = [
5050
"//:auto_value",
5151
"//common/annotations",
52+
"//common/values",
53+
"//common/values:cel_byte_string",
5254
"@maven//:com_google_errorprone_error_prone_annotations",
5355
"@maven//:com_google_guava_guava",
5456
"@maven//:com_google_protobuf_protobuf_java",
@@ -62,9 +64,13 @@ java_library(
6264
],
6365
deps = [
6466
":ast",
67+
"//common/values",
68+
"//common/values:cel_byte_string",
6569
"@cel_spec//proto/cel/expr:checked_java_proto",
6670
"@cel_spec//proto/cel/expr:syntax_java_proto",
6771
"@maven//:com_google_guava_guava",
72+
"@maven//:com_google_protobuf_protobuf_java",
73+
"@maven_android//:com_google_protobuf_protobuf_javalite",
6874
],
6975
)
7076

@@ -75,9 +81,12 @@ cel_android_library(
7581
],
7682
deps = [
7783
":ast_android",
84+
"//common/values:cel_byte_string",
85+
"//common/values:values_android",
7886
"@cel_spec//proto/cel/expr:checked_java_proto_lite",
7987
"@cel_spec//proto/cel/expr:syntax_java_proto_lite",
8088
"@maven_android//:com_google_guava_guava",
89+
"@maven_android//:com_google_protobuf_protobuf_javalite",
8190
],
8291
)
8392

@@ -88,8 +97,12 @@ java_library(
8897
],
8998
deps = [
9099
":ast",
100+
"//common/values",
101+
"//common/values:cel_byte_string",
91102
"@com_google_googleapis//google/api/expr/v1alpha1:expr_java_proto",
92103
"@maven//:com_google_guava_guava",
104+
"@maven//:com_google_protobuf_protobuf_java",
105+
"@maven_android//:com_google_protobuf_protobuf_javalite",
93106
],
94107
)
95108

@@ -112,8 +125,10 @@ java_library(
112125
deps = [
113126
":ast",
114127
"//common/annotations",
128+
"//common/values:cel_byte_string",
115129
"@maven//:com_google_guava_guava",
116130
"@maven//:com_google_protobuf_protobuf_java",
131+
"@maven_android//:com_google_protobuf_protobuf_javalite",
117132
],
118133
)
119134

@@ -136,6 +151,8 @@ cel_android_library(
136151
deps = [
137152
"//:auto_value",
138153
"//common/annotations",
154+
"//common/values:cel_byte_string",
155+
"//common/values:values_android",
139156
"@maven//:com_google_errorprone_error_prone_annotations",
140157
"@maven_android//:com_google_guava_guava",
141158
"@maven_android//:com_google_protobuf_protobuf_javalite",

common/src/main/java/dev/cel/common/ast/CelConstant.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import com.google.common.collect.ImmutableSet;
2020
import com.google.common.primitives.UnsignedLong;
2121
import com.google.errorprone.annotations.Immutable;
22-
import com.google.protobuf.ByteString;
2322
import com.google.protobuf.Duration;
24-
import com.google.protobuf.NullValue;
2523
import com.google.protobuf.Timestamp;
2624
import dev.cel.common.annotations.Internal;
25+
import dev.cel.common.values.CelByteString;
26+
import dev.cel.common.values.NullValue;
2727

2828
/**
2929
* Represents a primitive literal.
@@ -42,7 +42,7 @@ public abstract class CelConstant {
4242
UnsignedLong.class,
4343
Double.class,
4444
String.class,
45-
ByteString.class);
45+
CelByteString.class);
4646

4747
/** Represents the type of the Constant */
4848
public enum Kind {
@@ -92,7 +92,7 @@ public abstract static class CelConstantNotSet {}
9292

9393
public abstract String stringValue();
9494

95-
public abstract ByteString bytesValue();
95+
public abstract CelByteString bytesValue();
9696

9797
/**
9898
* @deprecated Do not use. Timestamp is no longer built-in CEL type.
@@ -134,7 +134,7 @@ public static CelConstant ofValue(String value) {
134134
return AutoOneOf_CelConstant.stringValue(value);
135135
}
136136

137-
public static CelConstant ofValue(ByteString value) {
137+
public static CelConstant ofValue(CelByteString value) {
138138
return AutoOneOf_CelConstant.bytesValue(value);
139139
}
140140

@@ -163,8 +163,8 @@ public static CelConstant ofObjectValue(Object value) {
163163
return ofValue((double) value);
164164
} else if (value instanceof String) {
165165
return ofValue((String) value);
166-
} else if (value instanceof ByteString) {
167-
return ofValue((ByteString) value);
166+
} else if (value instanceof CelByteString) {
167+
return ofValue((CelByteString) value);
168168
}
169169

170170
throw new IllegalArgumentException("Value is not a CelConstant: " + value);

common/src/main/java/dev/cel/common/ast/CelExprConverter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.google.common.collect.ImmutableList;
3131
import com.google.common.collect.ImmutableMap;
3232
import com.google.common.primitives.UnsignedLong;
33+
import com.google.protobuf.ByteString;
34+
import dev.cel.common.values.CelByteString;
35+
import dev.cel.common.values.NullValue;
3336
import java.util.Map;
3437
import java.util.Optional;
3538

@@ -179,7 +182,7 @@ public static CelConstant exprConstantToCelConstant(Constant constExpr) {
179182
case CONSTANTKIND_NOT_SET:
180183
return CelConstant.ofNotSet();
181184
case NULL_VALUE:
182-
return CelConstant.ofValue(constExpr.getNullValue());
185+
return CelConstant.ofValue(NullValue.NULL_VALUE);
183186
case BOOL_VALUE:
184187
return CelConstant.ofValue(constExpr.getBoolValue());
185188
case INT64_VALUE:
@@ -191,7 +194,8 @@ public static CelConstant exprConstantToCelConstant(Constant constExpr) {
191194
case STRING_VALUE:
192195
return CelConstant.ofValue(constExpr.getStringValue());
193196
case BYTES_VALUE:
194-
return CelConstant.ofValue(constExpr.getBytesValue());
197+
ByteString bytesValue = constExpr.getBytesValue();
198+
return CelConstant.ofValue(CelByteString.of(bytesValue.toByteArray()));
195199
case DURATION_VALUE:
196200
return CelConstant.ofValue(constExpr.getDurationValue());
197201
case TIMESTAMP_VALUE:
@@ -250,7 +254,7 @@ public static Constant celConstantToExprConstant(CelConstant celConstant) {
250254
case NOT_SET:
251255
return Constant.getDefaultInstance();
252256
case NULL_VALUE:
253-
return Constant.newBuilder().setNullValue(celConstant.nullValue()).build();
257+
return Constant.newBuilder().setNullValue(com.google.protobuf.NullValue.NULL_VALUE).build();
254258
case BOOLEAN_VALUE:
255259
return Constant.newBuilder().setBoolValue(celConstant.booleanValue()).build();
256260
case INT64_VALUE:
@@ -262,7 +266,10 @@ public static Constant celConstantToExprConstant(CelConstant celConstant) {
262266
case STRING_VALUE:
263267
return Constant.newBuilder().setStringValue(celConstant.stringValue()).build();
264268
case BYTES_VALUE:
265-
return Constant.newBuilder().setBytesValue(celConstant.bytesValue()).build();
269+
CelByteString celByteString = celConstant.bytesValue();
270+
return Constant.newBuilder()
271+
.setBytesValue(ByteString.copyFrom(celByteString.toByteArray()))
272+
.build();
266273
case DURATION_VALUE:
267274
return Constant.newBuilder().setDurationValue(celConstant.durationValue()).build();
268275
case TIMESTAMP_VALUE:

common/src/main/java/dev/cel/common/ast/CelExprFactory.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import static com.google.common.base.Strings.isNullOrEmpty;
1919

2020
import com.google.common.primitives.UnsignedLong;
21-
import com.google.protobuf.ByteString;
2221
import dev.cel.common.annotations.Internal;
22+
import dev.cel.common.values.CelByteString;
2323
import java.util.Arrays;
2424

2525
/** Factory for generating expression nodes. */
@@ -42,23 +42,18 @@ public final CelExpr newBoolLiteral(boolean value) {
4242
}
4343

4444
/** Creates a new constant {@link CelExpr} for a bytes value. */
45-
public final CelExpr newBytesLiteral(ByteString value) {
46-
return newConstant(CelConstant.ofValue(value));
45+
public final CelExpr newBytesLiteral(String value) {
46+
return newBytesLiteral(CelByteString.copyFromUtf8(value));
4747
}
4848

4949
/** Creates a new constant {@link CelExpr} for a bytes value. */
5050
public final CelExpr newBytesLiteral(byte[] value) {
51-
return newBytesLiteral(value, 0, value.length);
51+
return newBytesLiteral(CelByteString.of(value));
5252
}
5353

5454
/** Creates a new constant {@link CelExpr} for a bytes value. */
55-
public final CelExpr newBytesLiteral(byte[] value, int offset, int size) {
56-
return newBytesLiteral(ByteString.copyFrom(value, offset, size));
57-
}
58-
59-
/** Creates a new constant {@link CelExpr} for a bytes value. */
60-
public final CelExpr newBytesLiteral(String value) {
61-
return newBytesLiteral(ByteString.copyFromUtf8(value));
55+
public final CelExpr newBytesLiteral(CelByteString value) {
56+
return newConstant(CelConstant.ofValue(value));
6257
}
6358

6459
/** Creates a new constant {@link CelExpr} for a double value. */

common/src/main/java/dev/cel/common/ast/CelExprFormatter.java

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

1717
import com.google.common.collect.ImmutableSet;
18+
import dev.cel.common.values.CelByteString;
19+
import java.nio.charset.StandardCharsets;
1820
import java.util.Locale;
1921

2022
/** Provides string formatting support for {@link CelExpr}. */
@@ -104,8 +106,12 @@ private void appendConst(CelConstant celConstant) {
104106
appendWithoutIndent("\"" + celConstant.stringValue() + "\"");
105107
break;
106108
case BYTES_VALUE:
109+
CelByteString byteString = celConstant.bytesValue();
107110
appendWithoutIndent(
108-
String.format(Locale.getDefault(), "b\"%s\"", celConstant.bytesValue().toStringUtf8()));
111+
String.format(
112+
Locale.getDefault(),
113+
"b\"%s\"",
114+
new String(byteString.toByteArray(), StandardCharsets.UTF_8)));
109115
break;
110116
default:
111117
append("Unknown kind: " + celConstant.getKind());

common/src/main/java/dev/cel/common/ast/CelExprV1Alpha1Converter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.google.common.collect.ImmutableList;
3131
import com.google.common.collect.ImmutableMap;
3232
import com.google.common.primitives.UnsignedLong;
33+
import com.google.protobuf.ByteString;
34+
import dev.cel.common.values.CelByteString;
35+
import dev.cel.common.values.NullValue;
3336
import java.util.Map;
3437
import java.util.Optional;
3538

@@ -179,7 +182,7 @@ public static CelConstant exprConstantToCelConstant(Constant constExpr) {
179182
case CONSTANTKIND_NOT_SET:
180183
return CelConstant.ofNotSet();
181184
case NULL_VALUE:
182-
return CelConstant.ofValue(constExpr.getNullValue());
185+
return CelConstant.ofValue(NullValue.NULL_VALUE);
183186
case BOOL_VALUE:
184187
return CelConstant.ofValue(constExpr.getBoolValue());
185188
case INT64_VALUE:
@@ -191,7 +194,8 @@ public static CelConstant exprConstantToCelConstant(Constant constExpr) {
191194
case STRING_VALUE:
192195
return CelConstant.ofValue(constExpr.getStringValue());
193196
case BYTES_VALUE:
194-
return CelConstant.ofValue(constExpr.getBytesValue());
197+
ByteString bytesValue = constExpr.getBytesValue();
198+
return CelConstant.ofValue(CelByteString.of(bytesValue.toByteArray()));
195199
case DURATION_VALUE:
196200
return CelConstant.ofValue(constExpr.getDurationValue());
197201
case TIMESTAMP_VALUE:
@@ -250,7 +254,7 @@ public static Constant celConstantToExprConstant(CelConstant celConstant) {
250254
case NOT_SET:
251255
return Constant.getDefaultInstance();
252256
case NULL_VALUE:
253-
return Constant.newBuilder().setNullValue(celConstant.nullValue()).build();
257+
return Constant.newBuilder().setNullValue(com.google.protobuf.NullValue.NULL_VALUE).build();
254258
case BOOLEAN_VALUE:
255259
return Constant.newBuilder().setBoolValue(celConstant.booleanValue()).build();
256260
case INT64_VALUE:
@@ -262,7 +266,10 @@ public static Constant celConstantToExprConstant(CelConstant celConstant) {
262266
case STRING_VALUE:
263267
return Constant.newBuilder().setStringValue(celConstant.stringValue()).build();
264268
case BYTES_VALUE:
265-
return Constant.newBuilder().setBytesValue(celConstant.bytesValue()).build();
269+
CelByteString celByteString = celConstant.bytesValue();
270+
return Constant.newBuilder()
271+
.setBytesValue(ByteString.copyFrom(celByteString.toByteArray()))
272+
.build();
266273
case DURATION_VALUE:
267274
return Constant.newBuilder().setDurationValue(celConstant.durationValue()).build();
268275
case TIMESTAMP_VALUE:

0 commit comments

Comments
 (0)