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 ;
3938import java .util .AbstractMap ;
4039import java .util .ArrayList ;
4140import java .util .List ;
41+ import java .util .Locale ;
4242import java .util .Map ;
4343import java .util .Optional ;
4444import java .util .TreeMap ;
4949 * client-server version skew issues where newer fields or submessages lack generated classes and
5050 * descriptors in the evaluation environment.
5151 *
52- * <p>Rather than requiring compiled {@link MessageLite} classes or runtime schema descriptors, this
52+ * <p>Rather than requiring compiled {@code MessageLite} classes or runtime schema descriptors, this
5353 * value encapsulates the raw wire-format {@link ByteString} payload and performs classless,
5454 * reflection-free field traversal directly over wire tags via {@link CodedInputStream}.
5555 */
5656@ AutoValue
5757@ AutoValue .CopyAnnotations
5858@ Immutable
5959@ SuppressWarnings ("Immutable" ) // Immutable wire fields
60- @ Internal
61- public abstract class RawProtoMessageLiteValue extends StructValue <String , RawProtoMessageLiteValue >
62- implements OptimizedSelectable {
60+ abstract class RawProtoMessageLiteValue extends StructValue <String , WireMessageLite >
61+ implements OptimizedSelectable , WireMessageLite {
6362
6463 private static final String UNKNOWN_MESSAGE_TYPE_NAME = "cel.@unknownMessage" ;
6564 private static final int MAP_KEY_FIELD_NUMBER = 1 ;
6665 private static final int MAP_VALUE_FIELD_NUMBER = 2 ;
6766
68- abstract ByteString rawWireBytes ();
67+ @ Override
68+ public abstract ByteString toByteString ();
6969
7070 @ Override
7171 public abstract CelType celType ();
7272
7373 abstract ProtoLiteCelValueConverter protoLiteCelValueConverter ();
7474
7575 @ Override
76- public RawProtoMessageLiteValue value () {
76+ public String protoTypeName () {
77+ return celType ().name ();
78+ }
79+
80+ @ Override
81+ public WireMessageLite value () {
7782 return this ;
7883 }
7984
85+ @ Override
86+ public final boolean equals (Object other ) {
87+ // TODO: Support message equality
88+ throw new UnsupportedOperationException ("Message equality is not supported" );
89+ }
90+
91+ @ Override
92+ public final int hashCode () {
93+ throw new UnsupportedOperationException ("Message equality is not supported" );
94+ }
95+
96+ @ Override
97+ public final String toString () {
98+ return String .format (
99+ Locale .US ,
100+ "WireMessageLite{protoTypeName=%s, size=%d}" ,
101+ protoTypeName (),
102+ toByteString ().size ());
103+ }
104+
80105 @ Memoized
81106 ImmutableListMultimap <Integer , Object > unknownFields () {
82107 try {
83- CodedInputStream inputStream = rawWireBytes ().newCodedInput ();
108+ CodedInputStream inputStream = toByteString ().newCodedInput ();
84109 Multimap <Integer , Object > fields = Multimaps .newMultimap (new TreeMap <>(), ArrayList ::new );
85110 for (int tag = inputStream .readTag (); tag != 0 ; tag = inputStream .readTag ()) {
86111 int tagWireType = WireFormat .getTagWireType (tag );
@@ -96,7 +121,7 @@ ImmutableListMultimap<Integer, Object> unknownFields() {
96121
97122 @ Override
98123 public boolean isZeroValue () {
99- return rawWireBytes ().isEmpty ();
124+ return toByteString ().isEmpty ();
100125 }
101126
102127 /**
@@ -169,27 +194,15 @@ private static Object decodeWireField(
169194 }
170195
171196 boolean isRepeated = field .defaultValue () instanceof List ;
172- String protoTypeName = resolveProtoTypeName (field );
173-
174- return decodeWireEntries (unknowns , typeCode , protoTypeName , isRepeated , converter );
175- }
176197
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 ;
198+ return decodeWireEntries (unknowns , typeCode , field .protoTypeName (), isRepeated , converter );
186199 }
187200
188201 private static Object resolveDefault (SelectField field , ProtoLiteCelValueConverter converter ) {
189202 if (field .defaultValue () != null ) {
190203 return field .defaultValue ();
191204 }
192- return create (ByteString .EMPTY , resolveProtoTypeName ( field ), converter );
205+ return decodeMessageValue (ByteString .EMPTY , field . protoTypeName ( ), converter );
193206 }
194207
195208 /**
@@ -419,9 +432,7 @@ static Object decodeWireValue(
419432 throw new UnsupportedOperationException ("Groups are not supported" );
420433 case MESSAGE :
421434 ByteString msgBytes = requireType (raw , ByteString .class , fieldType );
422- return converter
423- .tryDecodeWellKnownProto (msgBytes , protoTypeName )
424- .orElseGet (() -> create (msgBytes , protoTypeName , converter ));
435+ return decodeMessageValue (msgBytes , protoTypeName , converter );
425436 case BYTES :
426437 return CelByteString .of (requireType (raw , ByteString .class , fieldType ).toByteArray ());
427438 case UINT32 :
@@ -437,6 +448,13 @@ static Object decodeWireValue(
437448 throw new IllegalArgumentException ("Unsupported proto field type: " + fieldType );
438449 }
439450
451+ private static Object decodeMessageValue (
452+ ByteString msgBytes , String protoTypeName , ProtoLiteCelValueConverter converter ) {
453+ return converter
454+ .tryDecodeProtoMessage (msgBytes , protoTypeName )
455+ .orElseGet (() -> create (msgBytes , protoTypeName , converter ));
456+ }
457+
440458 private static <T > T requireType (
441459 Object raw , Class <T > expectedType , WireFormat .FieldType fieldType ) {
442460 if (!expectedType .isInstance (raw )) {
@@ -509,12 +527,12 @@ private static ImmutableList<Object> decodePacked(
509527 }
510528 }
511529
512- public static RawProtoMessageLiteValue create (
530+ static RawProtoMessageLiteValue create (
513531 ByteString rawWireBytes , ProtoLiteCelValueConverter protoLiteCelValueConverter ) {
514532 return create (rawWireBytes , "" , protoLiteCelValueConverter );
515533 }
516534
517- public static RawProtoMessageLiteValue create (
535+ static RawProtoMessageLiteValue create (
518536 ByteString rawWireBytes ,
519537 String protoTypeName ,
520538 ProtoLiteCelValueConverter protoLiteCelValueConverter ) {
0 commit comments