This commit is contained in:
david.yan 2025-04-10 17:25:23 +08:00
parent 23c5220745
commit dd8913297e
3 changed files with 79 additions and 15 deletions

2
go.mod
View File

@ -1,4 +1,4 @@
module protoc-gen-slc
module git.apinb.com/bsm-tools/protoc-gen-slc
go 1.24.0

61
main.go
View File

@ -7,10 +7,11 @@ import (
"io"
"os"
"path/filepath"
"protoc-gen-slc/tpl"
"regexp"
"strings"
"git.apinb.com/bsm-tools/protoc-gen-slc/tpl"
"git.apinb.com/bsm-sdk/core/utils"
"golang.org/x/mod/modfile"
"google.golang.org/protobuf/compiler/protogen"
@ -145,12 +146,6 @@ func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *prot
return nil
}
func generateClientFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
filename := fmt.Sprintf("%s_client.pb.go", strings.ToLower(service.GoName))
fmt.Println(filename, file.GoImportPath)
return nil
}
func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
logicPath := "./internal/logic/" + strings.ToLower(service.GoName)
if !utils.PathExists(logicPath) {
@ -169,6 +164,26 @@ func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *proto
"pb \"" + moduleName + "/pb\"",
}
if strings.ToLower(method.Input.GoIdent.GoName) == "identrequest" || strings.ToLower(method.Input.GoIdent.GoName) == "fetchrequest" {
if strings.ToLower(method.Input.GoIdent.GoName) == "identrequest" {
imports = append(imports, "\"git.apinb.com/bsm-sdk/core/errcode\"")
code = strings.ReplaceAll(code, "{valid}", tpl.ValidCode)
}
if strings.ToLower(method.Input.GoIdent.GoName) == "fetchrequest" {
code = strings.ReplaceAll(code, "{valid}", tpl.FetchValidCode)
}
} else {
code = strings.ReplaceAll(code, "{valid}", "// TODO: valid code")
}
if strings.ToLower(method.Output.GoIdent.GoName) == "statusreply" {
imports = append(imports, "\"time\"")
code = strings.ReplaceAll(code, "{return}", tpl.StatusReplyCode)
} else {
code = strings.ReplaceAll(code, "{return}", "return ")
}
code = strings.ReplaceAll(code, "{import}", strings.Join(imports, "\n"))
commit := strings.TrimSpace(method.Comments.Leading.String())
code = strings.ReplaceAll(code, "{func}", method.GoName)
@ -176,13 +191,12 @@ func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *proto
code = strings.ReplaceAll(code, "{input}", method.Input.GoIdent.GoName)
code = strings.ReplaceAll(code, "{output}", method.Output.GoIdent.GoName)
// formattedCode, err := format.Source([]byte(code))
// if err != nil {
// return fmt.Errorf("failed to format generated code: %w", err)
// }
formattedCode, err := format.Source([]byte(code))
if err != nil {
return fmt.Errorf("failed to format generated code: %w", err)
}
// StringToFile(filename, string(formattedCode))
StringToFile(filename, code)
StringToFile(filename, string(formattedCode))
}
return nil
@ -250,3 +264,24 @@ func StringToFile(path, content string) error {
}
return nil
}
// parseOptions 解析注释中的选项字符串并返回一个 map
func parseOptions(comment string) map[string]string {
// 去掉注释符号和分号
comment = strings.Trim(comment, " //;")
// 按逗号分割选项
options := strings.Split(comment, ",")
result := make(map[string]string)
for _, opt := range options {
// 按等号分割键和值
parts := strings.SplitN(opt, "=", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
result[key] = value
}
}
return result
}

View File

@ -15,9 +15,38 @@ func {func}(ctx context.Context, in *pb.{input}) (reply *pb.{output},err error)
if err != nil {
return nil, err
}
{valid}
// TODO: add your logic code & delete this line.
return
{return}
}
`
var CreateCode = `
`
var ValidCode = `
// valildate request id,identity.
if in.Id == 0 && in.Identity == "" {
return nil, errcode.ErrInvalidArgument
}
`
var FetchValidCode = `
// valildate request page_no,page_size.
if in.GetPageNo() < 1 {
in.PageNo = 1
}
if in.GetPageSize() < 10 {
in.PageSize = 50
}
`
var StatusReplyCode = `
return &pb.StatusReply{
Status: 200,
Message: "ok",
Timeseq: time.Now().UnixNano(),
}, nil
`