proto build client structName not match to server's structName (#2200)

* fix(proto/server): server .ServerName not Match to Client ServerName

* fix(proto/server): unit server_test
pull/2217/head
hshe 2 years ago committed by GitHub
parent 59b758ceda
commit b7422717cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      cmd/kratos/internal/proto/server/server.go
  2. 32
      cmd/kratos/internal/proto/server/server_test.go

@ -9,6 +9,8 @@ import (
"github.com/emicklei/proto"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// CmdServer the service command.
@ -54,7 +56,7 @@ func run(cmd *cobra.Command, args []string) {
proto.WithService(func(s *proto.Service) {
cs := &Service{
Package: pkg,
Service: s.Name,
Service: serviceName(s.Name),
}
for _, e := range s.Elements {
r, ok := e.(*proto.RPC)
@ -62,7 +64,7 @@ func run(cmd *cobra.Command, args []string) {
continue
}
cs.Methods = append(cs.Methods, &Method{
Service: s.Name, Name: ucFirst(r.Name), Request: r.RequestType,
Service: serviceName(s.Name), Name: serviceName(r.Name), Request: r.RequestType,
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
})
}
@ -103,10 +105,12 @@ func getMethodType(streamsRequest, streamsReturns bool) MethodType {
return unaryType
}
func ucFirst(str string) string {
if str == "" {
return ""
}
func serviceName(name string) string {
return toUpperCamelCase(strings.Split(name, ".")[0])
}
return strings.ToUpper(str[:1]) + str[1:]
func toUpperCamelCase(s string) string {
s = strings.ReplaceAll(s, "_", " ")
s = cases.Title(language.Und, cases.NoLower).String(s)
return strings.ReplaceAll(s, " ", "")
}

@ -2,7 +2,7 @@ package server
import "testing"
func Test_ucFirst(t *testing.T) {
func Test_serviceName(t *testing.T) {
type args struct {
str string
}
@ -12,40 +12,50 @@ func Test_ucFirst(t *testing.T) {
want string
}{
{
name: "ucFirst on lowercase words",
name: "serviceName on lowercase words",
args: args{str: "helloworld"},
want: "Helloworld",
},
{
name: "ucFirst on uppercase words",
name: "serviceName on uppercase words",
args: args{str: "HELLOWORLD"},
want: "HELLOWORLD",
},
{
name: "ucFirst on lowercase words with spaces",
name: "serviceName on lowercase words with spaces",
args: args{str: "hello world"},
want: "Hello world",
want: "HelloWorld",
},
{
name: "ucFirst on uppercase words with spaces",
name: "serviceName on uppercase words with spaces",
args: args{str: "HELLO WORLD"},
want: "HELLO WORLD",
want: "HELLOWORLD",
},
{
name: "ucFirst on Lower Camel Case words",
name: "serviceName on Lower Camel Case words",
args: args{str: "helloWorld"},
want: "HelloWorld",
},
{
name: "ucFirst on Upper Camel Case words",
name: "serviceName on Lower Camel Case words",
args: args{str: "helloWorld"},
want: "HelloWorld",
},
{
name: "serviceName on Upper Camel Case words",
args: args{str: "HelloWorld"},
want: "HelloWorld",
},
{
name: "serviceName on Upper Camel Case words",
args: args{str: "hello_world"},
want: "HelloWorld",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ucFirst(tt.args.str); got != tt.want {
t.Errorf("ucFirst() = %v, want %v", got, tt.want)
if got := serviceName(tt.args.str); got != tt.want {
t.Errorf("serviceName() = %v, want %v", got, tt.want)
}
})
}

Loading…
Cancel
Save