Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/containerd/typeurl

go 1.13

require (
github.com/gogo/protobuf v1.3.2
github.com/pkg/errors v0.9.1
)
require github.com/gogo/protobuf v1.3.2
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
16 changes: 9 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package typeurl

import (
"encoding/json"
"errors"
"fmt"
"path"
"reflect"
"sync"

"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
)

var (
Expand All @@ -39,6 +40,7 @@ var (
//
// To detect an error class, use errors.Is() functions to tell whether an
// error is of this type.

var (
ErrNotFound = errors.New("not found")
)
Expand All @@ -56,7 +58,7 @@ func Register(v interface{}, args ...string) {
defer mu.Unlock()
if et, ok := registry[t]; ok {
if et != p {
panic(errors.Errorf("type registered with alternate path %q != %q", et, p))
panic(fmt.Errorf("type registered with alternate path %q != %q", et, p))
}
return
}
Expand All @@ -72,7 +74,7 @@ func TypeURL(v interface{}) (string, error) {
// fallback to the proto registry if it is a proto message
pb, ok := v.(proto.Message)
if !ok {
return "", errors.Wrapf(ErrNotFound, "type %s", reflect.TypeOf(v))
return "", fmt.Errorf("type %s: %w", reflect.TypeOf(v), ErrNotFound)
}
return proto.MessageName(pb), nil
}
Expand All @@ -98,7 +100,7 @@ func MarshalAny(v interface{}) (*types.Any, error) {
var marshal func(v interface{}) ([]byte, error)
switch t := v.(type) {
case *types.Any:
// avoid reserializing the type if we have an any.
// avoid re-serializing the type if we have an any.
return t, nil
case proto.Message:
marshal = func(v interface{}) ([]byte, error) {
Expand Down Expand Up @@ -140,7 +142,7 @@ func UnmarshalTo(any *types.Any, out interface{}) error {
return UnmarshalToByTypeURL(any.TypeUrl, any.Value, out)
}

// UnmarshalTo unmarshals the given type and value into a concrete type passed
// UnmarshalToByTypeURL unmarshals the given type and value into a concrete type passed
// in the out argument. It is identical to UnmarshalByTypeURL, but lets clients
// provide a destination type through the out argument.
func UnmarshalToByTypeURL(typeURL string, value []byte, out interface{}) error {
Expand All @@ -163,7 +165,7 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
return nil, err
}
if typeURL != vURL {
return nil, errors.Errorf("can't unmarshal type %q to output %q", typeURL, vURL)
return nil, fmt.Errorf("can't unmarshal type %q to output %q", typeURL, vURL)
}
}

Expand Down Expand Up @@ -201,7 +203,7 @@ func getTypeByUrl(url string) (urlType, error) {
isProto: true,
}, nil
}
return urlType{}, errors.Wrapf(ErrNotFound, "type with url %s", url)
return urlType{}, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
}

func tryDereference(v interface{}) reflect.Type {
Expand Down