diff --git a/cmd/kratos/internal/proto/server/server.go b/cmd/kratos/internal/proto/server/server.go index 35e8f90c3..8a30d93c4 100644 --- a/cmd/kratos/internal/proto/server/server.go +++ b/cmd/kratos/internal/proto/server/server.go @@ -16,8 +16,8 @@ import ( // CmdServer the service command. var CmdServer = &cobra.Command{ Use: "server", - Short: "Generate the proto Server implementations", - Long: "Generate the proto Server implementations. Example: kratos proto server api/xxx.proto --target-dir=internal/service", + Short: "Generate the proto server implementations", + Long: "Generate the proto server implementations. Example: kratos proto server api/xxx.proto --target-dir=internal/service", Run: run, } var targetDir string @@ -64,8 +64,8 @@ func run(_ *cobra.Command, args []string) { continue } cs.Methods = append(cs.Methods, &Method{ - Service: serviceName(s.Name), Name: serviceName(r.Name), Request: r.RequestType, - Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns), + Service: serviceName(s.Name), Name: serviceName(r.Name), Request: parametersName(r.RequestType), + Reply: parametersName(r.ReturnsType), Type: getMethodType(r.StreamsRequest, r.StreamsReturns), }) } res = append(res, cs) @@ -105,6 +105,10 @@ func getMethodType(streamsRequest, streamsReturns bool) MethodType { return unaryType } +func parametersName(name string) string { + return strings.ReplaceAll(name, ".", "_") +} + func serviceName(name string) string { return toUpperCamelCase(strings.Split(name, ".")[0]) } diff --git a/cmd/kratos/internal/proto/server/server_test.go b/cmd/kratos/internal/proto/server/server_test.go index fa90f692e..891f8bbbb 100644 --- a/cmd/kratos/internal/proto/server/server_test.go +++ b/cmd/kratos/internal/proto/server/server_test.go @@ -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) + } + }) + } +}