Compare commits
44 Commits
Author | SHA1 | Date | |
---|---|---|---|
46ff53ccd7 | |||
244afcbe64 | |||
d6307cc251 | |||
543132594b | |||
1bdd68d6ea | |||
dbaa0089b6 | |||
2f9003161b | |||
388a58d998 | |||
d519d8ac6d | |||
92e78b6754 | |||
e25d0cdf02 | |||
71ad836e06 | |||
|
16a4ceaf26 | ||
|
5c4a300a4f | ||
|
821949fed3 | ||
|
b35607a63e | ||
|
da5d5191db | ||
0d2781dfc9 | |||
edd1ba2175 | |||
83b6567d3e | |||
|
e545d81720 | ||
|
a3cf27d062 | ||
|
bc4de7e632 | ||
|
53ed35a106 | ||
|
94b653cbe8 | ||
|
3d887ac2bf | ||
|
a58a9d5173 | ||
|
f072294110 | ||
|
e8389ad0f8 | ||
|
adb15d9a67 | ||
|
78a508f48d | ||
80ed8fba15 | |||
|
a9d41f3adb | ||
a73c4b836f | |||
|
e587b778b2 | ||
b950206585 | |||
|
773793955d | ||
3591d02d42 | |||
a2fea36f50 | |||
e4433a0257 | |||
4f94497f82 | |||
3e1a0a83cf | |||
|
91c4cba229 | ||
|
76e6924d40 |
2
.github/workflows/autoapprove.yml
vendored
2
.github/workflows/autoapprove.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: approve
|
||||
uses: hmarr/auto-approve-action@v2
|
||||
uses: hmarr/auto-approve-action@v3
|
||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||
id: approve
|
||||
with:
|
||||
|
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@@ -10,13 +10,13 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
@@ -34,7 +34,7 @@ jobs:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.1.0
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
8
.github/workflows/codeql-analysis.yml
vendored
8
.github/workflows/codeql-analysis.yml
vendored
@@ -45,12 +45,12 @@ jobs:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: init
|
||||
uses: github/codeql-action/init@v1
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
@@ -61,7 +61,7 @@ jobs:
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: autobuild
|
||||
uses: github/codeql-action/autobuild@v1
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
@@ -75,4 +75,4 @@ jobs:
|
||||
# make release
|
||||
|
||||
- name: analyze
|
||||
uses: github/codeql-action/analyze@v1
|
||||
uses: github/codeql-action/analyze@v2
|
||||
|
2
.github/workflows/dependabot-automerge.yml
vendored
2
.github/workflows/dependabot-automerge.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
||||
steps:
|
||||
- name: metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v1.3.0
|
||||
uses: dependabot/fetch-metadata@v1.3.6
|
||||
with:
|
||||
github-token: "${{ secrets.TOKEN }}"
|
||||
- name: merge
|
||||
|
6
.github/workflows/pr.yml
vendored
6
.github/workflows/pr.yml
vendored
@@ -10,13 +10,13 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
@@ -34,7 +34,7 @@ jobs:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.1.0
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
@@ -23,4 +23,4 @@ $> protoc --go_micro_out=debug=true,components="micro|http":. input.proto
|
||||
|
||||
* Install the **go** compiler and tools from https://golang.org/doc/install
|
||||
* Install **protoc-gen-go**: `go install google.golang.org/protobuf/cmd/protoc-gen-go`
|
||||
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v3`
|
||||
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v4`
|
||||
|
2
ast.go
2
ast.go
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
tag_options "go.unistack.org/micro-proto/v3/tag"
|
||||
tag_options "go.unistack.org/micro-proto/v4/tag"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
17
chi.go
17
chi.go
@@ -4,9 +4,7 @@ import (
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
var (
|
||||
chiPackageFiles map[protogen.GoPackageName]struct{}
|
||||
)
|
||||
var chiPackageFiles map[protogen.GoPackageName]struct{}
|
||||
|
||||
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
|
||||
chiPackageFiles = make(map[protogen.GoPackageName]struct{})
|
||||
@@ -43,7 +41,6 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.Import(stringsPackage)
|
||||
gfile.Import(chiPackage)
|
||||
gfile.Import(chiMiddlewarePackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
gfile.P("type routeKey struct{}")
|
||||
|
||||
@@ -52,17 +49,17 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.P("return value, ok")
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
|
||||
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
|
||||
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||
gfile.P("if v.NumMethod() < 1 {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, ep := range eps {")
|
||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
|
||||
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("name := ep.Name[idx+1:]")
|
||||
gfile.P(`name := ep.Name[idx+1:]`)
|
||||
gfile.P("m := v.MethodByName(name)")
|
||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
||||
@@ -71,9 +68,7 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.P("if !ok {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, method := range ep.Method {")
|
||||
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
|
||||
gfile.P("}")
|
||||
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`)
|
||||
gfile.P("}")
|
||||
gfile.P("return nil")
|
||||
gfile.P("}")
|
||||
|
@@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package example;
|
||||
|
||||
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v3/example;examplepb";
|
||||
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v4/example;examplepb";
|
||||
|
||||
import "tag/tag.proto";
|
||||
import "api/annotations.proto";
|
||||
@@ -26,7 +26,7 @@ service Example {
|
||||
}
|
||||
};
|
||||
option (micro.api.http) = { post: "/v1/example/call/{name}"; body: "*"; };
|
||||
option (micro.api.micro_method) = { timeout: 5; };
|
||||
option (micro.api.micro_method) = { timeout: "5s"; };
|
||||
};
|
||||
};
|
||||
|
||||
|
18
go.mod
18
go.mod
@@ -1,10 +1,18 @@
|
||||
module go.unistack.org/protoc-gen-go-micro/v3
|
||||
module go.unistack.org/protoc-gen-go-micro/v4
|
||||
|
||||
go 1.16
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0
|
||||
go.unistack.org/micro-proto/v3 v3.2.7
|
||||
golang.org/x/tools v0.1.10
|
||||
google.golang.org/protobuf v1.27.1
|
||||
go.unistack.org/micro-proto/v4 v4.0.1
|
||||
golang.org/x/tools v0.1.12
|
||||
google.golang.org/protobuf v1.28.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/gnostic v0.6.9 // indirect
|
||||
golang.org/x/mod v0.8.0 // indirect
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
38
go.sum
38
go.sum
@@ -39,8 +39,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/gnostic v0.6.6 h1:MVSM2r2j9aRUvYNym66JGW96Ddd5MN4sTi59yktb6yk=
|
||||
github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||
github.com/google/gnostic v0.6.9 h1:ZK/5VhkoX835RikCHpSUJV9a+S3e1zLh59YnyWeBW+0=
|
||||
github.com/google/gnostic v0.6.9/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
@@ -66,38 +66,32 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
||||
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
||||
go.unistack.org/micro-proto/v4 v4.0.1 h1:2RKHgtCOOcAFgKsnngGK5bqM/6MWXOjVCdw03dbuoF8=
|
||||
go.unistack.org/micro-proto/v4 v4.0.1/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
|
||||
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
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=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
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-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -106,25 +100,20 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
|
||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
|
||||
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
|
||||
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/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -153,15 +142,16 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
|
||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
15
gorilla.go
15
gorilla.go
@@ -4,9 +4,7 @@ import (
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
var (
|
||||
gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
||||
)
|
||||
var gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
||||
|
||||
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
|
||||
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{})
|
||||
@@ -40,20 +38,19 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
|
||||
gfile.Import(httpPackage)
|
||||
gfile.Import(reflectPackage)
|
||||
gfile.Import(stringsPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
gfile.Import(gorillaMuxPackage)
|
||||
|
||||
gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
|
||||
gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
|
||||
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||
gfile.P("if v.NumMethod() < 1 {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, ep := range eps {")
|
||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
|
||||
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("name := ep.Name[idx+1:]")
|
||||
gfile.P(`name := ep.Name[idx+1:]`)
|
||||
gfile.P("m := v.MethodByName(name)")
|
||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
||||
@@ -62,7 +59,7 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
|
||||
gfile.P("if !ok {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||
gfile.P("}")
|
||||
gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)")
|
||||
gfile.P(`r.HandleFunc(ep.Path, rh).Methods(ep.Method).Name(ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("return nil")
|
||||
gfile.P("}")
|
||||
|
5
http.go
5
http.go
@@ -28,7 +28,6 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
@@ -41,12 +40,12 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
||||
for _, service := range file.Services {
|
||||
if genClient {
|
||||
g.generateServiceClient(gfile, service)
|
||||
g.generateServiceClientMethods(gfile, service, true)
|
||||
g.generateServiceClientMethods(gfile, service, component)
|
||||
}
|
||||
if genServer {
|
||||
generateServiceServer(gfile, service)
|
||||
g.generateServiceServerMethods(gfile, service)
|
||||
g.generateServiceRegister(gfile, service)
|
||||
g.generateServiceRegister(gfile, service, component)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
5
main.go
5
main.go
@@ -18,6 +18,7 @@ var (
|
||||
flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3", "specify components to generate")
|
||||
flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir")
|
||||
flagOpenapiFile = flagSet.String("openapi_file", "apidocs.swagger.json", "openapi file name")
|
||||
flagReflection = flagSet.Bool("reflection", false, "enable server reflection support")
|
||||
flagHelp = flagSet.Bool("help", false, "display help message")
|
||||
)
|
||||
|
||||
@@ -45,6 +46,7 @@ type Generator struct {
|
||||
fieldaligment bool
|
||||
tagPath string
|
||||
openapiFile string
|
||||
reflection bool
|
||||
plugin *protogen.Plugin
|
||||
}
|
||||
|
||||
@@ -58,6 +60,7 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
g.fieldaligment = *flagFieldaligment
|
||||
g.tagPath = *flagTagPath
|
||||
g.openapiFile = *flagOpenapiFile
|
||||
g.reflection = *flagReflection
|
||||
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
||||
|
||||
var genClient bool
|
||||
@@ -90,7 +93,7 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
case "http":
|
||||
err = g.httpGenerate(component, plugin, genClient, genServer)
|
||||
case "grpc", "drpc", "rpc":
|
||||
err = g.rpcGenerate("rpc", plugin, genClient, genServer)
|
||||
err = g.rpcGenerate(component, plugin, genClient, genServer)
|
||||
case "gorilla":
|
||||
err = g.gorillaGenerate(component, plugin)
|
||||
case "chi":
|
||||
|
23
micro.go
23
micro.go
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
@@ -22,19 +24,22 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
|
||||
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("// versions:")
|
||||
gfile.P("// - protoc-gen-go-micro " + versionComment)
|
||||
gfile.P("// - protoc ", protocVersion(plugin))
|
||||
gfile.P("// source: ", file.Desc.Path())
|
||||
gfile.P()
|
||||
gfile.P("package ", file.GoPackageName)
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
}
|
||||
// generate services
|
||||
for _, service := range file.Services {
|
||||
g.generateServiceName(gfile, service)
|
||||
g.generateServiceEndpoints(gfile, service)
|
||||
if genClient {
|
||||
g.generateServiceClientInterface(gfile, service)
|
||||
@@ -50,3 +55,15 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func protocVersion(plugin *protogen.Plugin) string {
|
||||
v := plugin.Request.GetCompilerVersion()
|
||||
if v == nil {
|
||||
return "(unknown)"
|
||||
}
|
||||
var suffix string
|
||||
if s := v.GetSuffix(); s != "" {
|
||||
suffix = "-" + s
|
||||
}
|
||||
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
|
||||
}
|
||||
|
@@ -24,8 +24,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go.unistack.org/micro-proto/v3/api"
|
||||
v3 "go.unistack.org/micro-proto/v3/openapiv3"
|
||||
"go.unistack.org/micro-proto/v4/api"
|
||||
v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import v3 "go.unistack.org/micro-proto/v3/openapiv3"
|
||||
import v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
||||
|
||||
func getMediaType(eopt interface{}) string {
|
||||
ctype := "application/json"
|
||||
|
9
rpc.go
9
rpc.go
@@ -28,7 +28,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
}
|
||||
@@ -38,12 +38,15 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
|
||||
for _, service := range file.Services {
|
||||
if genClient {
|
||||
g.generateServiceClient(gfile, service)
|
||||
g.generateServiceClientMethods(gfile, service, false)
|
||||
g.generateServiceClientMethods(gfile, service, component)
|
||||
}
|
||||
if genServer {
|
||||
generateServiceServer(gfile, service)
|
||||
g.generateServiceServerMethods(gfile, service)
|
||||
g.generateServiceRegister(gfile, service)
|
||||
g.generateServiceRegister(gfile, service, component)
|
||||
}
|
||||
if component == "grpc" && g.reflection {
|
||||
g.generateServiceDesc(gfile, file, service)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
158
util.go
158
util.go
@@ -4,11 +4,13 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
api_options "go.unistack.org/micro-proto/v3/api"
|
||||
v2 "go.unistack.org/micro-proto/v3/openapiv2"
|
||||
v3 "go.unistack.org/micro-proto/v3/openapiv3"
|
||||
api_options "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"
|
||||
)
|
||||
@@ -45,13 +47,16 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, service
|
||||
gfile.P()
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, http bool) {
|
||||
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
|
||||
serviceName := service.GoName
|
||||
for _, method := range service.Methods {
|
||||
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
|
||||
if component == "drpc" {
|
||||
methodName = fmt.Sprintf("%s.%s", method.Parent.Desc.FullName(), method.Desc.Name())
|
||||
}
|
||||
g.generateClientFuncSignature(gfile, serviceName, method)
|
||||
|
||||
if http && method.Desc.Options() != nil {
|
||||
if component == "http" && method.Desc.Options() != nil {
|
||||
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
|
||||
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
|
||||
if opts != nil {
|
||||
@@ -210,8 +215,14 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
|
||||
}
|
||||
|
||||
if rule, ok := getMicroApiMethod(method); ok {
|
||||
if rule.Timeout > 0 {
|
||||
gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(", timePackage.Ident("Second"), "*", rule.Timeout, "))")
|
||||
if rule.Timeout != "" {
|
||||
td, err := time.ParseDuration(rule.Timeout)
|
||||
if err != nil {
|
||||
log.Printf("parse duration error %s\n", err.Error())
|
||||
} else {
|
||||
gfile.P("td := ", timePackage.Ident("Duration"), "(", td.Nanoseconds(), ")")
|
||||
gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(td))")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,6 +295,11 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
|
||||
gfile.P()
|
||||
|
||||
if method.Desc.IsStreamingClient() {
|
||||
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Header() ", microMetadataPackage.Ident("Metadata"), "{")
|
||||
gfile.P("return s.stream.Response().Header()")
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
|
||||
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error {")
|
||||
gfile.P("return s.stream.Send(msg)")
|
||||
gfile.P("}")
|
||||
@@ -316,10 +332,16 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
|
||||
for _, method := range service.Methods {
|
||||
generateServerFuncSignature(gfile, serviceName, method, true)
|
||||
if rule, ok := getMicroApiMethod(method); ok {
|
||||
if rule.Timeout > 0 {
|
||||
gfile.P("var cancel ", contextPackage.Ident("CancelFunc"))
|
||||
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", timePackage.Ident("Second"), "*", rule.Timeout, ")")
|
||||
gfile.P("defer cancel()")
|
||||
if rule.Timeout != "" {
|
||||
td, err := time.ParseDuration(rule.Timeout)
|
||||
if err != nil {
|
||||
log.Printf("parse duration error %s\n", err.Error())
|
||||
} else {
|
||||
gfile.P("var cancel ", contextPackage.Ident("CancelFunc"))
|
||||
gfile.P("td := ", timePackage.Ident("Duration"), "(", td.Nanoseconds(), ")")
|
||||
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", "td", ")")
|
||||
gfile.P("defer cancel()")
|
||||
}
|
||||
}
|
||||
}
|
||||
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
||||
@@ -455,7 +477,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
|
||||
serviceName := service.GoName
|
||||
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
||||
gfile.P("type ", unexport(serviceName), " interface {")
|
||||
@@ -468,9 +490,9 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, servi
|
||||
gfile.P("}")
|
||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||
gfile.P("for _, endpoint := range ", serviceName, "Endpoints {")
|
||||
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(&endpoint))")
|
||||
gfile.P("}")
|
||||
if component == "http" {
|
||||
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
|
||||
}
|
||||
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
||||
gfile.P("}")
|
||||
}
|
||||
@@ -595,6 +617,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
|
||||
}
|
||||
gfile.P("Close() error")
|
||||
if method.Desc.IsStreamingClient() {
|
||||
gfile.P("Header() ", microMetadataPackage.Ident("Metadata"))
|
||||
gfile.P("Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error")
|
||||
}
|
||||
if method.Desc.IsStreamingServer() {
|
||||
@@ -632,35 +655,6 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||
gfile.P()
|
||||
gfile.P(serviceName, "Endpoints", "=", "[]", microApiPackage.Ident("Endpoint"), "{")
|
||||
for _, method := range service.Methods {
|
||||
if method.Desc.Options() == nil {
|
||||
continue
|
||||
}
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
endpoints, streaming := generateEndpoints(method)
|
||||
for _, endpoint := range endpoints {
|
||||
gfile.P("{")
|
||||
generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming)
|
||||
gfile.P("},")
|
||||
}
|
||||
}
|
||||
}
|
||||
gfile.P("}")
|
||||
gfile.P(")")
|
||||
gfile.P()
|
||||
|
||||
gfile.P("func New", serviceName, "Endpoints()", "[]", microApiPackage.Ident("Endpoint"), "{")
|
||||
gfile.P("return ", serviceName, "Endpoints")
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
}
|
||||
|
||||
func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool) {
|
||||
if method.Desc.Options() == nil {
|
||||
return nil, false
|
||||
@@ -795,3 +789,79 @@ func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoI
|
||||
}
|
||||
return protogen.GoIdent{}, fmt.Errorf("not found")
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
|
||||
gfile.P("// ", serviceName, "_ServiceDesc", " is the ", grpcPackage.Ident("ServiceDesc"), " for ", serviceName, " service.")
|
||||
gfile.P("// It's only intended for direct use with ", grpcPackage.Ident("RegisterService"), ",")
|
||||
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),")
|
||||
gfile.P("Methods: []", grpcPackage.Ident("MethodDesc"), "{")
|
||||
for _, method := range service.Methods {
|
||||
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
||||
continue
|
||||
}
|
||||
gfile.P("{")
|
||||
gfile.P("MethodName: ", strconv.Quote(string(method.Desc.Name())), ",")
|
||||
gfile.P("Handler: ", method.GoName, ",")
|
||||
gfile.P("},")
|
||||
}
|
||||
gfile.P("},")
|
||||
gfile.P("Streams: []", grpcPackage.Ident("StreamDesc"), "{")
|
||||
for _, method := range service.Methods {
|
||||
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
||||
continue
|
||||
}
|
||||
gfile.P("{")
|
||||
gfile.P("StreamName: ", strconv.Quote(string(method.Desc.Name())), ",")
|
||||
gfile.P("Handler: ", method.GoName, ",")
|
||||
if method.Desc.IsStreamingServer() {
|
||||
gfile.P("ServerStreams: true,")
|
||||
}
|
||||
if method.Desc.IsStreamingClient() {
|
||||
gfile.P("ClientStreams: true,")
|
||||
}
|
||||
gfile.P("},")
|
||||
}
|
||||
gfile.P("},")
|
||||
gfile.P("Metadata: \"", file.Desc.Path(), "\",")
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||
gfile.P(")")
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
|
||||
|
||||
for _, method := range service.Methods {
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
||||
for _, ep := range endpoints {
|
||||
epath, emethod, ebody := getEndpoint(ep)
|
||||
gfile.P("{")
|
||||
gfile.P(`Name: "`, serviceName+"."+method.GoName, `",`)
|
||||
gfile.P(`Path: "`, epath, `",`)
|
||||
gfile.P(`Method: "`, emethod, `",`)
|
||||
gfile.P(`Body: "`, ebody, `",`)
|
||||
gfile.P(`Stream: `, streaming, `,`)
|
||||
gfile.P("},")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gfile.P("}")
|
||||
gfile.P(")")
|
||||
}
|
||||
|
17
variables.go
17
variables.go
@@ -11,14 +11,15 @@ var (
|
||||
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
|
||||
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
|
||||
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
|
||||
microApiPackage = protogen.GoImportPath("go.unistack.org/micro/v3/api")
|
||||
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
|
||||
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
|
||||
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
|
||||
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
|
||||
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec")
|
||||
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors")
|
||||
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v4/metadata")
|
||||
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v4/client")
|
||||
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v4/server")
|
||||
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v4")
|
||||
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v4")
|
||||
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v4/codec")
|
||||
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/errors")
|
||||
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
|
||||
timePackage = protogen.GoImportPath("time")
|
||||
deprecationComment = "// Deprecated: Do not use."
|
||||
versionComment = "v3.5.3"
|
||||
versionComment = "v4.0.0"
|
||||
)
|
||||
|
Reference in New Issue
Block a user