Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
|
87734c972f | |
|
3b283780ab | |
|
cf7e34c61d | |
|
9c3ac59467 | |
|
689afaff97 | |
|
dd8913297e |
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
|||
module protoc-gen-slc
|
||||
module git.apinb.com/bsm-tools/protoc-gen-slc
|
||||
|
||||
go 1.24.0
|
||||
|
||||
|
|
61
main.go
61
main.go
|
@ -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
|
||||
}
|
||||
|
|
31
tpl/logic.go
31
tpl/logic.go
|
@ -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{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
`
|
||||
|
|
|
@ -13,6 +13,7 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -23,7 +24,9 @@ type Server struct {
|
|||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux()}
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
|
||||
// register service to grpc.Server
|
||||
{register}
|
||||
|
@ -58,6 +61,19 @@ 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 = `
|
||||
|
|
Loading…
Reference in New Issue