fix:proto nesting causes template generation problems (#2718)

* fix:proto nesting causes template generation problems

* feat: add test

* fix: cmd introduction word change

---------

Co-authored-by: haiyux <haiyux@foxmail.com>
pull/2612/head
Bin 2 years ago committed by GitHub
parent e273c5188a
commit 446774f9e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      cmd/kratos/internal/proto/server/server.go
  2. 40
      cmd/kratos/internal/proto/server/server_test.go

@ -16,8 +16,8 @@ import (
// CmdServer the service command. // CmdServer the service command.
var CmdServer = &cobra.Command{ var CmdServer = &cobra.Command{
Use: "server", Use: "server",
Short: "Generate the proto Server implementations", Short: "Generate the proto server implementations",
Long: "Generate the proto Server implementations. Example: kratos proto server api/xxx.proto --target-dir=internal/service", Long: "Generate the proto server implementations. Example: kratos proto server api/xxx.proto --target-dir=internal/service",
Run: run, Run: run,
} }
var targetDir string var targetDir string
@ -64,8 +64,8 @@ func run(_ *cobra.Command, args []string) {
continue continue
} }
cs.Methods = append(cs.Methods, &Method{ cs.Methods = append(cs.Methods, &Method{
Service: serviceName(s.Name), Name: serviceName(r.Name), Request: r.RequestType, Service: serviceName(s.Name), Name: serviceName(r.Name), Request: parametersName(r.RequestType),
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns), Reply: parametersName(r.ReturnsType), Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
}) })
} }
res = append(res, cs) res = append(res, cs)
@ -105,6 +105,10 @@ func getMethodType(streamsRequest, streamsReturns bool) MethodType {
return unaryType return unaryType
} }
func parametersName(name string) string {
return strings.ReplaceAll(name, ".", "_")
}
func serviceName(name string) string { func serviceName(name string) string {
return toUpperCamelCase(strings.Split(name, ".")[0]) return toUpperCamelCase(strings.Split(name, ".")[0])
} }

@ -60,3 +60,43 @@ func Test_serviceName(t *testing.T) {
}) })
} }
} }
func Test_parametersName(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "parametersName on not nested",
args: args{
name: "MessageResponse",
},
want: "MessageResponse",
},
{
name: "parametersName on One layer of nesting",
args: args{
name: "Message.Response",
},
want: "Message_Response",
},
{
name: "parametersName on Two layer of nesting",
args: args{
name: "Message.Message2.Response",
},
want: "Message_Message2_Response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parametersName(tt.args.name); got != tt.want {
t.Errorf("parametersName() = %v, want %v", got, tt.want)
}
})
}
}

Loading…
Cancel
Save