Skip to content

Commit dd91fd6

Browse files
l46kokcopybara-github
authored andcommitted
Supports equality on protobuf messages in CelLiteRuntime.
`TestAllTypes{single_int32: 1} == TestAllTypes{single_int32: 1}` previously failed in `CelLiteRuntime`, and now evaluates to `true` following CEL message equality semantics (`NaN` is unequal to itself, `-0.0` equals `0.0` when both are set, `google.protobuf.Any` payloads unpack, and unknown fields are order-insensitive). PiperOrigin-RevId: 1025064966
1 parent 9bac8b2 commit dd91fd6

36 files changed

Lines changed: 2284 additions & 323 deletions

‎common/src/main/java/dev/cel/common/CelOptions.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ public Builder enableHeterogeneousNumericComparisons(boolean value) {
412412
public abstract Builder enableUnsignedLongs(boolean value);
413413

414414
/**
415-
* Perform equality using {@code ProtoEquality} proto equality.
415+
* Perform equality using {@code ProtoEquality} proto equality. {@code CelLiteRuntime} always
416+
* applies this equality regardless of this option.
416417
*
417418
* <p>CEL's alternative implementation to the {@code Message#equals} comes with a few key
418419
* differences:

‎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: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,23 +308,21 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
308308
case SFIXED32:
309309
case SINT32:
310310
case INT32:
311-
return unwrapAndConvert(INT_CONVERTER);
311+
return unwrapAndConvert(fieldDescriptor, Long.class, INT_CONVERTER);
312312
case FIXED32:
313313
case UINT32:
314314
if (celOptions.enableUnsignedLongs()) {
315-
return unwrapAndConvert(UNSIGNED_UINT32_CONVERTER);
315+
return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT32_CONVERTER);
316316
}
317-
return unwrapAndConvert(SIGNED_UINT32_CONVERTER);
317+
return unwrapAndConvert(fieldDescriptor, Long.class, SIGNED_UINT32_CONVERTER);
318318
case FIXED64:
319319
case UINT64:
320320
if (celOptions.enableUnsignedLongs()) {
321-
return unwrapAndConvert(UNSIGNED_UINT64_CONVERTER);
321+
return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT64_CONVERTER);
322322
}
323-
return BidiConverter.of(
324-
BidiConverter.IDENTITY.forwardConverter(),
325-
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
323+
return unwrapAndCheck(fieldDescriptor, Long.class);
326324
case FLOAT:
327-
return unwrapAndConvert(DOUBLE_CONVERTER);
325+
return unwrapAndConvert(fieldDescriptor, Double.class, DOUBLE_CONVERTER);
328326
case BYTES:
329327
if (celOptions.evaluateCanonicalTypesToNativeValues()) {
330328
return BidiConverter.<Object, Object>of(
@@ -336,21 +334,24 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
336334
BidiConverter.IDENTITY.forwardConverter(),
337335
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
338336
case DOUBLE:
337+
return unwrapAndCheck(fieldDescriptor, Double.class);
339338
case SFIXED64:
340339
case SINT64:
341340
case INT64:
341+
return unwrapAndCheck(fieldDescriptor, Long.class);
342342
case STRING:
343343
case BOOL:
344344
return BidiConverter.of(
345345
BidiConverter.IDENTITY.forwardConverter(),
346346
value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value)));
347347
case ENUM:
348-
return BidiConverter.<Object, Long>of(
348+
return BidiConverter.<Object, Object>of(
349349
value -> (long) ((EnumValueDescriptor) value).getNumber(),
350-
number ->
350+
value ->
351351
fieldDescriptor
352352
.getEnumType()
353-
.findValueByNumberCreatingIfUnknown(intCheckedCast(number)));
353+
.findValueByNumberCreatingIfUnknown(
354+
intCheckedCast((Long) unwrapNumber(fieldDescriptor, Long.class, value))));
354355
case MESSAGE:
355356
return BidiConverter.<MessageOrBuilder, Object>of(
356357
this::adaptProtoToValue,
@@ -434,9 +435,31 @@ private Object maybeUnwrap(Object value) {
434435
}
435436

436437
private BidiConverter<Number, Object> unwrapAndConvert(
437-
final BidiConverter<Number, Number> original) {
438+
FieldDescriptor fieldDescriptor, Class<?> celType, BidiConverter<Number, Number> original) {
438439
return BidiConverter.of(
439440
original.forwardConverter()::convert,
440-
value -> original.backwardConverter().convert((Number) maybeUnwrap(value)));
441+
value ->
442+
original
443+
.backwardConverter()
444+
.convert((Number) unwrapNumber(fieldDescriptor, celType, value)));
445+
}
446+
447+
private BidiConverter<Object, Object> unwrapAndCheck(
448+
FieldDescriptor fieldDescriptor, Class<?> celType) {
449+
return BidiConverter.of(
450+
BidiConverter.IDENTITY.forwardConverter(),
451+
value -> unwrapNumber(fieldDescriptor, celType, value));
452+
}
453+
454+
private Object unwrapNumber(FieldDescriptor fieldDescriptor, Class<?> celType, Object value) {
455+
Object celValue = maybeUnwrap(value);
456+
if ((celValue instanceof Long || celValue instanceof UnsignedLong || celValue instanceof Double)
457+
&& !celType.isInstance(celValue)) {
458+
throw new IllegalArgumentException(
459+
String.format(
460+
"unexpected value type for field '%s' in message '%s'",
461+
fieldDescriptor.getName(), fieldDescriptor.getContainingType().getFullName()));
462+
}
463+
return celValue;
441464
}
442465
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,17 @@ private MessageLite adaptValueToUint64(Object value) {
299299
throw new IllegalArgumentException("Unexpected value type: " + value);
300300
}
301301

302-
static int intCheckedCast(long value) {
302+
/** Narrows {@code value} to an int32, or throws {@link CelNumericOverflowException}. */
303+
public static int intCheckedCast(long value) {
303304
try {
304305
return Ints.checkedCast(value);
305306
} catch (IllegalArgumentException e) {
306307
throw new CelNumericOverflowException(e);
307308
}
308309
}
309310

310-
static int unsignedIntCheckedCast(long value) {
311+
/** Narrows {@code value} to a uint32, or throws {@link CelNumericOverflowException}. */
312+
public static int unsignedIntCheckedCast(long value) {
311313
try {
312314
return UnsignedInts.checkedCast(value);
313315
} catch (IllegalArgumentException e) {
@@ -316,14 +318,12 @@ static int unsignedIntCheckedCast(long value) {
316318
}
317319

318320
private static Any packAnyMessage(MessageLite msg, WellKnownProto wellKnownProto) {
319-
return packAnyMessage(msg, wellKnownProto.typeName());
321+
return packAny(msg.toByteString(), wellKnownProto.typeName());
320322
}
321323

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

329329
private Timestamp adaptValueToProtoTimestamp(Object value) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ java_library(
322322
name = "proto_message_lite_value",
323323
srcs = [
324324
"ProtoLiteCelValueConverter.java",
325+
"ProtoLiteEquality.java",
325326
"ProtoMessageLiteValue.java",
326327
"RawProtoMessageLiteValue.java",
327328
"WireMessageLite.java",
@@ -356,6 +357,7 @@ cel_android_library(
356357
name = "proto_message_lite_value_android",
357358
srcs = [
358359
"ProtoLiteCelValueConverter.java",
360+
"ProtoLiteEquality.java",
359361
"ProtoMessageLiteValue.java",
360362
"RawProtoMessageLiteValue.java",
361363
"WireMessageLite.java",
@@ -393,10 +395,16 @@ java_library(
393395
deps = [
394396
":base_proto_message_value_provider",
395397
":proto_message_lite_value",
398+
"//common:options",
396399
"//common/annotations",
397400
"//common/internal:cel_lite_descriptor_pool",
398401
"//common/internal:default_lite_descriptor_pool",
402+
"//common/internal:proto_lite_adapter",
403+
"//common/internal:proto_time_utils",
404+
"//common/internal:well_known_proto",
405+
"//common/values",
399406
"//common/values:base_proto_cel_value_converter",
407+
"//common/values:cel_byte_string",
400408
"//protobuf:cel_lite_descriptor",
401409
"@maven//:com_google_errorprone_error_prone_annotations",
402410
"@maven//:com_google_guava_guava",
@@ -412,10 +420,16 @@ cel_android_library(
412420
deps = [
413421
":base_proto_message_value_provider_android",
414422
":proto_message_lite_value_android",
423+
"//common:options",
415424
"//common/annotations",
416425
"//common/internal:cel_lite_descriptor_pool_android",
417426
"//common/internal:default_lite_descriptor_pool_android",
427+
"//common/internal:proto_lite_adapter_android",
428+
"//common/internal:proto_time_utils_android",
429+
"//common/internal:well_known_proto_android",
418430
"//common/values:base_proto_cel_value_converter_android",
431+
"//common/values:cel_byte_string",
432+
"//common/values:values_android",
419433
"//protobuf:cel_lite_descriptor",
420434
"@maven//:com_google_errorprone_error_prone_annotations",
421435
"@maven_android//:com_google_guava_guava",

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ public static boolean containsMapKey(Map<?, ?> map, Object key) {
221221
return false;
222222
}
223223

224+
/**
225+
* Returns whether {@code x} and {@code y} are equal in CEL, if they're protobuf messages this
226+
* converter knows how to compare.
227+
*/
228+
public Optional<Boolean> messageEquals(Object x, Object y) {
229+
return Optional.empty();
230+
}
231+
224232
protected Object normalizePrimitive(Object value) {
225233
Preconditions.checkNotNull(value);
226234

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import dev.cel.common.annotations.Internal;
21+
import java.util.Optional;
2122
import org.jspecify.annotations.Nullable;
2223

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

8283
return value;
8384
}
85+
86+
@Override
87+
public Optional<Boolean> messageEquals(Object x, Object y) {
88+
for (CelValueConverter converter : converters) {
89+
Optional<Boolean> result = converter.messageEquals(x, y);
90+
if (result.isPresent()) {
91+
return result;
92+
}
93+
}
94+
return Optional.empty();
95+
}
8496
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@
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.
69-
private static final int MAX_ANY_NESTING_DEPTH = 100;
69+
static final int MAX_ANY_NESTING_DEPTH = 100;
7070

7171
private final CelLiteDescriptorPool descriptorPool;
72+
private final ProtoLiteEquality protoLiteEquality;
7273

7374
public static ProtoLiteCelValueConverter newInstance(
7475
CelLiteDescriptorPool celLiteDescriptorPool) {
@@ -168,7 +169,7 @@ private Object mergeOrReadMessageField(
168169

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

@@ -190,7 +191,7 @@ static MessageLite parseMessageLite(ByteString bytes, MessageLiteDescriptor desc
190191
return mergeMessageLite(descriptor.newMessageBuilder(), bytes, descriptor.getProtoTypeName());
191192
}
192193

193-
private static MessageLite mergeMessageLite(
194+
static MessageLite mergeMessageLite(
194195
MessageLite.Builder builder, ByteString bytes, String protoTypeName) {
195196
try {
196197
return builder.mergeFrom(bytes, ExtensionRegistryLite.getEmptyRegistry()).build();
@@ -215,6 +216,13 @@ public Object toRuntimeValue(Object value) {
215216
return super.toRuntimeValue(value);
216217
}
217218

219+
@Override
220+
public Optional<Boolean> messageEquals(Object x, Object y) {
221+
return x instanceof MessageLite && y instanceof MessageLite
222+
? Optional.of(protoLiteEquality.equals((MessageLite) x, (MessageLite) y))
223+
: Optional.empty();
224+
}
225+
218226
private Object toRuntimeValue(MessageLite msg, MessageLiteDescriptor descriptor) {
219227
WellKnownProto wellKnownProto =
220228
WellKnownProto.getByTypeName(descriptor.getProtoTypeName()).orElse(null);
@@ -805,5 +813,6 @@ private static IllegalArgumentException newDecodeException(String protoTypeName,
805813

806814
private ProtoLiteCelValueConverter(CelLiteDescriptorPool celLiteDescriptorPool) {
807815
this.descriptorPool = checkNotNull(celLiteDescriptorPool);
816+
this.protoLiteEquality = new ProtoLiteEquality(celLiteDescriptorPool);
808817
}
809818
}

0 commit comments

Comments
 (0)