2020import com .google .common .annotations .VisibleForTesting ;
2121import com .google .common .base .Defaults ;
2222import com .google .common .collect .ImmutableList ;
23+ import com .google .common .collect .ImmutableListMultimap ;
2324import com .google .common .collect .ImmutableMap ;
2425import com .google .common .collect .Multimap ;
2526import com .google .common .collect .Multimaps ;
4546import java .util .List ;
4647import java .util .Map ;
4748import java .util .NoSuchElementException ;
49+ import java .util .Optional ;
4850import java .util .TreeMap ;
4951
5052/**
@@ -80,7 +82,7 @@ private static Object readPrimitiveField(
8082 case INT64 :
8183 return inputStream .readInt64 ();
8284 case UINT32 :
83- return UnsignedLong .fromLongBits (inputStream .readUInt32 ());
85+ return UnsignedLong .fromLongBits (Integer . toUnsignedLong ( inputStream .readUInt32 () ));
8486 case UINT64 :
8587 return UnsignedLong .fromLongBits (inputStream .readUInt64 ());
8688 case BOOL :
@@ -160,6 +162,17 @@ Object getDefaultCelValue(String protoTypeName, String fieldName) {
160162 return toRuntimeValue (defaultValue );
161163 }
162164
165+ public Optional <FieldLiteDescriptor > findFieldDescriptor (String protoTypeName , int fieldNumber ) {
166+ return descriptorPool
167+ .findDescriptor (protoTypeName )
168+ .flatMap (desc -> desc .findByFieldNumber (fieldNumber ));
169+ }
170+
171+ public Optional <Object > findDefaultCelValue (String protoTypeName , int fieldNumber ) {
172+ return findFieldDescriptor (protoTypeName , fieldNumber )
173+ .map (fieldDescriptor -> toRuntimeValue (getDefaultValue (fieldDescriptor )));
174+ }
175+
163176 @ Override
164177 @ SuppressWarnings ("LiteProtoToString" ) // No alternative identifier to use. Debug only info is OK.
165178 public Object toRuntimeValue (Object value ) {
@@ -193,7 +206,10 @@ protected Object fromWellKnownProto(MessageLiteOrBuilder msg, WellKnownProto wel
193206 descriptorPool
194207 .findDescriptor (message )
195208 .orElseThrow (
196- () -> new NoSuchElementException ("Could not find a descriptor for: " + message ));
209+ () ->
210+ new NoSuchElementException (
211+ "Could not find a descriptor for message of type: "
212+ + message .getClass ().getName ()));
197213 return ProtoMessageLiteValue .create (message , descriptor .getProtoTypeName (), this );
198214 }
199215
@@ -367,13 +383,11 @@ MessageFields readAllFields(byte[] bytes, String protoTypeName) throws IOExcepti
367383 return MessageFields .create (fieldValues .buildKeepingLast (), unknownFields );
368384 }
369385
370- ImmutableMap <String , Object > readAllFields (MessageLite msg , String protoTypeName )
371- throws IOException {
372- return readAllFields (msg .toByteArray (), protoTypeName ).values ();
386+ MessageFields readMessageFields (MessageLite msg , String protoTypeName ) throws IOException {
387+ return readAllFields (msg .toByteArray (), protoTypeName );
373388 }
374389
375- private static Object readUnknownField (int tagWireType , CodedInputStream inputStream )
376- throws IOException {
390+ static Object readUnknownField (int tagWireType , CodedInputStream inputStream ) throws IOException {
377391 switch (tagWireType ) {
378392 case WireFormat .WIRETYPE_VARINT :
379393 return inputStream .readInt64 ();
@@ -393,16 +407,19 @@ private static Object readUnknownField(int tagWireType, CodedInputStream inputSt
393407 }
394408
395409 @ AutoValue
396- @ SuppressWarnings ("AutoValueImmutableFields" ) // Unknowns are inaccessible to users.
410+ @ AutoValue .CopyAnnotations
411+ @ Immutable
412+ @ SuppressWarnings ("Immutable" ) // Safe immutable fields
397413 abstract static class MessageFields {
398414
399415 abstract ImmutableMap <String , Object > values ();
400416
401- abstract Multimap <Integer , Object > unknowns ();
417+ abstract ImmutableListMultimap <Integer , Object > unknowns ();
402418
403419 static MessageFields create (
404420 ImmutableMap <String , Object > fieldValues , Multimap <Integer , Object > unknownFields ) {
405- return new AutoValue_ProtoLiteCelValueConverter_MessageFields (fieldValues , unknownFields );
421+ return new AutoValue_ProtoLiteCelValueConverter_MessageFields (
422+ fieldValues , ImmutableListMultimap .copyOf (unknownFields ));
406423 }
407424 }
408425
0 commit comments