Skip to content

Commit 285bf57

Browse files
l46kokcopybara-github
authored andcommitted
Prepares CelLiteRuntime's protobuf adapters for message creation.
PiperOrigin-RevId: 1020240785
1 parent 6e7dbe7 commit 285bf57

10 files changed

Lines changed: 99 additions & 55 deletions

File tree

‎common/BUILD.bazel‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ java_library(
166166
exports = ["//common/src/main/java/dev/cel/common:proto_json_adapter"],
167167
)
168168

169+
cel_android_library(
170+
name = "proto_json_adapter_android",
171+
exports = ["//common/src/main/java/dev/cel/common:proto_json_adapter_android"],
172+
)
173+
169174
java_library(
170175
name = "source",
171176
visibility = ["//:internal"],

‎common/internal/BUILD.bazel‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ java_library(
4242
exports = ["//common/src/main/java/dev/cel/common/internal:proto_lite_adapter"],
4343
)
4444

45+
cel_android_library(
46+
name = "proto_lite_adapter_android",
47+
exports = ["//common/src/main/java/dev/cel/common/internal:proto_lite_adapter_android"],
48+
)
49+
4550
java_library(
4651
name = "proto_equality",
4752
exports = ["//common/src/main/java/dev/cel/common/internal:proto_equality"],

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,22 @@ java_library(
439439
],
440440
)
441441

442+
cel_android_library(
443+
name = "proto_json_adapter_android",
444+
srcs = ["CelProtoJsonAdapter.java"],
445+
tags = [
446+
],
447+
deps = [
448+
"//common/internal:date_time_helpers_android",
449+
"//common/internal:proto_time_utils_android",
450+
"//common/values:cel_byte_string",
451+
"//common/values:values_android",
452+
"@maven//:com_google_errorprone_error_prone_annotations",
453+
"@maven_android//:com_google_guava_guava",
454+
"@maven_android//:com_google_protobuf_protobuf_javalite",
455+
],
456+
)
457+
442458
java_library(
443459
name = "source_location",
444460
srcs = ["CelSourceLocation.java"],

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ java_library(
184184
":well_known_proto",
185185
"//common:options",
186186
"//common/annotations",
187-
"//common/exceptions:numeric_overflow",
188187
"//common/values",
189188
"//common/values:cel_byte_string",
190189
"@maven//:com_google_code_findbugs_annotations",
@@ -214,6 +213,26 @@ java_library(
214213
],
215214
)
216215

216+
cel_android_library(
217+
name = "proto_lite_adapter_android",
218+
srcs = ["ProtoLiteAdapter.java"],
219+
tags = [
220+
],
221+
deps = [
222+
":well_known_proto_android",
223+
"//common:options",
224+
"//common:proto_json_adapter_android",
225+
"//common/annotations",
226+
"//common/exceptions:numeric_overflow",
227+
"//common/internal:proto_time_utils_android",
228+
"//common/values:cel_byte_string",
229+
"//common/values:values_android",
230+
"@maven//:com_google_errorprone_error_prone_annotations",
231+
"@maven_android//:com_google_guava_guava",
232+
"@maven_android//:com_google_protobuf_protobuf_javalite",
233+
],
234+
)
235+
217236
java_library(
218237
name = "proto_equality",
219238
srcs = ["ProtoEquality.java"],

‎common/src/main/java/dev/cel/common/internal/ProtoAdapter.java‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package dev.cel.common.internal;
1616

1717
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static dev.cel.common.internal.ProtoLiteAdapter.intCheckedCast;
19+
import static dev.cel.common.internal.ProtoLiteAdapter.unsignedIntCheckedCast;
1820

19-
import com.google.common.primitives.Ints;
2021
import com.google.common.primitives.UnsignedInts;
2122
import com.google.common.primitives.UnsignedLong;
2223
import com.google.errorprone.annotations.CheckReturnValue;
@@ -33,7 +34,6 @@
3334
import com.google.protobuf.MessageOrBuilder;
3435
import dev.cel.common.CelOptions;
3536
import dev.cel.common.annotations.Internal;
36-
import dev.cel.common.exceptions.CelNumericOverflowException;
3737
import dev.cel.common.values.CelByteString;
3838
import dev.cel.common.values.NullValue;
3939
import java.util.ArrayList;
@@ -347,14 +347,10 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
347347
case ENUM:
348348
return BidiConverter.<Object, Long>of(
349349
value -> (long) ((EnumValueDescriptor) value).getNumber(),
350-
number -> {
351-
if (number > Integer.MAX_VALUE || number < Integer.MIN_VALUE) {
352-
throw new IllegalArgumentException("Enum value out of int32 range: " + number);
353-
}
354-
return fieldDescriptor
355-
.getEnumType()
356-
.findValueByNumberCreatingIfUnknown(number.intValue());
357-
});
350+
number ->
351+
fieldDescriptor
352+
.getEnumType()
353+
.findValueByNumberCreatingIfUnknown(intCheckedCast(number)));
358354
case MESSAGE:
359355
return BidiConverter.<MessageOrBuilder, Object>of(
360356
this::adaptProtoToValue,
@@ -401,9 +397,9 @@ public Message adaptValueToProto(Object value, String protoTypeName) {
401397
switch (wellKnownProto) {
402398
case ANY_VALUE:
403399
if (value instanceof Message) {
404-
protoTypeName = ((Message) value).getDescriptorForType().getFullName();
400+
return Any.pack((Message) value);
405401
}
406-
return protoLiteAdapter.adaptValueToAny(value, protoTypeName);
402+
return protoLiteAdapter.adaptValueToAny(value);
407403
default:
408404
return (Message) protoLiteAdapter.adaptValueToWellKnownProto(value, wellKnownProto);
409405
}
@@ -430,22 +426,6 @@ private static String typeName(Descriptor protoType) {
430426
return protoType.getFullName();
431427
}
432428

433-
private static int intCheckedCast(long value) {
434-
try {
435-
return Ints.checkedCast(value);
436-
} catch (IllegalArgumentException e) {
437-
throw new CelNumericOverflowException(e);
438-
}
439-
}
440-
441-
private static int unsignedIntCheckedCast(long value) {
442-
try {
443-
return UnsignedInts.checkedCast(value);
444-
} catch (IllegalArgumentException e) {
445-
throw new CelNumericOverflowException(e);
446-
}
447-
}
448-
449429
private Object maybeUnwrap(Object value) {
450430
if (value instanceof Message) {
451431
return adaptProtoToValue((MessageOrBuilder) value);

‎common/src/main/java/dev/cel/common/internal/ProtoLiteAdapter.java‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.protobuf.Int32Value;
3333
import com.google.protobuf.Int64Value;
3434
import com.google.protobuf.ListValue;
35-
import com.google.protobuf.Message;
3635
import com.google.protobuf.MessageLite;
3736
import com.google.protobuf.MessageLiteOrBuilder;
3837
import com.google.protobuf.NullValue;
@@ -110,11 +109,7 @@ public MessageLite adaptValueToWellKnownProto(Object value, WellKnownProto wellK
110109
}
111110
}
112111

113-
public Any adaptValueToAny(Object value, String typeName) {
114-
if (value instanceof MessageLite) {
115-
return packAnyMessage((MessageLite) value, typeName);
116-
}
117-
112+
public Any adaptValueToAny(Object value) {
118113
if (value instanceof dev.cel.common.values.NullValue) {
119114
return packAnyMessage(
120115
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(), WellKnownProto.JSON_VALUE);
@@ -234,7 +229,7 @@ private ImmutableMap<String, Object> adaptJsonStructToValue(Struct struct) {
234229
.collect(toImmutableMap(Entry::getKey, e -> adaptJsonToValue(e.getValue())));
235230
}
236231

237-
private Message adaptValueToDouble(Object value) {
232+
private MessageLite adaptValueToDouble(Object value) {
238233
if (value instanceof Double) {
239234
return DoubleValue.of((Double) value);
240235
}
@@ -244,7 +239,7 @@ private Message adaptValueToDouble(Object value) {
244239
throw new IllegalArgumentException("Unexpected value type: " + value);
245240
}
246241

247-
private Message adaptValueToFloat(Object value) {
242+
private MessageLite adaptValueToFloat(Object value) {
248243
if (value instanceof Double) {
249244
return FloatValue.of(((Double) value).floatValue());
250245
}
@@ -254,7 +249,7 @@ private Message adaptValueToFloat(Object value) {
254249
throw new IllegalArgumentException("Unexpected value type: " + value);
255250
}
256251

257-
private Message adaptValueToInt32(Object value) {
252+
private MessageLite adaptValueToInt32(Object value) {
258253
if (value instanceof Integer) {
259254
return Int32Value.of((Integer) value);
260255
}
@@ -264,7 +259,7 @@ private Message adaptValueToInt32(Object value) {
264259
throw new IllegalArgumentException("Unexpected value type: " + value);
265260
}
266261

267-
private Message adaptValueToInt64(Object value) {
262+
private MessageLite adaptValueToInt64(Object value) {
268263
if (value instanceof Integer) {
269264
return Int64Value.of(((Integer) value).longValue());
270265
}
@@ -275,30 +270,22 @@ private Message adaptValueToInt64(Object value) {
275270
throw new IllegalArgumentException("Unexpected value type: " + value);
276271
}
277272

278-
private Message adaptValueToUint32(Object value) {
273+
private MessageLite adaptValueToUint32(Object value) {
279274
if (value instanceof Integer) {
280275
return UInt32Value.of((Integer) value);
281276
}
282277
// Per the spec, an int isn't a uint, unless unsigned longs are disabled.
283278
if (!celOptions.enableUnsignedLongs() && value instanceof Long) {
284-
try {
285-
return UInt32Value.of(unsignedIntCheckedCast((Long) value));
286-
} catch (IllegalArgumentException e) {
287-
throw new CelNumericOverflowException(e);
288-
}
279+
return UInt32Value.of(unsignedIntCheckedCast((Long) value));
289280
}
290281
if (value instanceof UnsignedLong) {
291-
try {
292-
return UInt32Value.of(unsignedIntCheckedCast(((UnsignedLong) value).longValue()));
293-
} catch (IllegalArgumentException e) {
294-
throw new CelNumericOverflowException(e);
295-
}
282+
return UInt32Value.of(unsignedIntCheckedCast(((UnsignedLong) value).longValue()));
296283
}
297284

298285
throw new IllegalArgumentException("Unexpected value type: " + value);
299286
}
300287

301-
private Message adaptValueToUint64(Object value) {
288+
private MessageLite adaptValueToUint64(Object value) {
302289
if (value instanceof Integer) {
303290
return UInt64Value.of(UnsignedInts.toLong((Integer) value));
304291
}
@@ -312,15 +299,15 @@ private Message adaptValueToUint64(Object value) {
312299
throw new IllegalArgumentException("Unexpected value type: " + value);
313300
}
314301

315-
private static int intCheckedCast(long value) {
302+
static int intCheckedCast(long value) {
316303
try {
317304
return Ints.checkedCast(value);
318305
} catch (IllegalArgumentException e) {
319306
throw new CelNumericOverflowException(e);
320307
}
321308
}
322309

323-
private static int unsignedIntCheckedCast(long value) {
310+
static int unsignedIntCheckedCast(long value) {
324311
try {
325312
return UnsignedInts.checkedCast(value);
326313
} catch (IllegalArgumentException e) {

‎common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ private Object mergeOrReadMessageField(
145145
ByteString bytes, String fieldProtoTypeName, @Nullable Object existingValue, boolean isView) {
146146
MessageLiteDescriptor descriptor =
147147
descriptorPool.findDescriptor(fieldProtoTypeName).orElse(null);
148-
if (descriptor == null) {
148+
// Kept as raw bytes: unknown types have no descriptor, and map entries, which an Any's type URL
149+
// can name, have no builder.
150+
if (descriptor == null || descriptor.isMapEntry()) {
149151
if (existingValue instanceof RawProtoMessageLiteValue) {
150152
bytes = ((RawProtoMessageLiteValue) existingValue).toByteString().concat(bytes);
151153
}

‎common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ public void toRuntimeValue_anyWithUnregisteredMessage_returnsRawValueOfPackedTyp
210210
assertThat(rawValue.toByteString()).isEqualTo(msg.toByteString());
211211
}
212212

213+
@Test
214+
public void toRuntimeValue_anyWithMapEntry_returnsRawValueOfPackedType() {
215+
String mapEntryTypeName = PROTO3_TEST_ALL_TYPES + ".MapStringStringEntry";
216+
Any any = Any.newBuilder().setTypeUrl("type.googleapis.com/" + mapEntryTypeName).build();
217+
218+
Object adaptedValue = PROTO_LITE_CEL_VALUE_CONVERTER.toRuntimeValue(any);
219+
220+
assertThat(adaptedValue).isInstanceOf(RawProtoMessageLiteValue.class);
221+
assertThat(((RawProtoMessageLiteValue) adaptedValue).protoTypeName())
222+
.isEqualTo(mapEntryTypeName);
223+
}
224+
213225
@Test
214226
public void toRuntimeValue_anyNestedToMaxDepth_unpacks() {
215227
Object adaptedValue = PROTO_LITE_CEL_VALUE_CONVERTER.toRuntimeValue(nestedAny(100));

‎protobuf/src/main/java/dev/cel/protobuf/CelLiteDescriptor.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public abstract class CelLiteDescriptor {
4545
Collections.unmodifiableSet(
4646
new HashSet<>(Arrays.asList(false, 0, 0L, 0.0f, 0.0d, "", ByteString.EMPTY)));
4747

48+
// Map entries have no generated class to build.
49+
private static final Supplier<MessageLite.Builder> NO_BUILDER = () -> null;
50+
4851
@SuppressWarnings("Immutable") // Copied to unmodifiable map
4952
private final Map<String, MessageLiteDescriptor> protoFqnToDescriptors;
5053

@@ -121,6 +124,11 @@ public MessageLite.Builder newMessageBuilder() {
121124
return messageBuilderSupplier.get();
122125
}
123126

127+
/** Whether this describes a map field's entry, which has no message builder. */
128+
public boolean isMapEntry() {
129+
return messageBuilderSupplier.equals(NO_BUILDER);
130+
}
131+
124132
/** Whether {@link #getFieldValue} reads this message's fields with their generated getters. */
125133
public boolean hasGetters() {
126134
return owner != null;
@@ -415,7 +423,7 @@ protected static MessageLiteDescriptor mapEntry(
415423
valueType,
416424
false,
417425
valueTypeName)),
418-
() -> null);
426+
NO_BUILDER);
419427
}
420428

421429
protected CelLiteDescriptor(String version, List<MessageLiteDescriptor> messageInfoList) {

‎runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,16 @@ public void plan_createStruct_oneofSetTwice_lastFieldWins(
709709
assertThat(result.getKindCase()).isEqualTo(expectedCase);
710710
}
711711

712+
@Test
713+
public void plan_createStruct_enumOutOfRange_throwsNumericOverflow() throws Exception {
714+
CelAbstractSyntaxTree ast = compile("TestAllTypes{standalone_enum: 5000000000}");
715+
Program program = PLANNER.plan(ast);
716+
717+
CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
718+
719+
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.NUMERIC_OVERFLOW);
720+
}
721+
712722
@Test
713723
public void plan_call_zeroArgs() throws Exception {
714724
CelAbstractSyntaxTree ast = compile("zero()");

0 commit comments

Comments
 (0)