Merge branch 'v3'

This commit is contained in:
Василий Толстов 2023-05-04 00:23:28 +03:00
commit d6307cc251
17 changed files with 302 additions and 169 deletions

View File

@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: approve - name: approve
uses: hmarr/auto-approve-action@v2 uses: hmarr/auto-approve-action@v3
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]' if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
id: approve id: approve
with: with:

View File

@ -10,13 +10,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v3
with: with:
go-version: 1.16 go-version: 1.17
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache
uses: actions/cache@v2 uses: actions/cache@v3
with: with:
path: ~/go/pkg/mod path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@ -34,7 +34,7 @@ jobs:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: lint - name: lint
uses: golangci/golangci-lint-action@v3.1.0 uses: golangci/golangci-lint-action@v3.4.0
continue-on-error: true continue-on-error: true
with: with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

View File

@ -45,12 +45,12 @@ jobs:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v3
with: with:
go-version: 1.16 go-version: 1.17
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: init - name: init
uses: github/codeql-action/init@v1 uses: github/codeql-action/init@v2
with: with:
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file. # 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). # 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) # If this step fails, then you should remove it and run the build manually (see below)
- name: autobuild - name: autobuild
uses: github/codeql-action/autobuild@v1 uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell. # Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl # 📚 https://git.io/JvXDl
@ -75,4 +75,4 @@ jobs:
# make release # make release
- name: analyze - name: analyze
uses: github/codeql-action/analyze@v1 uses: github/codeql-action/analyze@v2

View File

@ -15,7 +15,7 @@ jobs:
steps: steps:
- name: metadata - name: metadata
id: metadata id: metadata
uses: dependabot/fetch-metadata@v1.3.0 uses: dependabot/fetch-metadata@v1.3.6
with: with:
github-token: "${{ secrets.TOKEN }}" github-token: "${{ secrets.TOKEN }}"
- name: merge - name: merge

View File

@ -10,13 +10,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v3
with: with:
go-version: 1.16 go-version: 1.17
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache
uses: actions/cache@v2 uses: actions/cache@v3
with: with:
path: ~/go/pkg/mod path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@ -34,7 +34,7 @@ jobs:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: lint - name: lint
uses: golangci/golangci-lint-action@v3.1.0 uses: golangci/golangci-lint-action@v3.4.0
continue-on-error: true continue-on-error: true
with: with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

17
chi.go
View File

