Skip to content
Open
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
3 changes: 2 additions & 1 deletion common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ public Builder enableHeterogeneousNumericComparisons(boolean value) {
public abstract Builder enableUnsignedLongs(boolean value);

/**
* Perform equality using {@code ProtoEquality} proto equality.
* Perform equality using {@code ProtoEquality} proto equality. {@code CelLiteRuntime} always
* applies this equality regardless of this option.
*
* <p>CEL's alternative implementation to the {@code Message#equals} comes with a few key
* differences:
Expand Down
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
49 changes: 36 additions & 13 deletions common/src/main/java/dev/cel/common/internal/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,21 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
case SFIXED32:
case SINT32:
case INT32:
return unwrapAndConvert(INT_CONVERTER);
return unwrapAndConvert(fieldDescriptor, Long.class, INT_CONVERTER);
case FIXED32:
case UINT32:
if (celOptions.enableUnsignedLongs()) {
return unwrapAndConvert(UNSIGNED_UINT32_CONVERTER);
return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT32_CONVERTER);
}
return unwrapAndConvert(SIGNED_UINT32_CONVERTER);
return unwrapAndConvert(fieldDescriptor, Long.class, SIGNED_UINT32_CONVERTER);
case FIXED64:
case UINT64:
if (celOptions.enableUnsignedLongs()) {
return unwrapAndConvert(UNSIGNED_UINT64_CONVERTER);
return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT64_CONVERTER);
}
return BidiConverter.of(
BidiConverter.IDENTITY.forwardConverter(),
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
return unwrapAndCheck(fieldDescriptor, Long.class);
case FLOAT:
return unwrapAndConvert(DOUBLE_CONVERTER);
return unwrapAndConvert(fieldDescriptor, Double.class, DOUBLE_CONVERTER);
case BYTES:
if (celOptions.evaluateCanonicalTypesToNativeValues()) {
return BidiConverter.<Object, Object>of(
Expand All @@ -336,21 +334,24 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
BidiConverter.IDENTITY.forwardConverter(),
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
case DOUBLE:
return unwrapAndCheck(fieldDescriptor, Double.class);
case SFIXED64:
case SINT64:
case INT64:
return unwrapAndCheck(fieldDescriptor, Long.class);
case STRING:
case BOOL:
return BidiConverter.of(
BidiConverter.IDENTITY.forwardConverter(),
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
case ENUM:
return BidiConverter.<Object, Long>of(
return BidiConverter.<Object, Object>of(
value -> (long) ((EnumValueDescriptor) value).getNumber(),
number ->
value ->
fieldDescriptor
.getEnumType()
.findValueByNumberCreatingIfUnknown(intCheckedCast(number)));
.findValueByNumberCreatingIfUnknown(
intCheckedCast((Long) unwrapNumber(fieldDescriptor, Long.class, value))));
case MESSAGE:
return BidiConverter.<MessageOrBuilder, Object>of(
this::adaptProtoToValue,
Expand Down Expand Up @@ -434,9 +435,31 @@ private Object maybeUnwrap(Object value) {
}

private BidiConverter<Number, Object> unwrapAndConvert(
final BidiConverter<Number, Number> original) {
FieldDescriptor fieldDescriptor, Class<?> celType, BidiConverter<Number, Number> original) {
return BidiConverter.of(
original.forwardConverter()::convert,
value -> original.backwardConverter().convert((Number) maybeUnwrap(value)));
value ->
original
.backwardConverter()
.convert((Number) unwrapNumber(fieldDescriptor, celType, value)));
}

private BidiConverter<Object, Object> unwrapAndCheck(
FieldDescriptor fieldDescriptor, Class<?> celType) {
return BidiConverter.of(
BidiConverter.IDENTITY.forwardConverter(),
value -> unwrapNumber(fieldDescriptor, celType, value));
}

private Object unwrapNumber(FieldDescriptor fieldDescriptor, Class<?> celType, Object value) {
Object celValue = maybeUnwrap(value);
if ((celValue instanceof Long || celValue instanceof UnsignedLong || celValue instanceof Double)
&& !celType.isInstance(celValue)) {
throw new IllegalArgumentException(
String.format(
"unexpected value type for field '%s' in message '%s'",
fieldDescriptor.getName(), fieldDescriptor.getContainingType().getFullName()));
}
return celValue;
}
}
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
14 changes: 14 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 @@ -322,6 +322,7 @@ java_library(
name = "proto_message_lite_value",
srcs = [
"ProtoLiteCelValueConverter.java",
"ProtoLiteEquality.java",
"ProtoMessageLiteValue.java",
"RawProtoMessageLiteValue.java",
"WireMessageLite.java",
Expand Down Expand Up @@ -356,6 +357,7 @@ cel_android_library(
name = "proto_message_lite_value_android",
srcs = [
"ProtoLiteCelValueConverter.java",
"ProtoLiteEquality.java",
"ProtoMessageLiteValue.java",
"RawProtoMessageLiteValue.java",
"WireMessageLite.java",
Expand Down Expand Up @@ -393,10 +395,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 +420,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 @@ -221,6 +221,14 @@ public static boolean containsMapKey(Map<?, ?> map, Object key) {
return false;
}

/**
* Returns whether {@code x} and {@code y} are equal in CEL, if they're protobuf messages this
* converter knows how to compare.
*/
public Optional<Boolean> messageEquals(Object x, Object y) {
return Optional.empty();
}

protected Object normalizePrimitive(Object value) {
Preconditions.checkNotNull(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableList;
import dev.cel.common.annotations.Internal;
import java.util.Optional;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -81,4 +82,15 @@ private CombinedCelValueConverter(ImmutableList<CelValueConverter> converters) {

return value;
}

@Override
public Optional<Boolean> messageEquals(Object x, Object y) {
for (CelValueConverter converter : converters) {
Optional<Boolean> result = converter.messageEquals(x, y);
if (result.isPresent()) {
return result;
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@
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;
static final int MAX_ANY_NESTING_DEPTH = 100;

private final CelLiteDescriptorPool descriptorPool;
private final ProtoLiteEquality protoLiteEquality;

public static ProtoLiteCelValueConverter newInstance(
CelLiteDescriptorPool celLiteDescriptorPool) {
Expand Down Expand Up @@ -168,7 +169,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 +191,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 All @@ -215,6 +216,13 @@ public Object toRuntimeValue(Object value) {
return super.toRuntimeValue(value);
}

@Override
public Optional<Boolean> messageEquals(Object x, Object y) {
return x instanceof MessageLite && y instanceof MessageLite
? Optional.of(protoLiteEquality.equals((MessageLite) x, (MessageLite) y))
: Optional.empty();
}

private Object toRuntimeValue(MessageLite msg, MessageLiteDescriptor descriptor) {
WellKnownProto wellKnownProto =
WellKnownProto.getByTypeName(descriptor.getProtoTypeName()).orElse(null);
Expand Down Expand Up @@ -805,5 +813,6 @@ private static IllegalArgumentException newDecodeException(String protoTypeName,

private ProtoLiteCelValueConverter(CelLiteDescriptorPool celLiteDescriptorPool) {
this.descriptorPool = checkNotNull(celLiteDescriptorPool);
this.protoLiteEquality = new ProtoLiteEquality(celLiteDescriptorPool);
}
}
Loading
Loading