@@ -32,16 +32,8 @@ import (
3232var (
3333 mu sync.RWMutex
3434 registry = make (map [reflect.Type ]string )
35- handlers []handler
3635)
3736
38- type handler interface {
39- Marshaller (interface {}) func () ([]byte , error )
40- Unmarshaller (interface {}) func ([]byte ) error
41- TypeURL (interface {}) string
42- GetType (url string ) reflect.Type
43- }
44-
4537// Definitions of common error types used throughout typeurl.
4638//
4739// These error types are used with errors.Wrap and errors.Wrapf to add context
@@ -120,11 +112,6 @@ func TypeURL(v interface{}) (string, error) {
120112 case proto.Message :
121113 return string (t .ProtoReflect ().Descriptor ().FullName ()), nil
122114 default :
123- for _ , h := range handlers {
124- if u := h .TypeURL (v ); u != "" {
125- return u , nil
126- }
127- }
128115 return "" , fmt .Errorf ("type %s: %w" , reflect .TypeOf (v ), ErrNotFound )
129116 }
130117 }
@@ -160,18 +147,7 @@ func MarshalAny(v interface{}) (Any, error) {
160147 return proto .Marshal (t )
161148 }
162149 default :
163- for _ , h := range handlers {
164- if m := h .Marshaller (v ); m != nil {
165- marshal = func (v interface {}) ([]byte , error ) {
166- return m ()
167- }
168- break
169- }
170- }
171-
172- if marshal == nil {
173- marshal = json .Marshal
174- }
150+ marshal = json .Marshal
175151 }
176152
177153 url , err := TypeURL (v )
@@ -260,17 +236,12 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
260236
261237 pm , ok := v .(proto.Message )
262238 if ok {
263- return v , proto .Unmarshal (value , pm )
264- }
265-
266- for _ , h := range handlers {
267- if unmarshal := h .Unmarshaller (v ); unmarshal != nil {
268- return v , unmarshal (value )
269- }
239+ err = proto .Unmarshal (value , pm )
240+ } else {
241+ err = json .Unmarshal (value , v )
270242 }
271243
272- // fallback to json unmarshaller
273- return v , json .Unmarshal (value , v )
244+ return v , err
274245}
275246
276247func getTypeByUrl (url string ) (reflect.Type , error ) {
@@ -284,16 +255,7 @@ func getTypeByUrl(url string) (reflect.Type, error) {
284255 mu .RUnlock ()
285256 mt , err := protoregistry .GlobalTypes .FindMessageByURL (url )
286257 if err != nil {
287- e := protoregistry .NotFound
288- if ! errors .Is (err , e ) {
289- return nil , fmt .Errorf ("type with url %s: %w" , url , ErrNotFound )
290- }
291-
292- for _ , h := range handlers {
293- if t := h .GetType (url ); t != nil {
294- return t , nil
295- }
296- }
258+ return nil , fmt .Errorf ("type with url %s: %w" , url , ErrNotFound )
297259 }
298260 empty := mt .New ().Interface ()
299261 return reflect .TypeOf (empty ).Elem (), nil
0 commit comments