Skip to content

Commit

Permalink
Merge 4b8e838 into f47a238
Browse files Browse the repository at this point in the history
  • Loading branch information
songzhibin97 authored Mar 23, 2023
2 parents f47a238 + 4b8e838 commit 45d7a7d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/kratos/internal/proto/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
}
Expand Down
40 changes: 40 additions & 0 deletions cmd/kratos/internal/proto/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 45d7a7d

Please sign in to comment.