Skip to content

Commit

Permalink
chore: use google.golang.org/protobuf only
Browse files Browse the repository at this point in the history
github.com/golang/protobuf is deprecated.
ref. golang/protobuf#1306
  • Loading branch information
zoncoen committed Dec 28, 2021
1 parent 0b7481e commit c6967bd
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 53 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/fatih/color v1.13.0
github.com/goccy/go-yaml v1.9.4
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.6
github.com/hashicorp/go-multierror v1.1.1
github.com/lestrrat-go/backoff v1.0.1
Expand All @@ -25,6 +24,7 @@ require (

require (
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package grpc
import (
"reflect"

"github.com/golang/protobuf/proto" // nolint:staticcheck
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/zoncoen/scenarigo/assert"
Expand Down
16 changes: 10 additions & 6 deletions protocol/grpc/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"strings"

"github.com/goccy/go-yaml"
"github.com/golang/protobuf/proto" // nolint:staticcheck
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

// Register proto messages to unmarshal com.google.protobuf.Any.
_ "google.golang.org/genproto/googleapis/rpc/errdetails"
Expand Down Expand Up @@ -172,7 +173,7 @@ func (e *Expect) assertStatusDetails(sts *status.Status) error {
return errors.Errorf(`expected status.details[%d] is "%s" but got detail is not a proto message: "%#v"`, i, expectName, actualDetails[i])
}

if name := string(proto.MessageV2(actual).ProtoReflect().Descriptor().FullName()); name != expectName {
if name := string(actual.ProtoReflect().Descriptor().FullName()); name != expectName {
return errors.Errorf(`expected status.details[%d] is "%s" but got detail is "%s"%s`, i, expectName, name, appendDetailsString(sts))
}

Expand All @@ -185,21 +186,24 @@ func (e *Expect) assertStatusDetails(sts *status.Status) error {
}

func appendDetailsString(sts *status.Status) string {
format := "%s: {%s}"
format := "%s: %s"
var details []string

for _, i := range sts.Details() {
if pb, ok := i.(proto.Message); ok {
details = append(details, fmt.Sprintf(format, proto.MessageV2(pb).ProtoReflect().Descriptor().FullName(), pb.String()))
continue
b, err := protojson.Marshal(pb)
if err == nil {
details = append(details, fmt.Sprintf(format, pb.ProtoReflect().Descriptor().FullName(), string(b)))
continue
}
}

if e, ok := i.(interface{ Error() string }); ok {
details = append(details, fmt.Sprintf(format, "<non proto message>", e.Error()))
continue
}

details = append(details, fmt.Sprintf(format, "<non proto message>", fmt.Sprintf("{%#v}", i)))
details = append(details, fmt.Sprintf(format, "<non proto message>", fmt.Sprintf("%#v", i)))
}

if len(details) == 0 {
Expand Down
91 changes: 52 additions & 39 deletions protocol/grpc/expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"testing"

"github.com/goccy/go-yaml"
"github.com/golang/protobuf/proto" // nolint:staticcheck
"google.golang.org/genproto/googleapis/rpc/errdetails"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/testdata/gen/pb/test"
Expand Down Expand Up @@ -161,16 +163,19 @@ func TestExpect_Build(t *testing.T) {
v: response{
rvalues: []reflect.Value{
reflect.Zero(reflect.TypeOf(&test.EchoResponse{})),
reflect.ValueOf(mustWithDetails(
status.New(codes.InvalidArgument, "invalid argument"),
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
).Err()),
reflect.ValueOf(status.FromProto(&spb.Status{
Code: int32(codes.InvalidArgument),
Message: "invalid argument",
Details: mustAny(
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
),
}).Err()),
},
},
},
Expand All @@ -184,9 +189,7 @@ func TestExpect_Build(t *testing.T) {
v: response{
rvalues: []reflect.Value{
reflect.Zero(reflect.TypeOf(&test.EchoResponse{})),
reflect.ValueOf(mustWithDetails(
status.New(codes.InvalidArgument, "invalid argument"),
).Err()),
reflect.ValueOf(status.New(codes.InvalidArgument, "invalid argument").Err()),
},
},
},
Expand Down Expand Up @@ -522,16 +525,19 @@ func TestExpect_Build(t *testing.T) {
v: response{
rvalues: []reflect.Value{
reflect.Zero(reflect.TypeOf(&test.EchoResponse{})),
reflect.ValueOf(mustWithDetails(
status.New(codes.InvalidArgument, "invalid argument"),
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
).Err()),
reflect.ValueOf(status.FromProto(&spb.Status{
Code: int32(codes.InvalidArgument),
Message: "invalid argument",
Details: mustAny(
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
),
}).Err()),
},
},
expectAssertError: true,
Expand All @@ -554,16 +560,19 @@ func TestExpect_Build(t *testing.T) {
v: response{
rvalues: []reflect.Value{
reflect.Zero(reflect.TypeOf(&test.EchoResponse{})),
reflect.ValueOf(mustWithDetails(
status.New(codes.InvalidArgument, "invalid argument"),
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
).Err()),
reflect.ValueOf(status.FromProto(&spb.Status{
Code: int32(codes.InvalidArgument),
Message: "invalid argument",
Details: mustAny(
&errdetails.LocalizedMessage{
Locale: "ja-JP",
Message: "エラー",
},
&errdetails.DebugInfo{
Detail: "debug",
},
),
}).Err()),
},
},
expectAssertError: true,
Expand Down Expand Up @@ -626,10 +635,14 @@ func TestExpect_Build(t *testing.T) {
})
}

func mustWithDetails(s *status.Status, details ...proto.Message) *status.Status {
ss, err := s.WithDetails(details...)
if err != nil {
panic(err)
func mustAny(pbs ...proto.Message) []*anypb.Any {
res := make([]*anypb.Any, len(pbs))
for i, pb := range pbs {
a, err := anypb.New(pb)
if err != nil {
panic(err)
}
res[i] = a
}
return ss
return res
}
10 changes: 6 additions & 4 deletions protocol/grpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/goccy/go-yaml"
"github.com/golang/protobuf/jsonpb" // nolint:staticcheck
"github.com/golang/protobuf/proto" // nolint:staticcheck
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -201,14 +201,16 @@ func buildRequestMsg(ctx *context.Context, req interface{}, src interface{}) err
if err != nil {
return err
}
if x == nil {
return nil
}
var buf bytes.Buffer
if err := yaml.NewEncoder(&buf, yaml.JSON()).Encode(x); err != nil {
return err
}
message, ok := req.(proto.Message)
if ok {
r := bytes.NewReader(buf.Bytes())
if err := jsonpb.Unmarshal(r, message); err != nil {
if err := protojson.Unmarshal(buf.Bytes(), message); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/goccy/go-yaml"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto" // nolint:staticcheck
"github.com/google/go-cmp/cmp"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/internal/mockutil"
Expand All @@ -19,6 +18,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
)

Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"reflect"

"github.com/golang/protobuf/proto" // nolint:staticcheck
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)

var (
Expand Down

0 comments on commit c6967bd

Please sign in to comment.