Skip to content

Commit cbc9630

Browse files
l46kokcopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 1018704683
1 parent 8f52fe7 commit cbc9630

25 files changed

Lines changed: 799 additions & 195 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/DefaultLiteDescriptorPool.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
@Immutable
5050
@Internal
5151
public final class DefaultLiteDescriptorPool implements CelLiteDescriptorPool {
52+
private static final String STRUCT_FIELDS_ENTRY = "google.protobuf.Struct.FieldsEntry";
53+
5254
private final ImmutableMap<String, MessageLiteDescriptor> protoFqnToMessageInfo;
5355
private final ImmutableMap<Class<?>, MessageLiteDescriptor> classToMessageInfo;
5456

@@ -163,7 +165,7 @@ private static MessageLiteDescriptor newMessageInfo(WellKnownProto wellKnownProt
163165
/* encodingType= */ EncodingType.MAP,
164166
/* protoFieldType= */ FieldLiteDescriptor.Type.MESSAGE,
165167
/* isPacked= */ false,
166-
/* fieldProtoTypeName= */ "google.protobuf.Struct.FieldsEntry"));
168+
/* fieldProtoTypeName= */ STRUCT_FIELDS_ENTRY));
167169
break;
168170
case JSON_VALUE:
169171
messageBuilder = Value::newBuilder;
@@ -268,6 +270,14 @@ private DefaultLiteDescriptorPool(ImmutableSet<CelLiteDescriptor> descriptors) {
268270
protoFqnMapBuilder.put(wellKnownProto.typeName(), wktMessageInfo);
269271
classMapBuilder.put(wellKnownProto.messageClass(), wktMessageInfo);
270272
}
273+
// Struct's map entry, which `google.protobuf.Struct{fields: ...}` literals are encoded with.
274+
protoFqnMapBuilder.put(
275+
STRUCT_FIELDS_ENTRY,
276+
CelLiteDescriptor.mapEntry(
277+
/* fullName= */ STRUCT_FIELDS_ENTRY,
278+
/* keyType= */ FieldLiteDescriptor.Type.STRING,
279+
/* valueType= */ FieldLiteDescriptor.Type.MESSAGE,
280+
/* valueTypeName= */ WellKnownProto.JSON_VALUE.typeName()));
271281

