diff --git a/.gitignore b/.gitignore index 0ee6fc0..947ab22 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ _cgo_export.* *.swo /protoc-gen-go-micro +/example/out \ No newline at end of file diff --git a/example/align_test.proto b/example/align_test.proto new file mode 100644 index 0000000..0597bcf --- /dev/null +++ b/example/align_test.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package example; + +option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example;examplepb"; + +message PaddingWaste { + bool flag_a = 1; + int64 value = 2; + bool flag_b = 3; +} \ No newline at end of file diff --git a/example/example.proto b/example/example.proto index c6f429c..577223a 100644 --- a/example/example.proto +++ b/example/example.proto @@ -2,7 +2,7 @@ syntax = "proto3"; package example; -option go_package = "github.com/unistack-org/protoc-gen-go-micro/v4/example;examplepb"; +option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example;examplepb"; import "tag/tag.proto"; import "api/annotations.proto"; diff --git a/example/stream_test.proto b/example/stream_test.proto new file mode 100644 index 0000000..7ea8c61 --- /dev/null +++ b/example/stream_test.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package example; + +option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example/out/go;examplepb"; + +service StreamExample { + // server-side streaming + rpc ServerStream(StreamReq) returns (stream StreamRsp) {}; + // client-side streaming + rpc ClientStream(stream StreamReq) returns (StreamRsp) {}; + // bidirectional streaming + rpc BidiStream(stream StreamReq) returns (stream StreamRsp) {}; +} + +message StreamReq { string name = 1; } +message StreamRsp { string data = 1; } \ No newline at end of file diff --git a/fiealaligment.go b/fiealaligment.go index 1446473..8eb1171 100644 --- a/fiealaligment.go +++ b/fiealaligment.go @@ -1,11 +1,16 @@ package main import ( - "log" + "fmt" + "go/token" + "go/types" "os" + "sort" + "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/fieldalignment" - "golang.org/x/tools/go/analysis/singlechecker" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/packages" "google.golang.org/protobuf/compiler/protogen" ) @@ -14,11 +19,97 @@ func (g *Generator) fieldAlign(plugin *protogen.Plugin) error { return nil } - log.Printf("%v\n", []string{"fieldalignment", "-fix", g.tagPath}) - origArgs := os.Args - os.Args = []string{"fieldalignment", "-fix", g.tagPath} - singlechecker.Main(fieldalignment.Analyzer) - os.Args = origArgs + fset := token.NewFileSet() + cfg := &packages.Config{ + Mode: packages.NeedName | packages.NeedFiles | packages.NeedSyntax | + packages.NeedTypes | packages.NeedTypesInfo | packages.NeedTypesSizes, + Fset: fset, + Dir: g.tagPath, + } + + pkgs, err := packages.Load(cfg, ".") + if err != nil { + return fmt.Errorf("fieldalignment load: %w", err) + } + + for _, pkg := range pkgs { + if len(pkg.Errors) > 0 { + continue + } + + makePass := func(a *analysis.Analyzer, resultOf map[*analysis.Analyzer]interface{}) *analysis.Pass { + return &analysis.Pass{ + Analyzer: a, + Fset: fset, + Files: pkg.Syntax, + Pkg: pkg.Types, + TypesInfo: pkg.TypesInfo, + TypesSizes: pkg.TypesSizes, + ResultOf: resultOf, + Report: func(d analysis.Diagnostic) {}, + ImportObjectFact: func(obj types.Object, fact analysis.Fact) bool { return false }, + ExportObjectFact: func(obj types.Object, fact analysis.Fact) {}, + ImportPackageFact: func(pkg *types.Package, fact analysis.Fact) bool { return false }, + ExportPackageFact: func(fact analysis.Fact) {}, + AllObjectFacts: func() []analysis.ObjectFact { return nil }, + AllPackageFacts: func() []analysis.PackageFact { return nil }, + } + } + + inspectResult, err := inspect.Analyzer.Run(makePass(inspect.Analyzer, nil)) + if err != nil { + return fmt.Errorf("inspect run: %w", err) + } + + var fixes []analysis.SuggestedFix + alignPass := makePass(fieldalignment.Analyzer, map[*analysis.Analyzer]interface{}{ + inspect.Analyzer: inspectResult, + }) + alignPass.Report = func(d analysis.Diagnostic) { + fixes = append(fixes, d.SuggestedFixes...) + } + + if _, err := fieldalignment.Analyzer.Run(alignPass); err != nil { + return fmt.Errorf("fieldalignment run: %w", err) + } + + if err := applyTextEdits(fset, fixes); err != nil { + return fmt.Errorf("fieldalignment apply: %w", err) + } + } + + return nil +} + +func applyTextEdits(fset *token.FileSet, fixes []analysis.SuggestedFix) error { + byFile := make(map[string][]analysis.TextEdit) + for _, fix := range fixes { + for _, edit := range fix.TextEdits { + fname := fset.Position(edit.Pos).Filename + byFile[fname] = append(byFile[fname], edit) + } + } + + for fname, edits := range byFile { + content, err := os.ReadFile(fname) + if err != nil { + return err + } + + sort.Slice(edits, func(i, j int) bool { + return edits[i].Pos > edits[j].Pos + }) + + for _, edit := range edits { + start := fset.Position(edit.Pos).Offset + end := fset.Position(edit.End).Offset + content = append(content[:start], append(edit.NewText, content[end:]...)...) + } + + if err := os.WriteFile(fname, content, 0o644); err != nil { + return err + } + } return nil } diff --git a/generate.go b/generate.go index eecd79b..8002a40 100644 --- a/generate.go +++ b/generate.go @@ -1,3 +1,14 @@ package main //go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto" + +//go:generate go build -o $GOPATH/bin/protoc-gen-go-micro . +//go:generate mkdir -p ./example/out/go/grpc ./example/out/align + +// stream_test: micro + grpc +//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go_out=paths=source_relative:./example/out/go --go-micro_out=components=micro,standalone=false,paths=source_relative:./example/out/go ./example/stream_test.proto" +//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=grpc,standalone=true,paths=source_relative:./example/out/go/grpc ./example/stream_test.proto" + +// align_test: fieldalignment +//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go_out=paths=source_relative:./example/out/align ./example/align_test.proto" +//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=micro,standalone=false,fieldaligment=true,tag_path=./example/out/align,paths=source_relative:./example/out/align ./example/align_test.proto" diff --git a/go.mod b/go.mod index 09c26b2..5e73a32 100644 --- a/go.mod +++ b/go.mod @@ -1,30 +1,28 @@ module go.unistack.org/protoc-gen-go-micro/v4 -go 1.22.0 -toolchain go1.24.1 +go 1.24.0 require ( github.com/fatih/structtag v1.2.0 - github.com/jhump/protoreflect v1.17.0 + github.com/jhump/protoreflect v1.18.0 github.com/vektah/gqlparser/v2 v2.5.27 go.unistack.org/micro-proto/v4 v4.1.0 - golang.org/x/tools v0.33.0 - google.golang.org/protobuf v1.35.2 + golang.org/x/tools v0.42.0 + google.golang.org/protobuf v1.36.11 ) require ( - github.com/bufbuild/protocompile v0.14.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic v0.7.0 // indirect github.com/google/gnostic-models v0.6.9 // indirect + github.com/jhump/protoreflect/v2 v2.0.0-beta.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/rogpeppe/go-internal v1.13.1 // indirect - golang.org/x/mod v0.24.0 // indirect - golang.org/x/sync v0.14.0 // indirect - golang.org/x/text v0.21.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect - google.golang.org/grpc v1.68.1 // indirect + github.com/rogpeppe/go-internal v1.14.1 // indirect + github.com/stretchr/testify v1.11.1 // indirect + golang.org/x/mod v0.33.0 // indirect + golang.org/x/sync v0.19.0 // indirect + google.golang.org/grpc v1.79.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e359cfd..4826c3f 100644 --- a/go.sum +++ b/go.sum @@ -737,8 +737,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -788,8 +788,10 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94= -github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8= +github.com/jhump/protoreflect v1.18.0 h1:TOz0MSR/0JOZ5kECB/0ufGnC2jdsgZ123Rd/k4Z5/2w= +github.com/jhump/protoreflect v1.18.0/go.mod h1:ezWcltJIVF4zYdIFM+D/sHV4Oh5LNU08ORzCGfwvTz8= +github.com/jhump/protoreflect/v2 v2.0.0-beta.1 h1:Dw1rslK/VotaUGYsv53XVWITr+5RCPXfvvlGrM/+B6w= +github.com/jhump/protoreflect/v2 v2.0.0-beta.1/go.mod h1:D9LBEowZyv8/iSu97FU2zmXG3JxVTmNw21mu63niFzU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= @@ -816,6 +818,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY= github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8IeTMnF8JTXieKnO4Z6JCsikNEzj0DwauVzE= +github.com/petermattis/goid v0.0.0-20260113132338-7c7de50cc741 h1:KPpdlQLZcHfTMQRi6bFQ7ogNO0ltFT4PmtwTLW4W+14= +github.com/petermattis/goid v0.0.0-20260113132338-7c7de50cc741/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4= github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= @@ -835,8 +839,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= -github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= +github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= @@ -858,8 +862,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/vektah/gqlparser/v2 v2.5.27 h1:RHPD3JOplpk5mP5JGX8RKZkt2/Vwj/PZv0HxTdwFp0s= github.com/vektah/gqlparser/v2 v2.5.27/go.mod h1:D1/VCZtV3LPnQrcPBeR/q5jkSQIPti0uYCP/RI0gIeo= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= @@ -951,8 +955,8 @@ golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -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.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= +golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1009,8 +1013,8 @@ golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= -golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60= +golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1055,8 +1059,8 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= -golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1132,8 +1136,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= -golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= @@ -1155,8 +1159,8 @@ golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU= +golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -1223,8 +1227,8 @@ golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc= -golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI= +golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k= +golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1437,13 +1441,14 @@ google.golang.org/genproto v0.0.0-20230323212658-478b75c54725/go.mod h1:UUQDJDOl google.golang.org/genproto v0.0.0-20230330154414-c0448cd141ea/go.mod h1:UUQDJDOlWu4KYeJZffbWgBkS1YFobzKbLVfK69pe0Ak= google.golang.org/genproto v0.0.0-20230331144136-dcfb400f0633/go.mod h1:UUQDJDOlWu4KYeJZffbWgBkS1YFobzKbLVfK69pe0Ak= google.golang.org/genproto v0.0.0-20230525234025-438c736192d0/go.mod h1:9ExIQyXL5hZrHzQceCwuSYwZZ5QZBazOcprJ5rgs3lY= +google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 h1:9NWlQfY2ePejTmfwUH1OWwmznFa+0kKcHGPDvcPza9M= google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54/go.mod h1:zqTuNwFlFRsw5zIts5VnzLQxSRqh+CGOTVMlYbY0Eyk= google.golang.org/genproto/googleapis/api v0.0.0-20230525234020-1aefcd67740a/go.mod h1:ts19tUU+Z0ZShN1y3aPyq2+O3d5FUNNgT6FtOzmrNn8= google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234015-3fc162c6f38a/go.mod h1:xURIpW9ES5+/GZhnV6beoEtxQrnkRGIfP5VQG2tCBLc= google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 h1:8ZmaLZE4XWrtU3MyClkYqqtl6Oegr3235h7jxsDyqCY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 h1:gRkg/vSppuSQoDjxyiGfN4Upv/h/DQmIR10ZU8dh4Ww= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -1483,8 +1488,8 @@ google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCD google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww= google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g= -google.golang.org/grpc v1.68.1 h1:oI5oTa11+ng8r8XMMN7jAOmWfPZWbYpCFaMUTACxkM0= -google.golang.org/grpc v1.68.1/go.mod h1:+q1XYFJjShcqn0QZHvCyeR4CXPA+llXIeUIfIe00waw= +google.golang.org/grpc v1.79.1 h1:zGhSi45ODB9/p3VAawt9a+O/MULLl9dpizzNNpq7flY= +google.golang.org/grpc v1.79.1/go.mod h1:KmT0Kjez+0dde/v2j9vzwoAScgEPx/Bw1CYChhHLrHQ= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -1503,8 +1508,8 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= -google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= +google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/http.go b/http.go index f964eb4..d1c87d1 100644 --- a/http.go +++ b/http.go @@ -13,40 +13,18 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC continue } - gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go" - path := file.GoImportPath - if g.standalone { - path = "." - } - gfile := plugin.NewGeneratedFile(gname, path) - - gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") - gfile.P("// protoc-gen-go-micro version: " + versionComment) - gfile.P("// source: ", file.Proto.GetName()) - gfile.P() - gfile.P("package ", file.GoPackageName) - gfile.P() - - gfile.Import(contextPackage) - - if genClient { - gfile.Import(microClientPackage) - gfile.Import(microClientHttpPackage) - } - if genServer { - gfile.Import(microServerPackage) - } + gFile := g.newGeneratedFile(plugin, file, component, genClient, genServer) for _, service := range file.Services { - g.generateServiceEndpoints(gfile, service, component) + g.generateServiceEndpoints(gFile, service, component) if genClient { - g.generateServiceClient(gfile, file, service) - g.generateServiceClientMethods(gfile, file, service, component) + g.generateServiceClient(gFile, file, service) + g.generateServiceClientMethods(gFile, file, service, component) } if genServer { - g.generateServiceServer(gfile, file, service) - g.generateServiceServerMethods(gfile, service) - g.generateServiceRegister(gfile, file, service, component) + g.generateServiceServer(gFile, file, service) + g.generateServiceServerMethods(gFile, service) + g.generateServiceRegister(gFile, file, service, component) } } } diff --git a/main.go b/main.go index c85210c..90779e4 100644 --- a/main.go +++ b/main.go @@ -24,11 +24,6 @@ var ( ) func main() { - opts := &protogen.Options{ - ParamFunc: flagSet.Set, - } - - _ = flagSet.Parse(os.Args[1:]) if *flagHelp { flagSet.PrintDefaults() @@ -37,6 +32,10 @@ func main() { g := &Generator{} + opts := &protogen.Options{ + ParamFunc: flagSet.Set, + } + opts.Run(g.Generate) } diff --git a/rpc.go b/rpc.go index 10972e3..92f4269 100644 --- a/rpc.go +++ b/rpc.go @@ -13,40 +13,20 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl continue } - gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go" - path := file.GoImportPath - if g.standalone { - path = "." - } - gfile := plugin.NewGeneratedFile(gname, path) + gFile := g.newGeneratedFile(plugin, file, component, genClient, genServer) - gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") - gfile.P("// protoc-gen-go-micro version: " + versionComment) - gfile.P("// source: ", file.Proto.GetName()) - gfile.P() - gfile.P("package ", file.GoPackageName) - gfile.P() - - gfile.Import(contextPackage) - - if genClient { - gfile.Import(microClientPackage) - } - if genServer { - gfile.Import(microServerPackage) - } for _, service := range file.Services { if genClient { - g.generateServiceClient(gfile, file, service) - g.generateServiceClientMethods(gfile, file, service, component) + g.generateServiceClient(gFile, file, service) + g.generateServiceClientMethods(gFile, file, service, component) } if genServer { - g.generateServiceServer(gfile, file, service) - g.generateServiceServerMethods(gfile, service) - g.generateServiceRegister(gfile, file, service, component) + g.generateServiceServer(gFile, file, service) + g.generateServiceServerMethods(gFile, service) + g.generateServiceRegister(gFile, file, service, component) } if component == "grpc" && g.reflection { - g.generateServiceDesc(gfile, file, service) + g.generateServiceDesc(gFile, file, service) } } } diff --git a/util.go b/util.go index 8cb6485..9102528 100644 --- a/util.go +++ b/util.go @@ -8,9 +8,10 @@ import ( "strings" "time" - api_options "go.unistack.org/micro-proto/v4/api" + apioptions "go.unistack.org/micro-proto/v4/api" v2 "go.unistack.org/micro-proto/v4/openapiv2" v3 "go.unistack.org/micro-proto/v4/openapiv3" + "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/proto" ) @@ -31,6 +32,33 @@ func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] } +func (g *Generator) newGeneratedFile(plugin *protogen.Plugin, file *protogen.File, component string, genClient bool, genServer bool) *protogen.GeneratedFile { + gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go" + path := file.GoImportPath + if g.standalone { + path = protogen.GoImportPath(string(file.GoImportPath) + "/" + component) + } + + gFile := plugin.NewGeneratedFile(gname, path) + + gFile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") + gFile.P("// protoc-gen-go-micro version: " + versionComment) + gFile.P("// source: ", file.Proto.GetName()) + gFile.P() + gFile.P("package ", file.GoPackageName) + gFile.P() + + gFile.Import(contextPackage) + if genClient { + gFile.Import(microClientPackage) + } + if genServer { + gFile.Import(microServerPackage) + } + + return gFile +} + func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { serviceName := service.GoName // if rule, ok := getMicroApiService(service); ok { @@ -42,7 +70,7 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p gfile.P("}") if g.standalone { - gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", file.GoImportPath.Ident(serviceName), "Client {") + gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Client")), " {") } else { gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {") } @@ -141,7 +169,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, } labelMethod: - if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { + if proto.HasExtension(method.Desc.Options(), apioptions.E_Http) { gfile.P("opts = append(opts,") endpoints, _ := generateEndpoints(method) path, method, body := getEndpoint(endpoints[0]) @@ -327,7 +355,7 @@ func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *p serviceName := service.GoName gfile.P("type ", unexport(serviceName), "Server struct {") if g.standalone { - gfile.P(file.GoImportPath.Ident(serviceName), "Server") + gfile.P(gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName + "Server"))) } else { gfile.P(serviceName, "Server") } @@ -488,7 +516,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) { serviceName := service.GoName if g.standalone { - gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") + gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Server")), ", opts ...", microServerPackage.Ident("HandlerOption"), ") error {") } else { gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") } @@ -573,9 +601,11 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent)) } else { - // TODO - // args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: method.Output.GoIdent.GoImportPath})) - args = append(args, serviceName+"_"+method.GoName+"Client") + if g.standalone { + args = append(args, gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"_"+method.GoName+"Client"))) + } else { + args = append(args, serviceName+"_"+method.GoName+"Client") + } } args = append(args, ", error) {") gfile.P(args...) @@ -674,64 +704,64 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat } } -func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool) { +func generateEndpoints(method *protogen.Method) ([]*apioptions.HttpRule, bool) { if method.Desc.Options() == nil { return nil, false } - if !proto.HasExtension(method.Desc.Options(), api_options.E_Http) { + if !proto.HasExtension(method.Desc.Options(), apioptions.E_Http) { return nil, false } - r := proto.GetExtension(method.Desc.Options(), api_options.E_Http) + r := proto.GetExtension(method.Desc.Options(), apioptions.E_Http) if r == nil { return nil, false } - rule := r.(*api_options.HttpRule) - rules := []*api_options.HttpRule{rule} + rule := r.(*apioptions.HttpRule) + rules := []*apioptions.HttpRule{rule} rules = append(rules, rule.GetAdditionalBindings()...) return rules, method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() } -func getMicroApiMethod(method *protogen.Method) (*api_options.MicroMethod, bool) { +func getMicroApiMethod(method *protogen.Method) (*apioptions.MicroMethod, bool) { if method.Desc.Options() == nil { return nil, false } - if !proto.HasExtension(method.Desc.Options(), api_options.E_MicroMethod) { + if !proto.HasExtension(method.Desc.Options(), apioptions.E_MicroMethod) { return nil, false } - r := proto.GetExtension(method.Desc.Options(), api_options.E_MicroMethod) + r := proto.GetExtension(method.Desc.Options(), apioptions.E_MicroMethod) if r == nil { return nil, false } - rule := r.(*api_options.MicroMethod) + rule := r.(*apioptions.MicroMethod) return rule, true } -func getMicroApiService(service *protogen.Service) (*api_options.MicroService, bool) { +func getMicroApiService(service *protogen.Service) (*apioptions.MicroService, bool) { if service.Desc.Options() == nil { return nil, false } - if !proto.HasExtension(service.Desc.Options(), api_options.E_MicroService) { + if !proto.HasExtension(service.Desc.Options(), apioptions.E_MicroService) { return nil, false } - r := proto.GetExtension(service.Desc.Options(), api_options.E_MicroService) + r := proto.GetExtension(service.Desc.Options(), apioptions.E_MicroService) if r == nil { return nil, false } - rule := r.(*api_options.MicroService) + rule := r.(*apioptions.MicroService) return rule, true } -func getEndpoint(rule *api_options.HttpRule) (string, string, string) { +func getEndpoint(rule *apioptions.HttpRule) (string, string, string) { var meth string var path string var body string @@ -762,7 +792,7 @@ func getEndpoint(rule *api_options.HttpRule) (string, string, string) { return path, meth, body } -func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodName string, rule *api_options.HttpRule, streaming bool) { +func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodName string, rule *apioptions.HttpRule, streaming bool) { path, meth, body := getEndpoint(rule) gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName)) gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path)) @@ -846,7 +876,11 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro gfile.P("// and not to be introspected or modified (even as a copy)") gfile.P("var ", serviceName, "_ServiceDesc", " = ", grpcPackage.Ident("ServiceDesc"), " {") gfile.P("ServiceName: ", strconv.Quote(string(service.Desc.FullName())), ",") - gfile.P("HandlerType: (*", serviceName, "Server)(nil),") + if g.standalone { + gfile.P("HandlerType: (*", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Server")), ")(nil),") + } else { + gfile.P("HandlerType: (*", serviceName, "Server)(nil),") + } gfile.P("Methods: []", grpcPackage.Ident("MethodDesc"), "{") for _, method := range service.Methods { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { @@ -896,7 +930,7 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv serviceName := service.GoName for _, method := range service.Methods { - if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { + if proto.HasExtension(method.Desc.Options(), apioptions.E_Http) { if endpoints, streaming := generateEndpoints(method); endpoints != nil { if !generate { gfile.P("var (") diff --git a/variables.go b/variables.go index 11f2936..722f0d8 100644 --- a/variables.go +++ b/variables.go @@ -1,6 +1,10 @@ package main -import "google.golang.org/protobuf/compiler/protogen" +import ( + "runtime/debug" + + "google.golang.org/protobuf/compiler/protogen" +) var ( ioPackage = protogen.GoImportPath("io") @@ -24,5 +28,10 @@ var ( protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson") timePackage = protogen.GoImportPath("time") deprecationComment = "// Deprecated: Do not use." - versionComment = "v3.10.4" + versionComment = func() string { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" && info.Main.Version != "(devel)" { + return info.Main.Version + } + return "(devel)" + }() )