diff --git a/main.go b/main.go index 310f42b..3f3ac56 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "errors" "fmt" "go/format" @@ -9,6 +10,7 @@ import ( "path/filepath" "regexp" "strings" + "unicode" "git.apinb.com/bsm-tools/protoc-gen-slc/tpl" @@ -114,7 +116,7 @@ func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *prot //create servers. code := tpl.Server imports := []string{ - "\"" + moduleName + "/internal/logic/" + strings.ToLower(service.GoName) + "\"", + "\"" + moduleName + "/internal/logic/" + toSnakeCase(service.GoName) + "\"", "pb \"" + moduleName + "/pb\"", } @@ -147,7 +149,7 @@ func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *prot } func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error { - logicPath := "./internal/logic/" + strings.ToLower(service.GoName) + logicPath := "./internal/logic/" + toSnakeCase(service.GoName) if !utils.PathExists(logicPath) { os.MkdirAll(logicPath, os.ModePerm) } @@ -285,3 +287,20 @@ func parseOptions(comment string) map[string]string { return result } + +// CamelToSnake 将驼峰命名转换为下划线命名 +func CamelToSnake(s string) string { + var buf bytes.Buffer + for i, r := range s { + if unicode.IsUpper(r) { + // 如果不是第一个字符,添加下划线 + if i > 0 { + buf.WriteRune('_') + } + buf.WriteRune(unicode.ToLower(r)) + } else { + buf.WriteRune(r) + } + } + return buf.String() +}