@ -4,9 +4,7 @@ import (
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
) )
var ( var chiPackageFiles map[protogen.GoPackageName]struct{}
chiPackageFiles map[protogen.GoPackageName]struct{}
)
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error { func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
chiPackageFiles = make(map[protogen.GoPackageName]struct{}) 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(stringsPackage)
gfile.Import(chiPackage) gfile.Import(chiPackage)
gfile.Import(chiMiddlewarePackage) gfile.Import(chiMiddlewarePackage)
gfile.Import(microApiPackage)
gfile.P("type routeKey struct{}") 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("return value, ok")
gfile.P("}") gfile.P("}")
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("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {") gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}") gfile.P("}")
gfile.P("for _, ep := range eps {") gfile.P("for _, ep := range eps {")
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`) gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
gfile.P("if idx < 1 || len(ep.Name) <= idx {") 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(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
gfile.P("}") gfile.P("}")
gfile.P("name := ep.Name[idx+1:]") gfile.P(`name := ep.Name[idx+1:]`)
gfile.P("m := v.MethodByName(name)") gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {") gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`) 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("if !ok {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
gfile.P("}") gfile.P("}")
gfile.P("for _, method := range ep.Method {") gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`)
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
gfile.P("}")
gfile.P("}") gfile.P("}")
gfile.P("return nil") gfile.P("return nil")
gfile.P("}") gfile.P("}")

View File

@ -26,7 +26,7 @@ service Example {
} }
}; };
option (micro.api.http) = { post: "/v1/example/call/{name}"; body: "*"; }; option (micro.api.http) = { post: "/v1/example/call/{name}"; body: "*"; };
option (micro.api.micro_method) = { timeout: 5; }; option (micro.api.micro_method) = { timeout: "5s"; };
}; };
}; };

4
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/fatih/structtag v1.2.0 github.com/fatih/structtag v1.2.0
go.unistack.org/micro-proto/v3 v3.2.7 go.unistack.org/micro-proto/v3 v3.3.1
golang.org/x/tools v0.1.9 golang.org/x/tools v0.1.9
google.golang.org/protobuf v1.27.1 google.golang.org/protobuf v1.28.1
) )

11
go.sum
View File

@ -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.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= 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/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.9 h1:ZK/5VhkoX835RikCHpSUJV9a+S3e1zLh59YnyWeBW+0=
github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E= 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.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.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
@ -68,8 +68,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= 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= 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.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.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo=
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM= go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@ -151,8 +151,9 @@ 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.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-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.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.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 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 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -4,9 +4,7 @@ import (
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
) )
var ( var gorillaPackageFiles map[protogen.GoPackageName]struct{}
gorillaPackageFiles map[protogen.GoPackageName]struct{}
)
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error { func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{}) 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(httpPackage)
gfile.Import(reflectPackage) gfile.Import(reflectPackage)
gfile.Import(stringsPackage) gfile.Import(stringsPackage)
gfile.Import(microApiPackage)
gfile.Import(gorillaMuxPackage) 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("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {") gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}") gfile.P("}")
gfile.P("for _, ep := range eps {") gfile.P("for _, ep := range eps {")
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`) gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
gfile.P("if idx < 1 || len(ep.Name) <= idx {") 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(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
gfile.P("}") gfile.P("}")
gfile.P("name := ep.Name[idx+1:]") gfile.P(`name := ep.Name[idx+1:]`)
gfile.P("m := v.MethodByName(name)") gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {") gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`) 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("if !ok {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
gfile.P("}") 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("}")
gfile.P("return nil") gfile.P("return nil")
gfile.P("}") gfile.P("}")

View File

@ -28,7 +28,6 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
gfile.P() gfile.P()
gfile.Import(contextPackage) gfile.Import(contextPackage)
gfile.Import(microApiPackage)
if genClient { if genClient {
gfile.Import(microClientPackage) gfile.Import(microClientPackage)
@ -40,13 +39,13 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
for _, service := range file.Services { for _, service := range file.Services {
if genClient { if genClient {
generateServiceClient(gfile, service) g.generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, true) g.generateServiceClientMethods(gfile, service, component)
} }
if genServer { if genServer {
generateServiceServer(gfile, service) generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service) g.generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service) g.generateServiceRegister(gfile, service, component)
} }
} }
} }

View File

@ -18,6 +18,7 @@ var (
flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3", "specify components to generate") flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3", "specify components to generate")
flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir") flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir")
flagOpenapiFile = flagSet.String("openapi_file", "apidocs.swagger.json", "openapi file name") 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") flagHelp = flagSet.Bool("help", false, "display help message")
) )
@ -45,17 +46,21 @@ type Generator struct {
fieldaligment bool fieldaligment bool
tagPath string tagPath string
openapiFile string openapiFile string
reflection bool
plugin *protogen.Plugin
} }
func (g *Generator) Generate(plugin *protogen.Plugin) error { func (g *Generator) Generate(plugin *protogen.Plugin) error {
var err error var err error
g.plugin = plugin
g.standalone = *flagStandalone g.standalone = *flagStandalone
g.debug = *flagDebug g.debug = *flagDebug
g.components = *flagComponents g.components = *flagComponents
g.fieldaligment = *flagFieldaligment g.fieldaligment = *flagFieldaligment
g.tagPath = *flagTagPath g.tagPath = *flagTagPath
g.openapiFile = *flagOpenapiFile g.openapiFile = *flagOpenapiFile
g.reflection = *flagReflection
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
var genClient bool var genClient bool
@ -87,8 +92,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
err = g.microGenerate(component, plugin, genClient, genServer) err = g.microGenerate(component, plugin, genClient, genServer)
case "http": case "http":
err = g.httpGenerate(component, plugin, genClient, genServer) err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "rpc": case "grpc", "drpc", "rpc":
err = g.rpcGenerate("rpc", plugin, genClient, genServer) err = g.rpcGenerate(component, plugin, genClient, genServer)
case "gorilla": case "gorilla":
err = g.gorillaGenerate(component, plugin) err = g.gorillaGenerate(component, plugin)
case "chi": case "chi":

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"fmt"
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
) )
@ -22,27 +24,30 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
gfile := plugin.NewGeneratedFile(gname, path) gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment) gfile.P("// versions:")
gfile.P("// source: ", file.Proto.GetName()) gfile.P("// - protoc-gen-go-micro " + versionComment)
gfile.P("// - protoc ", protocVersion(plugin))
gfile.P("// source: ", file.Desc.Path())
gfile.P() gfile.P()
gfile.P("package ", file.GoPackageName) gfile.P("package ", file.GoPackageName)
gfile.P() gfile.P()
gfile.Import(contextPackage) gfile.Import(contextPackage)
gfile.Import(microApiPackage)
if genClient { if genClient {
gfile.Import(microClientPackage) gfile.Import(microClientPackage)
} }
// generate services // generate services
for _, service := range file.Services { for _, service := range file.Services {
generateServiceEndpoints(gfile, service) g.generateServiceName(gfile, service)
g.generateServiceEndpoints(gfile, service)
if genClient { if genClient {
generateServiceClientInterface(gfile, service) g.generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service) g.generateServiceClientStreamInterface(gfile, service)
} }
if genServer { if genServer {
generateServiceServerInterface(gfile, service) g.generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service) g.generateServiceServerStreamInterface(gfile, service)
} }
} }
@ -50,3 +55,15 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
return nil 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)
}

