From 16785884cbe5f40358b25c834ea35a878eb4249e Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 17 May 2023 17:11:26 +0900 Subject: [PATCH] Revert https://github.com/k1LoW/grpcstub/pull/32 --- dynamic.go | 2 +- dynamic_test.go | 5 +++-- grpcstub.go | 41 +---------------------------------------- grpcstub_test.go | 25 +++---------------------- 4 files changed, 8 insertions(+), 65 deletions(-) diff --git a/dynamic.go b/dynamic.go index 467fbb0..e9d215f 100644 --- a/dynamic.go +++ b/dynamic.go @@ -92,7 +92,7 @@ func generateDynamicMessage(gs generators, r *Request, m *desc.MessageDescriptor for i := 0; i < l; i++ { fn, ok := gs.matchFunc(strings.Join(names, fieldSep)) if ok { - values = append(values, cast(fn(r))) + values = append(values, fn(r)) continue } switch f.GetType() { diff --git a/dynamic_test.go b/dynamic_test.go index eb348d6..68c18cd 100644 --- a/dynamic_test.go +++ b/dynamic_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/k1LoW/grpcstub/testdata/hello" "github.com/k1LoW/grpcstub/testdata/routeguide" + "google.golang.org/protobuf/types/known/timestamppb" ) func TestResponseDynamic(t *testing.T) { @@ -77,7 +78,7 @@ func TestResponseDynamicGenerated(t *testing.T) { want := time.Now() opts := []GeneratorOption{ Generator("*_time", func(r *Request) interface{} { - return want + return timestamppb.New(want) }), } ts.Method("Hello").ResponseDynamic(opts...) @@ -100,7 +101,7 @@ func TestResponseDynamicServer(t *testing.T) { want := time.Now() opts := []GeneratorOption{ Generator("*_time", func(r *Request) interface{} { - return want + return timestamppb.New(want) }), } ts.ResponseDynamic(opts...) diff --git a/grpcstub.go b/grpcstub.go index 9109ede..8c93b21 100644 --- a/grpcstub.go +++ b/grpcstub.go @@ -34,7 +34,6 @@ import ( "google.golang.org/protobuf/reflect/protodesc" "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoregistry" - "google.golang.org/protobuf/types/known/timestamppb" ) type Message map[string]interface{} @@ -362,7 +361,7 @@ func (m *matcher) Response(message map[string]interface{}) *matcher { } else { res = prev(r, md) } - res.Messages = append(res.Messages, castMessage(message)) + res.Messages = append(res.Messages, message) return res } return m @@ -891,44 +890,6 @@ func contains(s []string, e string) bool { return false } -func castMessage(message map[string]interface{}) map[string]interface{} { - casted := map[string]interface{}{} - for k, v := range message { - casted[k] = cast(v) - } - return casted -} - -func cast(in interface{}) interface{} { - switch v := in.(type) { - case time.Time: - return timestamppb.New(v) - case string: - t, err := time.Parse(time.RFC3339Nano, v) - if err != nil { - t, err = time.Parse(time.RFC3339, v) - if err != nil { - return v - } - } - return timestamppb.New(t) - case []interface{}: - casted := []interface{}{} - for _, vv := range v { - casted = append(casted, cast(vv)) - } - return casted - case map[string]interface{}: - casted := map[string]interface{}{} - for k, vv := range v { - casted[k] = cast(vv) - } - return casted - default: - return v - } -} - // copy from google.golang.org/protobuf/reflect/protoregistry func rangeTopLevelDescriptors(fd protoreflect.FileDescriptor, f func(protoreflect.Descriptor)) { eds := fd.Enums() diff --git a/grpcstub_test.go b/grpcstub_test.go index d71cf35..570d51d 100644 --- a/grpcstub_test.go +++ b/grpcstub_test.go @@ -20,6 +20,7 @@ import ( "google.golang.org/grpc/metadata" rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/timestamppb" ) func TestUnary(t *testing.T) { @@ -683,16 +684,6 @@ func TestTime(t *testing.T) { res map[string]interface{} wantTime time.Time }{ - { - "Cast time.Time to timestamppb.Timestamp", - map[string]interface{}{ - "message": "hello", - "num": 3, - "hello": []string{"hello", "world"}, - "create_time": now, - }, - now, - }, { "empty is 0 of UNIX timestamp", map[string]interface{}{ @@ -703,22 +694,12 @@ func TestTime(t *testing.T) { time.Unix(0, 0), }, { - "Cast RFC3339 string to timestamppb.Timestamp", - map[string]interface{}{ - "message": "hello", - "num": 3, - "hello": []string{"hello", "world"}, - "create_time": now.Format(time.RFC3339), - }, - now, - }, - { - "Cast RFC3339Nano string to timestamppb.Timestamp", + "timestamppb.Timestamp", map[string]interface{}{ "message": "hello", "num": 3, "hello": []string{"hello", "world"}, - "create_time": now.Format(time.RFC3339Nano), + "create_time": timestamppb.New(now), }, now, },