fix: kratos proto server cmd method name bugfix (#2089) (#2094)

pull/2102/head
raw34 3 years ago committed by GitHub
parent 4b7afe52af
commit 228ceaae9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      cmd/kratos/internal/proto/server/server.go
  2. 52
      cmd/kratos/internal/proto/server/server_test.go

@ -7,15 +7,10 @@ import (
"path"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/emicklei/proto"
"github.com/spf13/cobra"
)
var caser = cases.Title(language.English)
// CmdServer the service command.
var CmdServer = &cobra.Command{
Use: "server",
@ -67,7 +62,7 @@ func run(cmd *cobra.Command, args []string) {
continue
}
cs.Methods = append(cs.Methods, &Method{
Service: s.Name, Name: caser.String(r.Name), Request: r.RequestType,
Service: s.Name, Name: ucFirst(r.Name), Request: r.RequestType,
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
})
}
@ -107,3 +102,11 @@ func getMethodType(streamsRequest, streamsReturns bool) MethodType {
}
return unaryType
}
func ucFirst(str string) string {
if str == "" {
return ""
}
return strings.ToUpper(str[:1]) + str[1:]
}

@ -0,0 +1,52 @@
package server
import "testing"
func Test_ucFirst(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
}{
{
name: "ucFirst on lowercase words",
args: args{str: "helloworld"},
want: "Helloworld",
},
{
name: "ucFirst on uppercase words",
args: args{str: "HELLOWORLD"},
want: "HELLOWORLD",
},
{
name: "ucFirst on lowercase words with spaces",
args: args{str: "hello world"},
want: "Hello world",
},
{
name: "ucFirst on uppercase words with spaces",
args: args{str: "HELLO WORLD"},
want: "HELLO WORLD",
},
{
name: "ucFirst on Lower Camel Case words",
args: args{str: "helloWorld"},
want: "HelloWorld",
},
{
name: "ucFirst on Upper Camel Case words",
args: args{str: "HelloWorld"},
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)
}
})
}
}
Loading…
Cancel
Save