View File

@ -653,9 +653,10 @@ func (g *openapiv3Generator) buildOperationV3(
opt := eopt.(*v3.Operation) opt := eopt.(*v3.Operation)
if r := opt.Responses; r != nil { if r := opt.Responses; r != nil {
responses = r responses = r
}
if ref := responses.Default.GetReference(); ref != nil && ref.GetXRef() != "" { rd := responses.Default
if rd != nil {
if ref := rd.GetReference(); ref != nil && ref.GetXRef() != "" {
xref := strings.TrimPrefix(ref.GetXRef(), ".") xref := strings.TrimPrefix(ref.GetXRef(), ".")
description := "Default" description := "Default"
if strings.Contains(xref, "micro.errors.Error") { if strings.Contains(xref, "micro.errors.Error") {
@ -675,6 +676,7 @@ func (g *openapiv3Generator) buildOperationV3(
} }
} }
} }
}
for _, rref := range responses.GetResponseOrReference() { for _, rref := range responses.GetResponseOrReference() {
if ref := rref.Value.GetReference(); ref != nil && ref.GetXRef() != "" { if ref := rref.Value.GetReference(); ref != nil && ref.GetXRef() != "" {
xref := strings.TrimPrefix(ref.GetXRef(), ".") xref := strings.TrimPrefix(ref.GetXRef(), ".")
@ -700,6 +702,9 @@ func (g *openapiv3Generator) buildOperationV3(
} else { } else {
responses = &v3.Responses{} responses = &v3.Responses{}
} }
} else {
responses = &v3.Responses{}
}
// Create the response. // Create the response.
responses.ResponseOrReference = append(responses.ResponseOrReference, &v3.NamedResponseOrReference{ responses.ResponseOrReference = append(responses.ResponseOrReference, &v3.NamedResponseOrReference{

13
rpc.go
View File

@ -28,7 +28,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
gfile.P() gfile.P()
gfile.Import(contextPackage) gfile.Import(contextPackage)
gfile.Import(microApiPackage)
if genClient { if genClient {
gfile.Import(microClientPackage) gfile.Import(microClientPackage)
} }
@ -37,13 +37,16 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
} }
for _, service := range file.Services { for _, service := range file.Services {
if genClient { if genClient {
generateServiceClient(gfile, service) g.generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, false) g.generateServiceClientMethods(gfile, service, component)
} }
if genServer { if genServer {
generateServiceServer(gfile, service) generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service) g.generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service) g.generateServiceRegister(gfile, service, component)
}
if component == "grpc" && g.reflection {
g.generateServiceDesc(gfile, file, service)
} }
} }
} }

232
util.go
View File

