Remove gogo/protobuf and adjust types

This commit migrates containerd/protobuf from github.com/gogo/protobuf
to google.golang.org/protobuf and adjust types. Proto-generated structs
cannot be passed as values.

Fixes #6564.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato
2022-04-19 16:25:10 +00:00
parent fd37cc75be
commit e3db7de8f5
8 changed files with 44 additions and 53 deletions

View File

@@ -17,28 +17,28 @@
package protobuf
import (
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl"
"google.golang.org/protobuf/types/known/anypb"
)
// FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any.
func FromAny(from typeurl.Any) *types.Any {
func FromAny(from typeurl.Any) *anypb.Any {
if from == nil {
return nil
}
if pbany, ok := from.(*types.Any); ok {
if pbany, ok := from.(*anypb.Any); ok {
return pbany
}
return &types.Any{
return &anypb.Any{
TypeUrl: from.GetTypeUrl(),
Value: from.GetValue(),
}
}
// FromAny converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any.
func MarshalAnyToProto(from interface{}) (*types.Any, error) {
func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
any, err := typeurl.MarshalAny(from)
if err != nil {
return nil, err

View File

@@ -18,13 +18,13 @@
package proto
import (
gogo "github.com/gogo/protobuf/proto"
google "google.golang.org/protobuf/proto"
)
func Marshal(input gogo.Message) ([]byte, error) {
return gogo.Marshal(input)
func Marshal(input google.Message) ([]byte, error) {
return google.Marshal(input)
}
func Unmarshal(input []byte, output gogo.Message) error {
return gogo.Unmarshal(input, output)
func Unmarshal(input []byte, output google.Message) error {
return google.Unmarshal(input, output)
}

View File

@@ -19,30 +19,18 @@ package protobuf
import (
"time"
"github.com/gogo/protobuf/types"
"google.golang.org/protobuf/types/known/timestamppb"
)
// Once we migrate off from gogo/protobuf, we can use the function below, which don't return any errors.
// https://github.com/protocolbuffers/protobuf-go/blob/v1.28.0/types/known/timestamppb/timestamp.pb.go#L200-L208
// ToTimestamp creates protobuf's Timestamp from time.Time.
func ToTimestamp(from time.Time) *types.Timestamp {
pt, err := types.TimestampProto(from)
if err != nil {
panic(err)
}
return pt
func ToTimestamp(from time.Time) *timestamppb.Timestamp {
return timestamppb.New(from)
}
// FromTimestamp creates time.Time from protobuf's Timestamp.
func FromTimestamp(from *types.Timestamp) time.Time {
if from == nil {
// Return time.Time's zero value as like timestamppb.
return time.Time{}.UTC()
}
tt, err := types.TimestampFromProto(from)
if err != nil {
panic(err)
}
return tt
func FromTimestamp(from *timestamppb.Timestamp) time.Time {
return from.AsTime()
}

View File

@@ -18,9 +18,11 @@
package types
import (
gogo "github.com/gogo/protobuf/types"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
)
type Empty = gogo.Empty
type Any = gogo.Any
type FieldMask = gogo.FieldMask
type Empty = emptypb.Empty
type Any = anypb.Any
type FieldMask = field_mask.FieldMask