From 9dc28f08b82bc289681b1a87b4499360dd7a1b99 Mon Sep 17 00:00:00 2001
From: xuxiaoliang <xuxiaoliang03411@Hellobike.com>
Date: Sat, 6 Jun 2020 01:10:56 +0800
Subject: [PATCH] fix dubbo plugin and feat gprc json support

---
 protocol/grpc/client.go                       | 12 ++++++------
 protocol/grpc/codec.go                        | 19 +++++++++++++------
 protocol/grpc/config.go                       | 17 +++++++++--------
 .../protoc-gen-dubbo/plugin/dubbo/dubbo.go    |  5 ++---
 4 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/protocol/grpc/client.go b/protocol/grpc/client.go
index fd45f1fb1..0a98749aa 100644
--- a/protocol/grpc/client.go
+++ b/protocol/grpc/client.go
@@ -42,12 +42,14 @@ var (
 func init() {
 	// load clientconfig from consumer_config
 	// default use grpc
+	defaultClientConfig := GetDefaultClientConfig()
+	clientConf = &defaultClientConfig
 	consumerConfig := config.GetConsumerConfig()
+
 	if consumerConfig.ApplicationConfig == nil {
 		return
 	}
 	protocolConf := config.GetConsumerConfig().ProtocolConf
-	customClientConfig := GetCustomClientConfig()
 
 	if protocolConf == nil {
 		logger.Info("protocol_conf default use dubbo config")
@@ -61,15 +63,13 @@ func init() {
 		if err != nil {
 			panic(err)
 		}
-		err = yaml.Unmarshal(grpcConfByte, &customClientConfig)
+		err = yaml.Unmarshal(grpcConfByte, clientConf)
 		if err != nil {
 			panic(err)
 		}
 	}
 
-	clientConf = &customClientConfig
-	if clientConf == nil || len(clientConf.ContentType) == 0 {
-		defaultClientConfig := GetDefaultClientConfig()
+	if clientConf == nil || len(clientConf.ContentSubType) == 0 {
 		clientConf = &defaultClientConfig
 	}
 	if err := clientConf.Validate(); err != nil {
@@ -90,7 +90,7 @@ func NewClient(url common.URL) *Client {
 	dailOpts := make([]grpc.DialOption, 0, 4)
 	dailOpts = append(dailOpts, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithUnaryInterceptor(
 		otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads())),
-		grpc.WithDefaultCallOptions(grpc.CallContentSubtype(clientConf.ContentType)))
+		grpc.WithDefaultCallOptions(grpc.CallContentSubtype(clientConf.ContentSubType)))
 	conn, err := grpc.Dial(url.Location, dailOpts...)
 	if err != nil {
 		panic(err)
diff --git a/protocol/grpc/codec.go b/protocol/grpc/codec.go
index f417926a3..1f34674d6 100644
--- a/protocol/grpc/codec.go
+++ b/protocol/grpc/codec.go
@@ -14,6 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package grpc
 
 import (
@@ -28,12 +29,14 @@ import (
 )
 
 const (
-	CODEC_JSON  = "json"
+	// json
+	CODEC_JSON = "json"
+	// proto
 	CODEC_PROTO = "proto"
 )
 
 func init() {
-	encoding.RegisterCodec(JSON{
+	encoding.RegisterCodec(grpcJson{
 		Marshaler: jsonpb.Marshaler{
 			EmitDefaults: true,
 			OrigName:     true,
@@ -41,16 +44,19 @@ func init() {
 	})
 }
 
-type JSON struct {
+// grpcJson ...
+type grpcJson struct {
 	jsonpb.Marshaler
 	jsonpb.Unmarshaler
 }
 
-func (_ JSON) Name() string {
+// implements grpc encoding package Codec interface method
+func (_ grpcJson) Name() string {
 	return CODEC_JSON
 }
 
-func (j JSON) Marshal(v interface{}) (out []byte, err error) {
+// implements grpc encoding package Codec interface method
+func (j grpcJson) Marshal(v interface{}) (out []byte, err error) {
 	if pm, ok := v.(proto.Message); ok {
 		b := new(bytes.Buffer)
 		err := j.Marshaler.Marshal(b, pm)
@@ -62,7 +68,8 @@ func (j JSON) Marshal(v interface{}) (out []byte, err error) {
 	return json.Marshal(v)
 }
 
-func (j JSON) Unmarshal(data []byte, v interface{}) (err error) {
+// implements grpc encoding package Codec interface method
+func (j grpcJson) Unmarshal(data []byte, v interface{}) (err error) {
 	if pm, ok := v.(proto.Message); ok {
 		b := bytes.NewBuffer(data)
 		return j.Unmarshaler.Unmarshal(b, pm)
diff --git a/protocol/grpc/config.go b/protocol/grpc/config.go
index db29634ea..529b6068c 100644
--- a/protocol/grpc/config.go
+++ b/protocol/grpc/config.go
@@ -21,8 +21,6 @@ import (
 	perrors "github.com/pkg/errors"
 )
 
-
-
 type (
 	// ServerConfig
 	ServerConfig struct {
@@ -31,14 +29,14 @@ type (
 	// ClientConfig
 	ClientConfig struct {
 		// content type, more information refer by https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
-		ContentType string `default:"proto" yaml:"content_type" json:"content_type,omitempty"`
+		ContentSubType string `default:"proto" yaml:"content_sub_type" json:"content_sub_type,omitempty"`
 	}
 )
 
 // GetDefaultClientConfig ...
 func GetDefaultClientConfig() ClientConfig {
 	return ClientConfig{
-		ContentType: "proto",
+		ContentSubType: CODEC_PROTO,
 	}
 }
 
@@ -47,18 +45,21 @@ func GetDefaultServerConfig() ServerConfig {
 	return ServerConfig{}
 }
 
-func GetCustomClientConfig() ClientConfig {
+// GetClientConfig ...
+func GetClientConfig() ClientConfig {
 	return ClientConfig{}
 }
 
+// clientConfig Validate ...
 func (c *ClientConfig) Validate() error {
-	if c.ContentType != CODEC_JSON && c.ContentType != CODEC_PROTO {
-		return perrors.Errorf(" dubbo-go grpc codec currently only support protobuf銆乯son, %s isn't supported,"+
-			" please check protocol content_type config", c.ContentType)
+	if c.ContentSubType != CODEC_JSON && c.ContentSubType != CODEC_PROTO {
+		return perrors.Errorf(" dubbo-go grpc codec currently only support proto銆乯son, %s isn't supported,"+
+			" please check protocol content_sub_type config", c.ContentSubType)
 	}
 	return nil
 }
 
+// severConfig Validate ...
 func (c *ServerConfig) Validate() error {
 	return nil
 }
diff --git a/protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go b/protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go
index 83d28519f..1af4fafdc 100644
--- a/protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go
+++ b/protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go
@@ -107,7 +107,6 @@ func (g *dubboGrpc) GenerateImports(file *generator.FileDescriptor) {
 	g.P(`dgrpc "github.com/apache/dubbo-go/protocol/grpc"`)
 	g.P(`"github.com/apache/dubbo-go/protocol/invocation"`)
 	g.P(`"github.com/apache/dubbo-go/protocol"`)
-	g.P(`"github.com/apache/dubbo-go/config"`)
 	g.P(` ) `)
 }
 
@@ -266,7 +265,7 @@ func (g *dubboGrpc) generateServerMethod(servName, fullServName string, method *
 		g.P(`invo := invocation.NewRPCInvocation("`, methName, `", args, nil)`)
 
 		g.P("if interceptor == nil {")
-		g.P("result := base.GetProxyImpl().Invoke(invo)")
+		g.P("result := base.GetProxyImpl().Invoke(context.Background(), invo)")
 		g.P("return result.Result(), result.Error()")
 		g.P("}")
 
@@ -276,7 +275,7 @@ func (g *dubboGrpc) generateServerMethod(servName, fullServName string, method *
 		g.P("}")
 
 		g.P("handler := func(ctx ", contextPkg, ".Context, req interface{}) (interface{}, error) {")
-		g.P("result := base.GetProxyImpl().Invoke(invo)")
+		g.P("result := base.GetProxyImpl().Invoke(context.Background(), invo)")
 		g.P("return result.Result(), result.Error()")
 		g.P("}")
 
-- 
GitLab