From 7be14b87a44e79e55fbfeb0eb8755f0611df9bda Mon Sep 17 00:00:00 2001 From: seeflood Date: Wed, 28 Sep 2022 14:52:50 +0800 Subject: [PATCH] feat: use copier to convert structs (#803) --- go.mod | 1 + go.sum | 2 -- pkg/grpc/delay_queue/server.go | 27 ++++++------------ pkg/grpc/email/server.go | 50 ++++++++++++---------------------- pkg/grpc/phone/server.go | 26 ++++++------------ 5 files changed, 35 insertions(+), 71 deletions(-) diff --git a/go.mod b/go.mod index 710eac4c80..8fc5c7a130 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/google/uuid v1.3.0 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/jinzhu/copier v0.3.6-0.20220506024824-3e39b055319a github.com/json-iterator/go v1.1.12 github.com/opentracing/opentracing-go v1.2.0 github.com/openzipkin/zipkin-go v0.2.2 diff --git a/go.sum b/go.sum index d8f260e107..c836e7103a 100644 --- a/go.sum +++ b/go.sum @@ -358,8 +358,6 @@ github.com/dancannon/gorethink v4.0.0+incompatible/go.mod h1:BLvkat9KmZc1efyYwhz github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U= github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0= github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0= -github.com/dapr/components-contrib v1.5.1-rc.1 h1:t7Y8jxGcNnYXYglBWuvT/A6QszfpVIICi+AGvi0aaaY= -github.com/dapr/components-contrib v1.5.1-rc.1/go.mod h1:k40RvOMnDmJMSSbWZ10ajjWJ9pEuq4Z5eKxCa/yrAe8= github.com/dapr/components-contrib v1.5.2 h1:zMP0pvWjpu4/AVTQzlWnixhlJUg9S1op8QIuq4DMSus= github.com/dapr/components-contrib v1.5.2/go.mod h1:k40RvOMnDmJMSSbWZ10ajjWJ9pEuq4Z5eKxCa/yrAe8= github.com/dapr/kit v0.0.2-0.20210614175626-b9074b64d233 h1:M0dWIG8kUxEFU57IqTWeqptNqlBsfosFgsA5Ov7rJ8g= diff --git a/pkg/grpc/delay_queue/server.go b/pkg/grpc/delay_queue/server.go index f21a3242e2..f85f1b9c6c 100644 --- a/pkg/grpc/delay_queue/server.go +++ b/pkg/grpc/delay_queue/server.go @@ -17,9 +17,10 @@ package delay_queue import ( "context" - "encoding/json" "fmt" + "github.com/jinzhu/copier" + "mosn.io/pkg/log" delay_queue "mosn.io/layotto/components/delay_queue" @@ -62,33 +63,21 @@ func (s *server) PublishDelayMessage(ctx context.Context, in *delay_queue1.Delay } // convert request - var req delay_queue.DelayMessageRequest - bytes, err := json.Marshal(in) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the request: %s", err.Error()) - } - err = json.Unmarshal(bytes, &req) + req := &delay_queue.DelayMessageRequest{} + err := copier.CopyWithOption(req, in, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the request: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the request: %s", err.Error()) } // delegate to the component - resp, err := comp.PublishDelayMessage(ctx, &req) + resp, err := comp.PublishDelayMessage(ctx, req) if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } // convert response - var out delay_queue1.DelayMessageResponse - bytes, err = json.Marshal(resp) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the response: %s", err.Error()) - } - err = json.Unmarshal(bytes, &out) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the response: %s", err.Error()) - } - return &out, nil + out := &delay_queue1.DelayMessageResponse{MessageId: resp.MessageId} + return out, nil } func invalidArgumentError(method string, format string, a ...interface{}) error { diff --git a/pkg/grpc/email/server.go b/pkg/grpc/email/server.go index 48beec5b89..595f4a88e2 100644 --- a/pkg/grpc/email/server.go +++ b/pkg/grpc/email/server.go @@ -17,9 +17,9 @@ package email import ( "context" - "encoding/json" "fmt" + "github.com/jinzhu/copier" "mosn.io/pkg/log" email "mosn.io/layotto/components/email" @@ -53,33 +53,25 @@ func (s *server) SendEmail(ctx context.Context, in *email1.SendEmailRequest) (*e } // convert request - var req email.SendEmailRequest - bytes, err := json.Marshal(in) + req := &email.SendEmailRequest{} + err := copier.CopyWithOption(req, in, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the request: %s", err.Error()) - } - err = json.Unmarshal(bytes, &req) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the request: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the request: %s", err.Error()) } // delegate to the component - resp, err := comp.SendEmail(ctx, &req) + resp, err := comp.SendEmail(ctx, req) if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } // convert response - var out email1.SendEmailResponse - bytes, err = json.Marshal(resp) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the response: %s", err.Error()) - } - err = json.Unmarshal(bytes, &out) + out := &email1.SendEmailResponse{} + err = copier.CopyWithOption(out, resp, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the response: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the response: %s", err.Error()) } - return &out, nil + return out, nil } func (s *server) SendEmailWithTemplate(ctx context.Context, in *email1.SendEmailWithTemplateRequest) (*email1.SendEmailWithTemplateResponse, error) { @@ -90,33 +82,25 @@ func (s *server) SendEmailWithTemplate(ctx context.Context, in *email1.SendEmail } // convert request - var req email.SendEmailWithTemplateRequest - bytes, err := json.Marshal(in) + req := &email.SendEmailWithTemplateRequest{} + err := copier.CopyWithOption(req, in, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the request: %s", err.Error()) - } - err = json.Unmarshal(bytes, &req) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the request: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the request: %s", err.Error()) } // delegate to the component - resp, err := comp.SendEmailWithTemplate(ctx, &req) + resp, err := comp.SendEmailWithTemplate(ctx, req) if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } // convert response - var out email1.SendEmailWithTemplateResponse - bytes, err = json.Marshal(resp) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the response: %s", err.Error()) - } - err = json.Unmarshal(bytes, &out) + out := &email1.SendEmailWithTemplateResponse{} + err = copier.CopyWithOption(out, resp, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the response: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the response: %s", err.Error()) } - return &out, nil + return out, nil } func invalidArgumentError(method string, format string, a ...interface{}) error { diff --git a/pkg/grpc/phone/server.go b/pkg/grpc/phone/server.go index e37633ec62..41ae1e98a5 100644 --- a/pkg/grpc/phone/server.go +++ b/pkg/grpc/phone/server.go @@ -17,9 +17,9 @@ package phone import ( "context" - "encoding/json" "fmt" + "github.com/jinzhu/copier" "mosn.io/pkg/log" phone "mosn.io/layotto/components/phone" @@ -53,33 +53,25 @@ func (s *server) SendVoiceWithTemplate(ctx context.Context, in *phone1.SendVoice } // convert request - var req phone.SendVoiceWithTemplateRequest - bytes, err := json.Marshal(in) + req := &phone.SendVoiceWithTemplateRequest{} + err := copier.CopyWithOption(req, in, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the request: %s", err.Error()) - } - err = json.Unmarshal(bytes, &req) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the request: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the request: %s", err.Error()) } // delegate to the component - resp, err := comp.SendVoiceWithTemplate(ctx, &req) + resp, err := comp.SendVoiceWithTemplate(ctx, req) if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } // convert response - var out phone1.SendVoiceWithTemplateResponse - bytes, err = json.Marshal(resp) - if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Marshal the response: %s", err.Error()) - } - err = json.Unmarshal(bytes, &out) + out := &phone1.SendVoiceWithTemplateResponse{} + err = copier.CopyWithOption(out, resp, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{}}) if err != nil { - return nil, status.Errorf(codes.Internal, "Error when json.Unmarshal the response: %s", err.Error()) + return nil, status.Errorf(codes.Internal, "Error when converting the response: %s", err.Error()) } - return &out, nil + return out, nil } func invalidArgumentError(method string, format string, a ...interface{}) error {