-
Notifications
You must be signed in to change notification settings - Fork 931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support grpc json protocol #582
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb259e9
feat: support json protocol with grpc
xujianhai666 7296533
merge apache dubbo-go master
9390b0f
fix dubbo grpc json support
9dc28f0
fix dubbo plugin and feat gprc json support
e00853c
add public method 、struc comment
45a983e
alter client config by defer delay check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package grpc | ||
relaxedCat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
import ( | ||
"github.com/golang/protobuf/jsonpb" | ||
"github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
const ( | ||
codecJson = "json" | ||
codecProto = "proto" | ||
) | ||
|
||
func init() { | ||
encoding.RegisterCodec(grpcJson{ | ||
Marshaler: jsonpb.Marshaler{ | ||
EmitDefaults: true, | ||
OrigName: true, | ||
}, | ||
}) | ||
} | ||
|
||
type grpcJson struct { | ||
jsonpb.Marshaler | ||
jsonpb.Unmarshaler | ||
} | ||
|
||
// Name implements grpc encoding package Codec interface method, | ||
// returns the name of the Codec implementation. | ||
func (_ grpcJson) Name() string { | ||
return codecJson | ||
} | ||
|
||
// Marshal implements grpc encoding package Codec interface method,returns the wire format of v. | ||
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) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b.Bytes(), nil | ||
} | ||
return json.Marshal(v) | ||
} | ||
|
||
// Unmarshal implements grpc encoding package Codec interface method,Unmarshal parses the wire format into v. | ||
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) | ||
} | ||
return json.Unmarshal(data, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package grpc | ||
|
||
import ( | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
type ( | ||
// ServerConfig currently is empty struct,for future expansion | ||
ServerConfig struct { | ||
} | ||
|
||
// ClientConfig wrap client call parameters | ||
ClientConfig struct { | ||
// content type, more information refer by https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests | ||
ContentSubType string `default:"proto" yaml:"content_sub_type" json:"content_sub_type,omitempty"` | ||
} | ||
) | ||
|
||
// GetDefaultClientConfig return grpc client default call options | ||
func GetDefaultClientConfig() ClientConfig { | ||
fangyincheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ClientConfig{ | ||
ContentSubType: codecProto, | ||
} | ||
} | ||
|
||
// GetDefaultServerConfig currently return empty struct,for future expansion | ||
func GetDefaultServerConfig() ServerConfig { | ||
return ServerConfig{} | ||
} | ||
|
||
// GetClientConfig return grpc client custom call options | ||
func GetClientConfig() ClientConfig { | ||
return ClientConfig{} | ||
} | ||
|
||
// Validate check if custom config encoding is supported in dubbo grpc | ||
func (c *ClientConfig) Validate() error { | ||
if c.ContentSubType != codecJson && c.ContentSubType != codecProto { | ||
return perrors.Errorf(" dubbo-go grpc codec currently only support proto、json, %s isn't supported,"+ | ||
" please check protocol content_sub_type config", c.ContentSubType) | ||
} | ||
return nil | ||
} | ||
|
||
// Validate currently return empty struct,for future expansion | ||
func (c *ServerConfig) Validate() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the above two line codes is very strange. why now define a
NewDefaultClientConfig() *ClientConfig
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,use defer delay check instead