mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-12 02:28:05 +02:00
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>
This commit is contained in:
parent
e273c5188a
commit
446774f9e5
@ -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])
|
||||
}
|
||||
|
@ -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…
Reference in New Issue
Block a user