Skip to content

Commit

Permalink
Remove gogo registry
Browse files Browse the repository at this point in the history
Since containerd is not using gogo anymore it seems like we should
remove the depenency on gogo here since gogo is a fairly large
dependency and is also unaintained.

This also gets imported by the runc shim and adds a fair amount to the
binary size (in terms of percentage of the whole size).

I did keep a fallback test in that still imports gogo and continues to
work. The test marshals a type with gogo and then unmarshals it by
typeurl.
This is why gogo is still in the go.mod
Not sure if this is worthwhile or not.

This may need a module version bump since some functions will behave
differently now.

Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Oct 12, 2024
1 parent 35cf9db commit 7d3d258
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down Expand Up @@ -33,6 +32,5 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
43 changes: 8 additions & 35 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"reflect"
"sync"

gogoproto "github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/anypb"
Expand Down Expand Up @@ -112,8 +111,6 @@ func TypeURL(v interface{}) (string, error) {
switch t := v.(type) {
case proto.Message:
return string(t.ProtoReflect().Descriptor().FullName()), nil
case gogoproto.Message:
return gogoproto.MessageName(t), nil
default:
return "", fmt.Errorf("type %s: %w", reflect.TypeOf(v), ErrNotFound)
}
Expand Down Expand Up @@ -149,10 +146,6 @@ func MarshalAny(v interface{}) (Any, error) {
marshal = func(v interface{}) ([]byte, error) {
return proto.Marshal(t)
}
case gogoproto.Message:
marshal = func(v interface{}) ([]byte, error) {
return gogoproto.Marshal(t)
}
default:
marshal = json.Marshal
}
Expand Down Expand Up @@ -229,7 +222,7 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
}

if v == nil {
v = reflect.New(t.t).Interface()
v = reflect.New(t).Interface()
} else {
// Validate interface type provided by client
vURL, err := TypeURL(v)
Expand All @@ -241,51 +234,31 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
}
}

if t.isProto {
switch t := v.(type) {
case proto.Message:
err = proto.Unmarshal(value, t)
case gogoproto.Message:
err = gogoproto.Unmarshal(value, t)
}
pm, ok := v.(proto.Message)
if ok {
err = proto.Unmarshal(value, pm)
} else {
err = json.Unmarshal(value, v)
}

return v, err
}

type urlType struct {
t reflect.Type
isProto bool
}

func getTypeByUrl(url string) (urlType, error) {
func getTypeByUrl(url string) (reflect.Type, error) {
mu.RLock()
for t, u := range registry {
if u == url {
mu.RUnlock()
return urlType{
t: t,
}, nil
return t, nil
}
}
mu.RUnlock()
// fallback to proto registry
t := gogoproto.MessageType(url)
if t != nil {
return urlType{
// get the underlying Elem because proto returns a pointer to the type
t: t.Elem(),
isProto: true,
}, nil
}
mt, err := protoregistry.GlobalTypes.FindMessageByURL(url)
if err != nil {
return urlType{}, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
return nil, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
}
empty := mt.New().Interface()
return urlType{t: reflect.TypeOf(empty).Elem(), isProto: true}, nil
return reflect.TypeOf(empty).Elem(), nil
}

func tryDereference(v interface{}) reflect.Type {
Expand Down
2 changes: 0 additions & 2 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/gogo/protobuf/proto"
gogotypes "github.com/gogo/protobuf/types"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand All @@ -37,7 +36,6 @@ func clear() {
registry = make(map[reflect.Type]string)
}

var _ Any = &gogotypes.Any{}
var _ Any = &anypb.Any{}

func TestRegisterPointerGetPointer(t *testing.T) {
Expand Down

0 comments on commit 7d3d258

Please sign in to comment.