anqiansong 3 years ago committed by GitHub
parent c7f5aad83a
commit 14ec29991c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/emicklei/proto"
"github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/core/collection"
conf "github.com/tal-tech/go-zero/tools/goctl/config" conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
@ -90,7 +91,8 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
alias := collection.NewSet() alias := collection.NewSet()
for _, item := range proto.Message { for _, item := range proto.Message {
alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(item.Name), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(item.Name)))) msgName := getMessageName(*item.Message)
alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(msgName), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(msgName))))
} }
err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{ err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
@ -106,6 +108,26 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
return err return err
} }
func getMessageName(msg proto.Message) string {
var list = []string{msg.Name}
for {
parent := msg.Parent
if parent == nil {
break
}
parentMsg, ok := parent.(*proto.Message)
if !ok {
break
}
tmp := []string{parentMsg.Name}
list = append(tmp, list...)
msg = *parentMsg
}
return strings.Join(list, "_")
}
func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) { func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
functions := make([]string, 0) functions := make([]string, 0)
for _, rpc := range service.RPC { for _, rpc := range service.RPC {

@ -19,7 +19,7 @@ func TestDefaultProtoParse(t *testing.T) {
assert.Equal(t, "test", data.Package.Name) assert.Equal(t, "test", data.Package.Name)
assert.Equal(t, true, data.GoPackage == "go") assert.Equal(t, true, data.GoPackage == "go")
assert.Equal(t, true, data.PbPackage == "_go") assert.Equal(t, true, data.PbPackage == "_go")
assert.Equal(t, []string{"TestMessage", "TestReply", "TestReq"}, func() []string { assert.Equal(t, []string{"Inline", "Inner", "TestMessage", "TestReply", "TestReq"}, func() []string {
var list []string var list []string
for _, item := range data.Message { for _, item := range data.Message {
list = append(list, item.Name) list = append(list, item.Name)

@ -8,6 +8,9 @@ import "base.proto";
message TestMessage{} message TestMessage{}
message TestReq{} message TestReq{}
message TestReply{} message TestReply{}
message Inline {
message Inner{}
}
enum TestEnum { enum TestEnum {
unknown = 0; unknown = 0;

Loading…
Cancel
Save