diff --git a/go.mod b/go.mod index 18ee815..1a4cfdb 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,21 @@ module git.apinb.com/bsm-tools/protoc-gen-ts -go 1.22 +go 1.24 + +toolchain go1.24.0 require ( - google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d - google.golang.org/protobuf v1.34.2 + google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb + google.golang.org/protobuf v1.36.5 gotest.tools/v3 v3.5.1 ) -require github.com/google/go-cmp v0.5.9 // indirect +require ( + github.com/google/uuid v1.6.0 // indirect + github.com/oklog/ulid/v2 v2.1.0 // indirect +) + +require ( + git.apinb.com/bsm-sdk/core v0.0.48 + github.com/google/go-cmp v0.5.9 // indirect +) diff --git a/go.sum b/go.sum index 15ac3e7..4a41d1c 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,15 @@ +git.apinb.com/bsm-sdk/core v0.0.48 h1:zIzPs+TcUlOz7pfOOcdKtwGX4TgPyLYwhLcRRDaVxc0= +git.apinb.com/bsm-sdk/core v0.0.48/go.mod h1:FY2knuEVN7d7eHhpkyI+Cj/4oJ34gqZ8xzE7hB3JkOE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg= -google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +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/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= +google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb h1:p31xT4yrYrSM/G4Sn2+TNUkVhFCbG9y8itM2S6Th950= +google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:jbe3Bkdp+Dh2IrslsFCklNhweNTBgSYanP1UXhJDhKg= +google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= diff --git a/internal/plugin/commentgen.go b/internal/plugin/commentgen.go index 3a1c12a..d42b09a 100644 --- a/internal/plugin/commentgen.go +++ b/internal/plugin/commentgen.go @@ -13,7 +13,7 @@ type commentGenerator struct { descriptor protoreflect.Descriptor } -func (c commentGenerator) generateLeading(f *codegen.File, indent int) { +func (c commentGenerator) generateLeading(f *codegen.File, indent int) string { loc := c.descriptor.ParentFile().SourceLocations().ByDescriptor(c.descriptor) var comments string if loc.TrailingComments != "" { @@ -34,6 +34,8 @@ func (c commentGenerator) generateLeading(f *codegen.File, indent int) { f.P(t(indent), "/** "+behaviorComment+" */") } } + + return strings.TrimSpace(comments) } func fieldBehaviorComment(field protoreflect.FieldDescriptor) string { diff --git a/internal/plugin/const.go b/internal/plugin/const.go new file mode 100644 index 0000000..9710c9b --- /dev/null +++ b/internal/plugin/const.go @@ -0,0 +1,14 @@ +package plugin + +var ( + AllSrvMethods []SrvMethod +) + +type SrvMethod struct { + PkgName string + ServiceName string + MethodName string + Comment string + In string + Out string +} diff --git a/internal/plugin/generate.go b/internal/plugin/generate.go index 31fb7fc..a05a675 100644 --- a/internal/plugin/generate.go +++ b/internal/plugin/generate.go @@ -14,6 +14,8 @@ import ( ) func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error) { + AllSrvMethods = make([]SrvMethod, 0) + generate := make(map[string]struct{}) registry, err := protodesc.NewFiles(&descriptorpb.FileDescriptorSet{ File: request.GetProtoFile(), @@ -46,7 +48,10 @@ func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRe Name: proto.String(path.Join(indexPathElems...)), Content: proto.String(string(index.Content())), }) + + } res.SupportedFeatures = proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)) + return &res, nil } diff --git a/internal/plugin/packagegen.go b/internal/plugin/packagegen.go index 535a108..15173fb 100644 --- a/internal/plugin/packagegen.go +++ b/internal/plugin/packagegen.go @@ -33,6 +33,8 @@ func (p packageGenerator) Generate(f *codegen.File) error { walkErr = err return false } + + seenService = true } return true diff --git a/internal/plugin/servicegen.go b/internal/plugin/servicegen.go index 13f1c2c..9612ac4 100644 --- a/internal/plugin/servicegen.go +++ b/internal/plugin/servicegen.go @@ -35,6 +35,7 @@ func (s serviceGenerator) generateInterface(f *codegen.File) { input := typeFromMessage(s.pkg, method.Input()) output := typeFromMessage(s.pkg, method.Output()) f.P(t(1), method.Name(), "(request: ", input.Reference(), "): Promise<", output.Reference(), ">;") + }) f.P("}") f.P() @@ -80,6 +81,25 @@ func (s serviceGenerator) generateClient(f *codegen.File) error { } func (s serviceGenerator) generateMethod(f *codegen.File, method protoreflect.MethodDescriptor) error { + + loc := method.ParentFile().SourceLocations().ByDescriptor(method) + var comments string + if loc.TrailingComments != "" { + comments = comments + loc.TrailingComments + } + if loc.LeadingComments != "" { + comments = comments + loc.LeadingComments + } + + AllSrvMethods = append(AllSrvMethods, SrvMethod{ + PkgName: string(s.pkg.Name()), + ServiceName: string(s.service.Name()), + MethodName: string(method.Name()), + Comment: comments, + In: string(method.Input().Name()), + Out: string(method.Output().Name()), + }) + outputType := typeFromMessage(s.pkg, method.Output()) r, ok := httprule.Get(method) if !ok { diff --git a/main.go b/main.go index 22ae01d..129a6ae 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,9 @@ import ( "io" "os" "path/filepath" + "strings" + "git.apinb.com/bsm-sdk/core/utils" "git.apinb.com/bsm-tools/protoc-gen-ts/internal/plugin" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/pluginpb" @@ -16,6 +18,28 @@ func main() { fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err) os.Exit(1) } + + genConst() +} + +func genConst() { + var body map[string][]*plugin.SrvMethod = make(map[string][]*plugin.SrvMethod) + for _, item := range plugin.AllSrvMethods { + name := strings.ToLower(item.PkgName) + body[name] = append(body[name], &item) + } + + for name, item := range body { + constPath := "./sdk/typescript/" + name + "/const.ts" + + var code string + for _, row := range item { + code += "/** " + strings.TrimSpace(row.Comment) + " */\n" + code += fmt.Sprintf("export const URL_%s_%s = \"/%s.%s/%s\"\n", row.ServiceName, row.MethodName, name, row.ServiceName, row.MethodName) + code += "\n" + } + utils.StringToFile(constPath, code) + } } func run() error { @@ -38,5 +62,6 @@ func run() error { if _, err := os.Stdout.Write(out); err != nil { return err } + return nil }