3030import com .google .protobuf .ByteString ;
3131import com .google .protobuf .CodedInputStream ;
3232import com .google .protobuf .WireFormat ;
33- import dev .cel .common .annotations .Internal ;
3433import dev .cel .common .exceptions .CelAttributeNotFoundException ;
3534import dev .cel .common .types .CelType ;
3635import dev .cel .common .types .StructTypeReference ;
4948 * client-server version skew issues where newer fields or submessages lack generated classes and
5049 * descriptors in the evaluation environment.
5150 *
52- * <p>Rather than requiring compiled {@link MessageLite} classes or runtime schema descriptors, this
51+ * <p>Rather than requiring compiled {@code MessageLite} classes or runtime schema descriptors, this
5352 * value encapsulates the raw wire-format {@link ByteString} payload and performs classless,
5453 * reflection-free field traversal directly over wire tags via {@link CodedInputStream}.
5554 */
5655@ AutoValue
5756@ AutoValue .CopyAnnotations
5857@ Immutable
5958@ SuppressWarnings ("Immutable" ) // Immutable wire fields
60- @ Internal
61- public abstract class RawProtoMessageLiteValue extends StructValue <String , RawProtoMessageLiteValue >
62- implements OptimizedSelectable {
59+ abstract class RawProtoMessageLiteValue extends StructValue <String , WireMessageLite >
60+ implements OptimizedSelectable , WireMessageLite {
6361
6462 private static final String UNKNOWN_MESSAGE_TYPE_NAME = "cel.@unknownMessage" ;
6563 private static final int MAP_KEY_FIELD_NUMBER = 1 ;
6664 private static final int MAP_VALUE_FIELD_NUMBER = 2 ;
6765
68- abstract ByteString rawWireBytes ();
66+ @ Override
67+ public abstract ByteString toByteString ();
6968
7069 @ Override
7170 public abstract CelType celType ();
7271
7372 abstract ProtoLiteCelValueConverter protoLiteCelValueConverter ();
7473
7574 @ Override
76- public RawProtoMessageLiteValue value () {
75+ public String protoTypeName () {
76+ return celType ().name ();
77+ }
78+
79+ @ Override
80+ public WireMessageLite value () {
7781 return this ;
7882 }
7983
84+ @ Override
85+ public final boolean equals (Object other ) {
86+ // TODO: Support message equality
87+ throw new UnsupportedOperationException ("Message equality is not supported" );
88+ }
89+
90+ @ Override
91+ public final int hashCode () {
92+ throw new UnsupportedOperationException ("Message equality is not supported" );
93+ }
94+
8095 @ Memoized
8196 ImmutableListMultimap <Integer , Object > unknownFields () {
8297 try {
83- CodedInputStream inputStream = rawWireBytes ().newCodedInput ();
98+ CodedInputStream inputStream = toByteString ().newCodedInput ();
8499 Multimap <Integer , Object > fields = Multimaps .newMultimap (new TreeMap <>(), ArrayList ::new );
85100 for (int tag = inputStream .readTag (); tag != 0 ; tag = inputStream .readTag ()) {
86101 int tagWireType = WireFormat .getTagWireType (tag );
@@ -96,7 +111,7 @@ ImmutableListMultimap<Integer, Object> unknownFields() {
96111
97112 @ Override
98113 public boolean isZeroValue () {
99- return rawWireBytes ().isEmpty ();
114+ return toByteString ().isEmpty ();
100115 }
101116
102117 /**
@@ -169,27 +184,15 @@ private static Object decodeWireField(
169184 }
170185
171186 boolean isRepeated = field .defaultValue () instanceof List ;
172- String protoTypeName = resolveProtoTypeName (field );
173-
174- return decodeWireEntries (unknowns , typeCode , protoTypeName , isRepeated , converter );
175- }
176187
177- /**
178- * Resolves the protobuf message type name for a field from the optimizer metadata in {@link
179- * SelectField}, or {@link #UNKNOWN_MESSAGE_TYPE_NAME} if unspecified.
180- */
181- private static String resolveProtoTypeName (SelectField field ) {
182- if (!field .protoTypeName ().isEmpty ()) {
183- return field .protoTypeName ();
184- }
185- return UNKNOWN_MESSAGE_TYPE_NAME ;
188+ return decodeWireEntries (unknowns , typeCode , field .protoTypeName (), isRepeated , converter );
186189 }
187190
188191 private static Object resolveDefault (SelectField field , ProtoLiteCelValueConverter converter ) {
189192 if (field .defaultValue () != null ) {
190193 return field .defaultValue ();
191194 }
192- return create (ByteString .EMPTY , resolveProtoTypeName ( field ), converter );
195+ return decodeMessageValue (ByteString .EMPTY , field . protoTypeName ( ), converter );
193196 }
194197
195198 /**
@@ -419,9 +422,7 @@ static Object decodeWireValue(
419422 throw new UnsupportedOperationException ("Groups are not supported" );
420423 case MESSAGE :
421424 ByteString msgBytes = requireType (raw , ByteString .class , fieldType );
422- return converter
423- .tryDecodeWellKnownProto (msgBytes , protoTypeName )
424- .orElseGet (() -> create (msgBytes , protoTypeName , converter ));
425+ return decodeMessageValue (msgBytes , protoTypeName , converter );
425426 case BYTES :
426427 return CelByteString .of (requireType (raw , ByteString .class , fieldType ).toByteArray ());
427428 case UINT32 :
@@ -437,6 +438,13 @@ static Object decodeWireValue(
437438 throw new IllegalArgumentException ("Unsupported proto field type: " + fieldType );
438439 }
439440
441+ private static Object decodeMessageValue (
442+ ByteString msgBytes , String protoTypeName , ProtoLiteCelValueConverter converter ) {
443+ return converter
444+ .tryDecodeProtoMessage (msgBytes , protoTypeName )
445+ .orElseGet (() -> create (msgBytes , protoTypeName , converter ));
446+ }
447+
440448 private static <T > T requireType (
441449 Object raw , Class <T > expectedType , WireFormat .FieldType fieldType ) {
442450 if (!expectedType .isInstance (raw )) {
@@ -509,12 +517,12 @@ private static ImmutableList<Object> decodePacked(
509517 }
510518 }
511519
512- public static RawProtoMessageLiteValue create (
520+ static RawProtoMessageLiteValue create (
513521 ByteString rawWireBytes , ProtoLiteCelValueConverter protoLiteCelValueConverter ) {
514522 return create (rawWireBytes , "" , protoLiteCelValueConverter );
515523 }
516524
517- public static RawProtoMessageLiteValue create (
525+ static RawProtoMessageLiteValue create (
518526 ByteString rawWireBytes ,
519527 String protoTypeName ,
520528 ProtoLiteCelValueConverter protoLiteCelValueConverter ) {
0 commit comments