Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
@Immutable
@Internal
public final class DefaultLiteDescriptorPool implements CelLiteDescriptorPool {
private static final String STRUCT_FIELDS_ENTRY = "google.protobuf.Struct.FieldsEntry";

private final ImmutableMap<String, MessageLiteDescriptor> protoFqnToMessageInfo;
private final ImmutableMap<Class<?>, MessageLiteDescriptor> classToMessageInfo;

Expand Down Expand Up @@ -163,7 +165,7 @@ private static MessageLiteDescriptor newMessageInfo(WellKnownProto wellKnownProt
/* encodingType= */ EncodingType.MAP,
/* protoFieldType= */ FieldLiteDescriptor.Type.MESSAGE,
/* isPacked= */ false,
/* fieldProtoTypeName= */ "google.protobuf.Struct.FieldsEntry"));
/* fieldProtoTypeName= */ STRUCT_FIELDS_ENTRY));
break;
case JSON_VALUE:
messageBuilder = Value::newBuilder;
Expand Down Expand Up @@ -268,6 +270,14 @@ private DefaultLiteDescriptorPool(ImmutableSet<CelLiteDescriptor> descriptors) {
protoFqnMapBuilder.put(wellKnownProto.typeName(), wktMessageInfo);
classMapBuilder.put(wellKnownProto.messageClass(), wktMessageInfo);
}
// Struct's map entry, which `google.protobuf.Struct{fields: ...}` literals are encoded with.
protoFqnMapBuilder.put(
STRUCT_FIELDS_ENTRY,
CelLiteDescriptor.mapEntry(
/* fullName= */ STRUCT_FIELDS_ENTRY,
/* keyType= */ FieldLiteDescriptor.Type.STRING,
/* valueType= */ FieldLiteDescriptor.Type.MESSAGE,
/* valueTypeName= */ WellKnownProto.JSON_VALUE.typeName()));

for (CelLiteDescriptor descriptor : descriptors) {
protoFqnMapBuilder.putAll(descriptor.getProtoTypeNamesToDescriptors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,17 @@ private MessageLite adaptValueToUint64(Object value) {
throw new IllegalArgumentException("Unexpected value type: " + value);
}

static int intCheckedCast(long value) {
/** Narrows {@code value} to an int32, or throws {@link CelNumericOverflowException}. */
public static int intCheckedCast(long value) {
try {
return Ints.checkedCast(value);
} catch (IllegalArgumentException e) {
throw new CelNumericOverflowException(e);
}
}

static int unsignedIntCheckedCast(long value) {
/** Narrows {@code value} to a uint32, or throws {@link CelNumericOverflowException}. */
public static int unsignedIntCheckedCast(long value) {
try {
return UnsignedInts.checkedCast(value);
} catch (IllegalArgumentException e) {
Expand All @@ -316,14 +318,12 @@ static int unsignedIntCheckedCast(long value) {
}

private static Any packAnyMessage(MessageLite msg, WellKnownProto wellKnownProto) {
return packAnyMessage(msg, wellKnownProto.typeName());
return packAny(msg.toByteString(), wellKnownProto.typeName());
}

private static Any packAnyMessage(MessageLite msg, String typeUrl) {
return Any.newBuilder()
.setValue(msg.toByteString())
.setTypeUrl("type.googleapis.com/" + typeUrl)
.build();
/** Packs {@code value}, a serialized message of type {@code typeName}, into an {@link Any}. */
public static Any packAny(ByteString value, String typeName) {
return Any.newBuilder().setValue(value).setTypeUrl("type.googleapis.com/" + typeName).build();
}

private Timestamp adaptValueToProtoTimestamp(Object value) {
Expand Down
12 changes: 12 additions & 0 deletions common/src/main/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,16 @@ java_library(
deps = [
":base_proto_message_value_provider",
":proto_message_lite_value",
"//common:options",
"//common/annotations",
"//common/internal:cel_lite_descriptor_pool",
"//common/internal:default_lite_descriptor_pool",
"//common/internal:proto_lite_adapter",
"//common/internal:proto_time_utils",
"//common/internal:well_known_proto",
"//common/values",
"//common/values:base_proto_cel_value_converter",
"//common/values:cel_byte_string",
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand All @@ -412,10 +418,16 @@ cel_android_library(
deps = [
":base_proto_message_value_provider_android",
":proto_message_lite_value_android",
"//common:options",
"//common/annotations",
"//common/internal:cel_lite_descriptor_pool_android",
"//common/internal:default_lite_descriptor_pool_android",
"//common/internal:proto_lite_adapter_android",
"//common/internal:proto_time_utils_android",
"//common/internal:well_known_proto_android",
"//common/values:base_proto_cel_value_converter_android",
"//common/values:cel_byte_string",
"//common/values:values_android",
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
public final class ProtoLiteCelValueConverter extends BaseProtoCelValueConverter {
private static final String MAP_KEY_FIELD_NAME = "key";
private static final String MAP_VALUE_FIELD_NAME = "value";
private static final int MAP_KEY_FIELD_NUMBER = 1;
private static final int MAP_VALUE_FIELD_NUMBER = 2;
static final int MAP_KEY_FIELD_NUMBER = 1;
static final int MAP_VALUE_FIELD_NUMBER = 2;
// Protobuf's default recursion limit. Nested Anys are unwrapped in a loop since each payload is
// parsed afresh, outside that limit.
private static final int MAX_ANY_NESTING_DEPTH = 100;
Expand Down Expand Up @@ -168,7 +168,7 @@ private Object mergeOrReadMessageField(

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

Expand All @@ -190,7 +190,7 @@ static MessageLite parseMessageLite(ByteString bytes, MessageLiteDescriptor desc
return mergeMessageLite(descriptor.newMessageBuilder(), bytes, descriptor.getProtoTypeName());
}

private static MessageLite mergeMessageLite(
static MessageLite mergeMessageLite(
MessageLite.Builder builder, ByteString bytes, String protoTypeName) {
try {
return builder.mergeFrom(bytes, ExtensionRegistryLite.getEmptyRegistry()).build();
Expand Down
Loading
Loading