272282
for (CelLiteDescriptor descriptor : descriptors) {
273283
protoFqnMapBuilder.putAll(descriptor.getProtoTypeNamesToDescriptors());

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

Lines changed: 2 additions & 18 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;
@@ -430,22 +430,6 @@ private static String typeName(Descriptor protoType) {
430430
return protoType.getFullName();
431431
}
432432

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-
449433
private Object maybeUnwrap(Object value) {
450434
if (value instanceof Message) {
451435
return adaptProtoToValue((MessageOrBuilder) value);

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

Lines changed: 17 additions & 26 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;
@@ -112,7 +111,7 @@ public MessageLite adaptValueToWellKnownProto(Object value, WellKnownProto wellK
112111

113112
public Any adaptValueToAny(Object value, String typeName) {
114113
if (value instanceof MessageLite) {
115-
return packAnyMessage((MessageLite) value, typeName);
114+
return packAny(((MessageLite) value).toByteString(), typeName);
116115
}
117116

118117
if (value instanceof dev.cel.common.values.NullValue) {
@@ -234,7 +233,7 @@ private ImmutableMap<String, Object> adaptJsonStructToValue(Struct struct) {
234233
.collect(toImmutableMap(Entry::getKey, e -> adaptJsonToValue(e.getValue())));
235234
}
236235

237-
private Message adaptValueToDouble(Object value) {
236+
private MessageLite adaptValueToDouble(Object value) {
238237
if (value instanceof Double) {
239238
return DoubleValue.of((Double) value);
240239
}
@@ -244,7 +243,7 @@ private Message adaptValueToDouble(Object value) {
244243
throw new IllegalArgumentException("Unexpected value type: " + value);
245244
}
246245

247-
private Message adaptValueToFloat(Object value) {
246+
private MessageLite adaptValueToFloat(Object value) {
248247
if (value instanceof Double) {
249248
return FloatValue.of(((Double) value).floatValue());
250249
}
@@ -254,7 +253,7 @@ private Message adaptValueToFloat(Object value) {
254253
throw new IllegalArgumentException("Unexpected value type: " + value);
255254
}
256255

257-
private Message adaptValueToInt32(Object value) {
256+
private MessageLite adaptValueToInt32(Object value) {
258257
if (value instanceof Integer) {
259258
return Int32Value.of((Integer) value);
260259
}
@@ -264,7 +263,7 @@ private Message adaptValueToInt32(Object value) {
264263
throw new IllegalArgumentException("Unexpected value type: " + value);
265264
}
266265

267-
private Message adaptValueToInt64(Object value) {
266+
private MessageLite adaptValueToInt64(Object value) {
268267
if (value instanceof Integer) {
269268
return Int64Value.of(((Integer) value).longValue());
270269
}
@@ -275,29 +274,21 @@ private Message adaptValueToInt64(Object value) {
275274
throw new IllegalArgumentException("Unexpected value type: " + value);
276275
}
277276

278-
private Message adaptValueToUint32(Object value) {
277+
private MessageLite adaptValueToUint32(Object value) {
279278
if (value instanceof Integer) {
280279
return UInt32Value.of((Integer) value);
281280
}
282281
if (value instanceof Long) {
283-
try {
284-
return UInt32Value.of(unsignedIntCheckedCast((Long) value));
285-
} catch (IllegalArgumentException e) {
286-
throw new CelNumericOverflowException(e);
287-
}
282+
return UInt32Value.of(unsignedIntCheckedCast((Long) value));
288283
}
289284
if (value instanceof UnsignedLong) {
290-
try {
291-
return UInt32Value.of(unsignedIntCheckedCast(((UnsignedLong) value).longValue()));
292-
} catch (IllegalArgumentException e) {
293-
throw new CelNumericOverflowException(e);
294-
}
285+
return UInt32Value.of(unsignedIntCheckedCast(((UnsignedLong) value).longValue()));
295286
}
296287

297288
throw new IllegalArgumentException("Unexpected value type: " + value);
298289
}
299290

300-
private Message adaptValueToUint64(Object value) {
291+
private MessageLite adaptValueToUint64(Object value) {
301292
if (value instanceof Integer) {
302293
return UInt64Value.of(UnsignedInts.toLong((Integer) value));
303294
}
@@ -311,15 +302,17 @@ private Message adaptValueToUint64(Object value) {
311302
throw new IllegalArgumentException("Unexpected value type: " + value);
312303
}
313304

314-
private static int intCheckedCast(long value) {
305+
/** Narrows {@code value} to an int32, or throws {@link CelNumericOverflowException}. */
306+
public static int intCheckedCast(long value) {
315307
try {
316308
return Ints.checkedCast(value);
317309
} catch (IllegalArgumentException e) {
318310
throw new CelNumericOverflowException(e);
319311
}
320312
}
321313

322-
private static int unsignedIntCheckedCast(long value) {
314+
/** Narrows {@code value} to a uint32, or throws {@link CelNumericOverflowException}. */
315+
public static int unsignedIntCheckedCast(long value) {
323316
try {
324317
return UnsignedInts.checkedCast(value);
325318
} catch (IllegalArgumentException e) {
@@ -328,14 +321,12 @@ private static int unsignedIntCheckedCast(long value) {
328321
}
329322

330323
private static Any packAnyMessage(MessageLite msg, WellKnownProto wellKnownProto) {
331-
return packAnyMessage(msg, wellKnownProto.typeName());
324+
return packAny(msg.toByteString(), wellKnownProto.typeName());
332325
}
333326

334-
private static Any packAnyMessage(MessageLite msg, String typeUrl) {
335-
return Any.newBuilder()
336-
.setValue(msg.toByteString())
337-
.setTypeUrl("type.googleapis.com/" + typeUrl)
338-
.build();
327+
/** Packs {@code value}, a serialized message of type {@code typeName}, into an {@link Any}. */
328+
public static Any packAny(ByteString value, String typeName) {
329+
return Any.newBuilder().setValue(value).setTypeUrl("type.googleapis.com/" + typeName).build();
339330
}
340331

341332
private Timestamp adaptValueToProtoTimestamp(Object value) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,16 @@ java_library(
393393
deps = [
394394
":base_proto_message_value_provider",
395395
":proto_message_lite_value",
396+
"//common:options",
396397
"//common/annotations",
397398
"//common/internal:cel_lite_descriptor_pool",
398399
"//common/internal:default_lite_descriptor_pool",
400+
"//common/internal:proto_lite_adapter",
401+
"//common/internal:proto_time_utils",
402+
"//common/internal:well_known_proto",
403+
"//common/values",
399404
"//common/values:base_proto_cel_value_converter",
405+
"//common/values:cel_byte_string",
400406
"//protobuf:cel_lite_descriptor",
401407
"@maven//:com_google_errorprone_error_prone_annotations",
402408
"@maven//:com_google_guava_guava",
@@ -412,10 +418,16 @@ cel_android_library(
412418
deps = [
413419
":base_proto_message_value_provider_android",
414420
":proto_message_lite_value_android",
421+
"//common:options",
415422
"//common/annotations",
416423
"//common/internal:cel_lite_descriptor_pool_android",
417424
"//common/internal:default_lite_descriptor_pool_android",
425+
"//common/internal:proto_lite_adapter_android",
426+
"//common/internal:proto_time_utils_android",
427+
"//common/internal:well_known_proto_android",
418428
"//common/values:base_proto_cel_value_converter_android",
429+
"//common/values:cel_byte_string",
430+
"//common/values:values_android",
419431
"//protobuf:cel_lite_descriptor",
420432
"@maven//:com_google_errorprone_error_prone_annotations",
421433
"@maven_android//:com_google_guava_guava",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected Object fromWellKnownProto(MessageLiteOrBuilder message, WellKnownProto
9595
case STRING_VALUE:
9696
return normalizePrimitive(((StringValue) message).getValue());
9797
case UINT32_VALUE:
98-
return UnsignedLong.valueOf(((UInt32Value) message).getValue());
98+
return UnsignedLong.valueOf(Integer.toUnsignedLong(((UInt32Value) message).getValue()));
9999
case UINT64_VALUE:
100100
return UnsignedLong.fromLongBits(((UInt64Value) message).getValue());
101101
case EMPTY:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
public final class ProtoLiteCelValueConverter extends BaseProtoCelValueConverter {
6363
private static final String MAP_KEY_FIELD_NAME = "key";
6464
private static final String MAP_VALUE_FIELD_NAME = "value";
65-
private static final int MAP_KEY_FIELD_NUMBER = 1;
66-
private static final int MAP_VALUE_FIELD_NUMBER = 2;
65+
static final int MAP_KEY_FIELD_NUMBER = 1;
66+
static final int MAP_VALUE_FIELD_NUMBER = 2;
6767
// Protobuf's default recursion limit. Nested Anys are unwrapped in a loop since each payload is
6868
// parsed afresh, outside that limit.
6969
private static final int MAX_ANY_NESTING_DEPTH = 100;
@@ -166,7 +166,7 @@ private Object mergeOrReadMessageField(
166166

167167
// Unlike other WellKnownProtos (which unbox to CEL scalars/containers), FieldMask is
168168
// represented as a standard struct message so field selection (e.g., mask.paths) works.
169-
private static boolean isStructLike(@Nullable WellKnownProto wellKnownProto) {
169+
static boolean isStructLike(@Nullable WellKnownProto wellKnownProto) {
170170
return wellKnownProto == null || wellKnownProto == WellKnownProto.FIELD_MASK;
171171
}
172172

@@ -188,7 +188,7 @@ static MessageLite parseMessageLite(ByteString bytes, MessageLiteDescriptor desc
188188
return mergeMessageLite(descriptor.newMessageBuilder(), bytes, descriptor.getProtoTypeName());
189189
}
190190

191-
private static MessageLite mergeMessageLite(
191+
static MessageLite mergeMessageLite(
192192
MessageLite.Builder builder, ByteString bytes, String protoTypeName) {
193193
try {
194194
return builder.mergeFrom(bytes, ExtensionRegistryLite.getEmptyRegistry()).build();

0 commit comments

Comments
 (0)