@ -2,8 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time"
api_options "go.unistack.org/micro-proto/v3/api" api_options "go.unistack.org/micro-proto/v3/api"
v2 "go.unistack.org/micro-proto/v3/openapiv2" v2 "go.unistack.org/micro-proto/v3/openapiv2"
@ -28,7 +31,7 @@ func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:] return strings.ToLower(s[:1]) + s[1:]
} }
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
// if rule, ok := getMicroApiService(service); ok { // if rule, ok := getMicroApiService(service); ok {
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", ")) // gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
@ -44,13 +47,16 @@ func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Serv
gfile.P() gfile.P()
} }
func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, http bool) { func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := service.GoName
for _, method := range service.Methods { for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName) methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
generateClientFuncSignature(gfile, serviceName, method) 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) { if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation) opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
if opts != nil { if opts != nil {
@ -61,20 +67,25 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")") gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")")
for _, rsp := range r.Responses.ResponseCode { for _, rsp := range r.Responses.ResponseCode {
if schema := rsp.Value.GetJsonReference(); schema != nil { if schema := rsp.Value.GetJsonReference(); schema != nil {
ref := schema.XRef xref := schema.XRef
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") { if strings.HasPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".") {
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") xref = strings.TrimPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".")
} }
if ref[0] == '.' { if xref[0] == '.' {
ref = ref[1:] xref = xref[1:]
} }
switch ref { switch xref {
case "micro.codec.Frame": case "micro.codec.Frame":
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}") gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
case "micro.errors.Error": case "micro.errors.Error":
gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}") gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}")
default: default:
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}") ident, err := g.getGoIdentByXref(strings.TrimPrefix(schema.XRef, "."))
if err != nil {
log.Printf("cant find message by ref %s\n", schema.XRef)
continue
}
gfile.P(`errmap["`, rsp.Name, `"] = &`, gfile.QualifiedGoIdent(ident), "{}")
} }
} }
} }
@ -97,20 +108,25 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("errmap := make(map[string]interface{}, ", len(resps), ")") gfile.P("errmap := make(map[string]interface{}, ", len(resps), ")")
for _, rsp := range resps { for _, rsp := range resps {
if schema := rsp.Value.GetReference(); schema != nil { if schema := rsp.Value.GetReference(); schema != nil {
ref := schema.XRef xref := schema.XRef
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") { if strings.HasPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".") {
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") xref = strings.TrimPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".")
} }
if ref[0] == '.' { if xref[0] == '.' {
ref = ref[1:] xref = xref[1:]
} }
switch ref { switch xref {
case "micro.codec.Frame": case "micro.codec.Frame":
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}") gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
case "micro.errors.Error": case "micro.errors.Error":
gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}") gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}")
default: default:
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}") ident, err := g.getGoIdentByXref(strings.TrimPrefix(schema.XRef, "."))
if err != nil {
log.Printf("cant find message by ref %s\n", schema.XRef)
continue
}
gfile.P(`errmap["`, rsp.Name, `"] = &`, gfile.QualifiedGoIdent(ident), "{}")
} }
} }
} }
@ -199,8 +215,14 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
} }
if rule, ok := getMicroApiMethod(method); ok { if rule, ok := getMicroApiMethod(method); ok {
if rule.Timeout > 0 { if rule.Timeout != "" {
gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(", timePackage.Ident("Second"), "*", 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))")
}
} }
} }
@ -273,6 +295,11 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P() gfile.P()
if method.Desc.IsStreamingClient() { 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("func (s *", unexport(serviceName), "Client", method.GoName, ") Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error {")
gfile.P("return s.stream.Send(msg)") gfile.P("return s.stream.Send(msg)")
gfile.P("}") gfile.P("}")
@ -300,17 +327,23 @@ func generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Serv
gfile.P() gfile.P()
} }
func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
for _, method := range service.Methods { for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true) generateServerFuncSignature(gfile, serviceName, method, true)
if rule, ok := getMicroApiMethod(method); ok { if rule, ok := getMicroApiMethod(method); ok {
if rule.Timeout > 0 { 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("var cancel ", contextPackage.Ident("CancelFunc"))
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", timePackage.Ident("Second"), "*", rule.Timeout, ")") gfile.P("td := ", timePackage.Ident("Duration"), "(", td.Nanoseconds(), ")")
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", "td", ")")
gfile.P("defer cancel()") gfile.P("defer cancel()")
} }
} }
}
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
if !method.Desc.IsStreamingClient() { if !method.Desc.IsStreamingClient() {
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Input.GoIdent), "{}") gfile.P("msg := &", gfile.QualifiedGoIdent(method.Input.GoIdent), "{}")
@ -444,7 +477,7 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
} }
} }
func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := service.GoName
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
gfile.P("type ", unexport(serviceName), " interface {") gfile.P("type ", unexport(serviceName), " interface {")
@ -457,9 +490,9 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
gfile.P("}") gfile.P("}")
gfile.P("h := &", unexport(serviceName), "Server{sh}") gfile.P("h := &", unexport(serviceName), "Server{sh}")
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption")) gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
gfile.P("for _, endpoint := range ", serviceName, "Endpoints {") if component == "http" {
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(&endpoint))") gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
gfile.P("}") }
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))") gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
gfile.P("}") gfile.P("}")
} }
@ -508,7 +541,7 @@ func generateServerSignature(gfile *protogen.GeneratedFile, serviceName string,
gfile.P(args...) gfile.P(args...)
} }
func generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) { func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
args := append([]interface{}{}, args := append([]interface{}{},
"func (c *", "func (c *",
unexport(serviceName), unexport(serviceName),
@ -547,7 +580,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
gfile.P(args...) gfile.P(args...)
} }
func generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
gfile.P("type ", serviceName, "Client interface {") gfile.P("type ", serviceName, "Client interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
@ -557,7 +590,7 @@ func generateServiceClientInterface(gfile *protogen.GeneratedFile, service *prot
gfile.P() gfile.P()
} }
func generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
gfile.P("type ", serviceName, "Server interface {") gfile.P("type ", serviceName, "Server interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
@ -567,7 +600,7 @@ func generateServiceServerInterface(gfile *protogen.GeneratedFile, service *prot
gfile.P() gfile.P()
} }
func generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
@ -584,6 +617,7 @@ func generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service
} }
gfile.P("Close() error") gfile.P("Close() error")
if method.Desc.IsStreamingClient() { if method.Desc.IsStreamingClient() {
gfile.P("Header() ", microMetadataPackage.Ident("Metadata"))
gfile.P("Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error") gfile.P("Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error")
} }
if method.Desc.IsStreamingServer() { if method.Desc.IsStreamingServer() {
@ -594,7 +628,7 @@ func generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service
} }
} }
func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := service.GoName
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
@ -621,35 +655,6 @@ func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service
} }
} }
func 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) { func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool) {
if method.Desc.Options() == nil { if method.Desc.Options() == nil {
return nil, false return nil, false
@ -755,3 +760,108 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
} }
gfile.P(`Handler: "rpc",`) gfile.P(`Handler: "rpc",`)
} }
func (g *Generator) getGoIdentByXref(xref string) (protogen.GoIdent, error) {
idx := strings.LastIndex(xref, ".")
pkg := xref[:idx]
msg := xref[idx+1:]
for _, file := range g.plugin.Files {
if strings.Compare(pkg, *(file.Proto.Package)) != 0 {
continue
}
if ident, err := getGoIdentByMessage(file.Messages, msg); err == nil {
return ident, nil
}
}
return protogen.GoIdent{}, fmt.Errorf("not found")
}
func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoIdent, error) {
for _, message := range messages {
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
return message.GoIdent, nil
}
if len(message.Messages) > 0 {
if ident, err := getGoIdentByMessage(message.Messages, msg); err == nil {
return ident, nil
}
}
}
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(")")
}

View File

@ -11,14 +11,15 @@ var (
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux") gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5") chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware") chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
microApiPackage = protogen.GoImportPath("go.unistack.org/micro/v3/api") microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata")
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client") microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server") microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3") microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3") microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/codec") microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/errors") microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
timePackage = protogen.GoImportPath("time") timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use." deprecationComment = "// Deprecated: Do not use."
versionComment = "v3.5.3" versionComment = "v3.10.2"
) )