fix:fix proto add underscore problem (#2101)

* fix:fix proto add underscore problem
pull/2105/head
haiyux 2 years ago committed by GitHub
parent 228ceaae9d
commit 752f011ba1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      cmd/kratos/internal/proto/add/add.go
  2. 38
      cmd/kratos/internal/proto/add/add_test.go

@ -7,6 +7,8 @@ import (
"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// CmdAdd represents the add command.
@ -63,7 +65,11 @@ func javaPackage(name string) string {
}
func serviceName(name string) string {
return export(strings.Split(name, ".")[0])
return toUpperCamelCase(strings.Split(name, ".")[0])
}
func export(s string) string { return strings.ToUpper(s[:1]) + s[1:] }
func toUpperCamelCase(s string) string {
s = strings.ReplaceAll(s, "_", " ")
s = cases.Title(language.Und, cases.NoLower).String(s)
return strings.ReplaceAll(s, " ", "")
}

@ -0,0 +1,38 @@
package add
import "testing"
func TestUnderscoreToUpperCamelCase(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "hello_world",
want: "HelloWorld",
},
{
name: "v2_kratos_dev",
want: "V2KratosDev",
},
{
name: "www_Google_com",
want: "WwwGoogleCom",
},
{
name: "wwwBaidu_com",
want: "WwwBaiduCom",
},
{
name: "HelloWorld",
want: "HelloWorld",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := toUpperCamelCase(tt.name); got != tt.want {
t.Errorf("toUpperCamelCase() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save