diff --git a/tools/goctl/rpc/generator/gencall.go b/tools/goctl/rpc/generator/gencall.go index e192b78f..c145b829 100644 --- a/tools/goctl/rpc/generator/gencall.go +++ b/tools/goctl/rpc/generator/gencall.go @@ -5,6 +5,7 @@ import ( "path/filepath" "strings" + "github.com/emicklei/proto" "github.com/tal-tech/go-zero/core/collection" conf "github.com/tal-tech/go-zero/tools/goctl/config" "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() 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{}{ @@ -106,6 +108,26 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf 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) { functions := make([]string, 0) for _, rpc := range service.RPC { diff --git a/tools/goctl/rpc/parser/parser_test.go b/tools/goctl/rpc/parser/parser_test.go index 2674a8d4..7fe8bdec 100644 --- a/tools/goctl/rpc/parser/parser_test.go +++ b/tools/goctl/rpc/parser/parser_test.go @@ -19,7 +19,7 @@ func TestDefaultProtoParse(t *testing.T) { assert.Equal(t, "test", data.Package.Name) assert.Equal(t, true, data.GoPackage == "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 for _, item := range data.Message { list = append(list, item.Name) diff --git a/tools/goctl/rpc/parser/test.proto b/tools/goctl/rpc/parser/test.proto index 45643864..1a04ff18 100644 --- a/tools/goctl/rpc/parser/test.proto +++ b/tools/goctl/rpc/parser/test.proto @@ -8,6 +8,9 @@ import "base.proto"; message TestMessage{} message TestReq{} message TestReply{} +message Inline { + message Inner{} +} enum TestEnum { unknown = 0;