Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
|
8c12160173 | |
|
0227a0c384 | |
|
c121ae0bca | |
|
9943ed0e6e | |
|
fa9ceaddc6 | |
|
87734c972f | |
|
3b283780ab | |
|
cf7e34c61d | |
|
9c3ac59467 | |
|
689afaff97 | |
|
dd8913297e |
20
go.mod
20
go.mod
|
@ -1,20 +1,20 @@
|
|||
module protoc-gen-slc
|
||||
module git.apinb.com/bsm-tools/protoc-gen-slc
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
git.apinb.com/bsm-sdk/core v0.0.29
|
||||
golang.org/x/mod v0.24.0
|
||||
google.golang.org/grpc v1.71.0
|
||||
google.golang.org/protobuf v1.36.6
|
||||
git.apinb.com/bsm-sdk/core v0.0.65
|
||||
golang.org/x/mod v0.27.0
|
||||
google.golang.org/grpc v1.75.0
|
||||
google.golang.org/protobuf v1.36.8
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/oklog/ulid/v2 v2.1.0 // indirect
|
||||
golang.org/x/net v0.35.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
|
||||
github.com/oklog/ulid/v2 v2.1.1 // indirect
|
||||
golang.org/x/net v0.43.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
golang.org/x/text v0.28.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250826171959-ef028d996bc1 // indirect
|
||||
)
|
||||
|
|
18
go.sum
18
go.sum
|
@ -1,5 +1,7 @@
|
|||
git.apinb.com/bsm-sdk/core v0.0.29 h1:7ibdZw5doJ0AP+7+5G8/HjZysD1frEapHxtemJySfGs=
|
||||
git.apinb.com/bsm-sdk/core v0.0.29/go.mod h1:FY2knuEVN7d7eHhpkyI+Cj/4oJ34gqZ8xzE7hB3JkOE=
|
||||
git.apinb.com/bsm-sdk/core v0.0.65 h1:mUgX3X6W1Lz4Keh5K6/XGVLc1EcY7AX2xBw6srKQT2g=
|
||||
git.apinb.com/bsm-sdk/core v0.0.65/go.mod h1:FY2knuEVN7d7eHhpkyI+Cj/4oJ34gqZ8xzE7hB3JkOE=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
|
@ -12,6 +14,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
|
||||
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
|
||||
github.com/oklog/ulid/v2 v2.1.1 h1:suPZ4ARWLOJLegGFiZZ1dFAkqzhMjL3J1TzI+5wHz8s=
|
||||
github.com/oklog/ulid/v2 v2.1.1/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
|
||||
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
||||
|
@ -27,15 +31,29 @@ go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC
|
|||
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
|
||||
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
|
||||
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
||||
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb h1:TLPQVbx1GJ8VKZxz52VAxl1EBgKXXbTiU9Fc5fZeLn4=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250826171959-ef028d996bc1 h1:pmJpJEvT846VzausCQ5d7KreSROcDqmO388w5YbnltA=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250826171959-ef028d996bc1/go.mod h1:GmFNa4BdJZ2a8G+wCe9Bg3wwThLrJun751XstdJt5Og=
|
||||
google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
|
||||
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
|
||||
google.golang.org/grpc v1.75.0 h1:+TW+dqTd2Biwe6KKfhE5JpiYIBWq865PhKGSXiivqt4=
|
||||
google.golang.org/grpc v1.75.0/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
|
|
92
main.go
92
main.go
|
@ -1,15 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"protoc-gen-slc/tpl"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"git.apinb.com/bsm-tools/protoc-gen-slc/tpl"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"golang.org/x/mod/modfile"
|
||||
|
@ -24,15 +27,15 @@ func main() {
|
|||
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
||||
|
||||
if !utils.PathExists("./internal") {
|
||||
os.MkdirAll("./internal", 777)
|
||||
os.MkdirAll("./internal", 0755)
|
||||
}
|
||||
|
||||
if !utils.PathExists("./internal/server") {
|
||||
os.MkdirAll("./internal/server", 777)
|
||||
os.MkdirAll("./internal/server", 0755)
|
||||
}
|
||||
|
||||
if !utils.PathExists("./internal/logic") {
|
||||
os.MkdirAll("./internal/logic", 777)
|
||||
os.MkdirAll("./internal/logic", 0755)
|
||||
}
|
||||
|
||||
for _, f := range gen.Files {
|
||||
|
@ -107,13 +110,13 @@ func generateNewServerFile(services []string) 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()
|
||||
|
||||
//create servers.
|
||||
code := tpl.Server
|
||||
imports := []string{
|
||||
"\"" + moduleName + "/internal/logic/" + strings.ToLower(service.GoName) + "\"",
|
||||
"\"" + moduleName + "/internal/logic/" + toSnakeCase(service.GoName) + "\"",
|
||||
"pb \"" + moduleName + "/pb\"",
|
||||
}
|
||||
|
||||
|
@ -145,14 +148,8 @@ 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)
|
||||
logicPath := "./internal/logic/" + toSnakeCase(service.GoName)
|
||||
if !utils.PathExists(logicPath) {
|
||||
os.MkdirAll(logicPath, os.ModePerm)
|
||||
}
|
||||
|
@ -169,6 +166,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 +193,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 +266,41 @@ 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
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
|
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