@@ -39,7 +39,7 @@ type handler interface {
3939 Marshaller (interface {}) func () ([]byte , error )
4040 Unmarshaller (interface {}) func ([]byte ) error
4141 TypeURL (interface {}) string
42- GetType (url string ) reflect.Type
42+ GetType (url string ) ( reflect.Type , bool )
4343}
4444
4545// Definitions of common error types used throughout typeurl.
@@ -240,7 +240,7 @@ func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
240240}
241241
242242func unmarshal (typeURL string , value []byte , v interface {}) (interface {}, error ) {
243- t , err := getTypeByUrl (typeURL )
243+ t , isProto , err := getTypeByUrl (typeURL )
244244 if err != nil {
245245 return nil , err
246246 }
@@ -258,43 +258,45 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
258258 }
259259 }
260260
261- pm , ok := v .(proto.Message )
262- if ok {
263- return v , proto .Unmarshal (value , pm )
264- }
261+ if isProto {
262+ pm , ok := v .(proto.Message )
263+ if ok {
264+ return v , proto .Unmarshal (value , pm )
265+ }
265266
266- for _ , h := range handlers {
267- if unmarshal := h .Unmarshaller (v ); unmarshal != nil {
268- return v , unmarshal (value )
267+ for _ , h := range handlers {
268+ if unmarshal := h .Unmarshaller (v ); unmarshal != nil {
269+ return v , unmarshal (value )
270+ }
269271 }
270272 }
271273
272274 // fallback to json unmarshaller
273275 return v , json .Unmarshal (value , v )
274276}
275277
276- func getTypeByUrl (url string ) (reflect.Type , error ) {
278+ func getTypeByUrl (url string ) (_ reflect.Type , isProto bool , _ error ) {
277279 mu .RLock ()
278280 for t , u := range registry {
279281 if u == url {
280282 mu .RUnlock ()
281- return t , nil
283+ return t , false , nil
282284 }
283285 }
284286 mu .RUnlock ()
285287 mt , err := protoregistry .GlobalTypes .FindMessageByURL (url )
286288 if err != nil {
287289 if errors .Is (err , protoregistry .NotFound ) {
288290 for _ , h := range handlers {
289- if t := h .GetType (url ); t != nil {
290- return t , nil
291+ if t , isProto := h .GetType (url ); t != nil {
292+ return t , isProto , nil
291293 }
292294 }
293295 }
294- return nil , fmt .Errorf ("type with url %s: %w" , url , ErrNotFound )
296+ return nil , false , fmt .Errorf ("type with url %s: %w" , url , ErrNotFound )
295297 }
296298 empty := mt .New ().Interface ()
297- return reflect .TypeOf (empty ).Elem (), nil
299+ return reflect .TypeOf (empty ).Elem (), true , nil
298300}
299301
300302func tryDereference (v interface {}) reflect.Type {
0 commit comments