Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
|
9943ed0e6e | |
|
fa9ceaddc6 | |
|
87734c972f | |
|
3b283780ab |
25
main.go
25
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"git.apinb.com/bsm-tools/protoc-gen-slc/tpl"
|
"git.apinb.com/bsm-tools/protoc-gen-slc/tpl"
|
||||||
|
|
||||||
|
@ -108,13 +110,13 @@ func generateNewServerFile(services []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
||||||
filename := fmt.Sprintf("./internal/server/%s_server.go", strings.ToLower(service.GoName))
|
filename := fmt.Sprintf("./internal/server/%s_server.go", toSnakeCase(service.GoName))
|
||||||
moduleName := getModuleName()
|
moduleName := getModuleName()
|
||||||
|
|
||||||
//create servers.
|
//create servers.
|
||||||
code := tpl.Server
|
code := tpl.Server
|
||||||
imports := []string{
|
imports := []string{
|
||||||
"\"" + moduleName + "/internal/logic/" + strings.ToLower(service.GoName) + "\"",
|
"\"" + moduleName + "/internal/logic/" + toSnakeCase(service.GoName) + "\"",
|
||||||
"pb \"" + moduleName + "/pb\"",
|
"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 {
|
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) {
|
if !utils.PathExists(logicPath) {
|
||||||
os.MkdirAll(logicPath, os.ModePerm)
|
os.MkdirAll(logicPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
@ -285,3 +287,20 @@ func parseOptions(comment string) map[string]string {
|
||||||
|
|
||||||
return result
|
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()
|
||||||
|
}
|
||||||
|
|
|
@ -47,6 +47,6 @@ var StatusReplyCode = `
|
||||||
return &pb.StatusReply{
|
return &pb.StatusReply{
|
||||||
Code: 0,
|
Code: 0,
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
Timeseq: time.Now().UnixNano(),
|
Timeseq: time.Now().UnixMilli(),
|
||||||
}, nil
|
}, nil
|
||||||
`
|
`
|
||||||
|
|
|
@ -69,7 +69,7 @@ func responseEnvelope(_ context.Context, response proto.Message) (interface{}, e
|
||||||
}
|
}
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": string(response.ProtoReflect().Descriptor().Name()),
|
"message": "OK",
|
||||||
"result": response,
|
"result": response,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue