Skip to content

Commit bdb7f49

Browse files
committed
Remove gogo registry
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]>
1 parent 35cf9db commit bdb7f49

File tree

3 files changed

+8
-40
lines changed

3 files changed

+8
-40
lines changed

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
22
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
3-
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
43
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
54
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
65
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -33,6 +32,5 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
3332
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3433
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
3534
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
36-
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
3735
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
3836
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

types.go

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"reflect"
2525
"sync"
2626

27-
gogoproto "github.com/gogo/protobuf/proto"
2827
"google.golang.org/protobuf/proto"
2928
"google.golang.org/protobuf/reflect/protoregistry"
3029
"google.golang.org/protobuf/types/known/anypb"
@@ -112,8 +111,6 @@ func TypeURL(v interface{}) (string, error) {
112111
switch t := v.(type) {
113112
case proto.Message:
114113
return string(t.ProtoReflect().Descriptor().FullName()), nil
115-
case gogoproto.Message:
116-
return gogoproto.MessageName(t), nil
117114
default:
118115
return "", fmt.Errorf("type %s: %w", reflect.TypeOf(v), ErrNotFound)
119116
}
@@ -149,10 +146,6 @@ func MarshalAny(v interface{}) (Any, error) {
149146
marshal = func(v interface{}) ([]byte, error) {
150147
return proto.Marshal(t)
151148
}
152-
case gogoproto.Message:
153-
marshal = func(v interface{}) ([]byte, error) {
154-
return gogoproto.Marshal(t)
155-
}
156149
default:
157150
marshal = json.Marshal
158151
}
@@ -229,7 +222,7 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
229222
}
230223

231224
if v == nil {
232-
v = reflect.New(t.t).Interface()
225+
v = reflect.New(t).Interface()
233226
} else {
234227
// Validate interface type provided by client
235228
vURL, err := TypeURL(v)
@@ -241,51 +234,30 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
241234
}
242235
}
243236

244-
if t.isProto {
245-
switch t := v.(type) {
246-
case proto.Message:
247-
err = proto.Unmarshal(value, t)
248-
case gogoproto.Message:
249-
err = gogoproto.Unmarshal(value, t)
250-
}
237+
pm, ok := v.(proto.Message)
238+
if ok {
239+
err = proto.Unmarshal(value, pm)
251240
} else {
252241
err = json.Unmarshal(value, v)
253242
}
254243

255244
return v, err
256245
}
257246

258-
type urlType struct {
259-
t reflect.Type
260-
isProto bool
261-
}
262-
263-
func getTypeByUrl(url string) (urlType, error) {
247+
func getTypeByUrl(url string) (reflect.Type, error) {
264248
mu.RLock()
265249
for t, u := range registry {
266250
if u == url {
267251
mu.RUnlock()
268-
return urlType{
269-
t: t,
270-
}, nil
252+
return t, nil
271253
}
272254
}
273-
mu.RUnlock()
274-
// fallback to proto registry
275-
t := gogoproto.MessageType(url)
276-
if t != nil {
277-
return urlType{
278-
// get the underlying Elem because proto returns a pointer to the type
279-
t: t.Elem(),
280-
isProto: true,
281-
}, nil
282-
}
283255
mt, err := protoregistry.GlobalTypes.FindMessageByURL(url)
284256
if err != nil {
285-
return urlType{}, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
257+
return nil, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
286258
}
287259
empty := mt.New().Interface()
288-
return urlType{t: reflect.TypeOf(empty).Elem(), isProto: true}, nil
260+
return reflect.TypeOf(empty).Elem(), nil
289261
}
290262

291263
func tryDereference(v interface{}) reflect.Type {

types_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/gogo/protobuf/proto"
26-
gogotypes "github.com/gogo/protobuf/types"
2726
"google.golang.org/protobuf/types/known/anypb"
2827
"google.golang.org/protobuf/types/known/timestamppb"
2928
)
@@ -37,7 +36,6 @@ func clear() {
3736
registry = make(map[reflect.Type]string)
3837
}
3938

40-
var _ Any = &gogotypes.Any{}
4139
var _ Any = &anypb.Any{}
4240

4341
func TestRegisterPointerGetPointer(t *testing.T) {

0 commit comments

Comments
 (0)