From dd8913297efdd9dd1a20f45b5591c68a9944a186 Mon Sep 17 00:00:00 2001 From: "david.yan" Date: Thu, 10 Apr 2025 17:25:23 +0800 Subject: [PATCH] fix mod --- go.mod | 2 +- main.go | 61 +++++++++++++++++++++++++++++++++++++++++----------- tpl/logic.go | 31 +++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 8933646..2b9cc5c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module protoc-gen-slc +module git.apinb.com/bsm-tools/protoc-gen-slc go 1.24.0 diff --git a/main.go b/main.go index 9df01cf..310f42b 100644 --- a/main.go +++ b/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 +} diff --git a/tpl/logic.go b/tpl/logic.go index 38168a5..cf45af5 100644 --- a/tpl/logic.go +++ b/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{ + Status: 200, + Message: "ok", + Timeseq: time.Now().UnixNano(), + }, nil +`