Compare commits

..

No commits in common. "main" and "v0.0.1" have entirely different histories.
main ... v0.0.1

4 changed files with 16 additions and 96 deletions

2
go.mod
View File

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

61
main.go
View File

@ -7,11 +7,10 @@ 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"
@ -146,6 +145,12 @@ 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) {
@ -164,26 +169,6 @@ 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)
@ -191,12 +176,13 @@ 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, string(formattedCode))
StringToFile(filename, code)
}
return nil
@ -264,24 +250,3 @@ 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,38 +15,9 @@ 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{
Code: 0,
Message: "OK",
Timeseq: time.Now().UnixMilli(),
}, nil
`

View File

@ -13,7 +13,6 @@ import (
"git.apinb.com/bsm-sdk/core/vars"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/proto"
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
@ -24,9 +23,7 @@ type Server struct {
}
func New(addr string) *Server {
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
)}
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux()}
// register service to grpc.Server
{register}
@ -61,19 +58,6 @@ func (s *Server) RegisterSwagger() {
})
}
// response envelope
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
name := string(response.ProtoReflect().Descriptor().Name())
if name == "Status" || name == "Error" || name == "StatusReply" {
return response, nil
}
return map[string]any{
"code": 0,
"message": "OK",
"result": response,
}, nil
}
`
var Server = `