From b7422717cf91b7de89bd0001373af08d8f63f13f Mon Sep 17 00:00:00 2001 From: hshe Date: Wed, 27 Jul 2022 10:51:19 +0800 Subject: [PATCH] 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 --- cmd/kratos/internal/proto/server/server.go | 18 +++++++---- .../internal/proto/server/server_test.go | 32 ++++++++++++------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cmd/kratos/internal/proto/server/server.go b/cmd/kratos/internal/proto/server/server.go index dcc19790d..3b8a81966 100644 --- a/cmd/kratos/internal/proto/server/server.go +++ b/cmd/kratos/internal/proto/server/server.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, " ", "") } diff --git a/cmd/kratos/internal/proto/server/server_test.go b/cmd/kratos/internal/proto/server/server_test.go index a2cbeee61..fa90f692e 100644 --- a/cmd/kratos/internal/proto/server/server_test.go +++ b/cmd/kratos/internal/proto/server/server_test.go @@ -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) } }) }