Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
f814b489db | |||
2ec1af2884 | |||
a63ea1ec4d |
@@ -10,7 +10,7 @@ This project is a generator plugin for the Google Protocol Buffers compiler (`pr
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$> protoc --micro_out=debug=true,components="micro|http":. input.proto
|
$> protoc --go_micro_out=debug=true,components="micro|http":. input.proto
|
||||||
```
|
```
|
||||||
|
|
||||||
| Option | Default Value | Accepted Values | Description
|
| Option | Default Value | Accepted Values | Description
|
||||||
@@ -22,5 +22,5 @@ $> protoc --micro_out=debug=true,components="micro|http":. input.proto
|
|||||||
## Install
|
## Install
|
||||||
|
|
||||||
* Install the **go** compiler and tools from https://golang.org/doc/install
|
* Install the **go** compiler and tools from https://golang.org/doc/install
|
||||||
* Install **protoc-gen-go**: `go get google.golang.org/protobuf/cmd/protoc-gen-go`
|
* Install **protoc-gen-go**: `go install google.golang.org/protobuf/cmd/protoc-gen-go`
|
||||||
* Install **protoc-gen-go-micro**: `go get github.com/unistack-org/protoc-gen-go-micro/v3`
|
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v3`
|
||||||
|
16
openapiv3.go
16
openapiv3.go
@@ -222,6 +222,15 @@ func (g *openapiv3Generator) buildOperationV3(
|
|||||||
parameters = append(parameters, opt.Parameters...)
|
parameters = append(parameters, opt.Parameters...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sparameters := make(map[string]struct{})
|
||||||
|
for _, paramOrRef := range parameters {
|
||||||
|
parameter := paramOrRef.GetParameter()
|
||||||
|
if parameter == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sparameters[parameter.Name] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// Build a list of path parameters.
|
// Build a list of path parameters.
|
||||||
pathParameters := make([]string, 0)
|
pathParameters := make([]string, 0)
|
||||||
if matches := g.namePattern.FindStringSubmatch(path); matches != nil {
|
if matches := g.namePattern.FindStringSubmatch(path); matches != nil {
|
||||||
@@ -244,6 +253,10 @@ func (g *openapiv3Generator) buildOperationV3(
|
|||||||
}
|
}
|
||||||
// Add the path parameters to the operation parameters.
|
// Add the path parameters to the operation parameters.
|
||||||
for _, pathParameter := range pathParameters {
|
for _, pathParameter := range pathParameters {
|
||||||
|
if _, ok := sparameters[pathParameter]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
parameters = append(parameters,
|
parameters = append(parameters,
|
||||||
&v3.ParameterOrReference{
|
&v3.ParameterOrReference{
|
||||||
Oneof: &v3.ParameterOrReference_Parameter{
|
Oneof: &v3.ParameterOrReference_Parameter{
|
||||||
@@ -268,6 +281,9 @@ func (g *openapiv3Generator) buildOperationV3(
|
|||||||
for _, field := range inputMessage.Fields {
|
for _, field := range inputMessage.Fields {
|
||||||
fieldName := string(field.Desc.Name())
|
fieldName := string(field.Desc.Name())
|
||||||
if !contains(coveredParameters, fieldName) {
|
if !contains(coveredParameters, fieldName) {
|
||||||
|
if _, ok := sparameters[fieldName]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
// Get the field description from the comments.
|
// Get the field description from the comments.
|
||||||
fieldDescription := g.filterCommentString(field.Comments.Leading)
|
fieldDescription := g.filterCommentString(field.Comments.Leading)
|
||||||
parameters = append(parameters,
|
parameters = append(parameters,
|
||||||
|
64
util.go
64
util.go
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -92,13 +91,39 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
|
|||||||
gfile.P(")")
|
gfile.P(")")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parameters := make(map[string]map[string]string)
|
||||||
// Build a list of header parameters.
|
// Build a list of header parameters.
|
||||||
eopt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
eopt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
||||||
if eopt != nil && eopt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
|
if eopt != nil && eopt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
|
||||||
opt := eopt.(*v3.Operation)
|
opt := eopt.(*v3.Operation)
|
||||||
log.Printf("xxx %#+v\n", opt)
|
for _, paramOrRef := range opt.Parameters {
|
||||||
|
parameter := paramOrRef.GetParameter()
|
||||||
|
if parameter == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if parameter.In != "header" && parameter.In != "cookie" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
in, ok := parameters[parameter.In]
|
||||||
|
if !ok {
|
||||||
|
in = make(map[string]string)
|
||||||
|
parameters[parameter.In] = in
|
||||||
|
}
|
||||||
|
in[parameter.Name] = fmt.Sprintf("%v", parameter.Required)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(parameters) > 0 {
|
||||||
|
gfile.P("opts = append(opts,")
|
||||||
|
for pk, pv := range parameters {
|
||||||
|
params := make([]string, 0, len(pv)/2)
|
||||||
|
for k, v := range pv {
|
||||||
|
params = append(params, k, v)
|
||||||
|
}
|
||||||
|
gfile.P(microClientHttpPackage.Ident(strings.Title(pk)), `("`, strings.Join(params, `" ,"`), `"),`)
|
||||||
|
}
|
||||||
|
gfile.P(")")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule, ok := getMicroApiMethod(method); ok {
|
if rule, ok := getMicroApiMethod(method); ok {
|
||||||
@@ -221,6 +246,39 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
|
|||||||
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, &", unexport(serviceName), method.GoName, "Stream{stream})")
|
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, &", unexport(serviceName), method.GoName, "Stream{stream})")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
parameters := make(map[string]map[string]string)
|
||||||
|
// Build a list of header parameters.
|
||||||
|
eopt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
||||||
|
if eopt != nil && eopt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
|
||||||
|
opt := eopt.(*v3.Operation)
|
||||||
|
for _, paramOrRef := range opt.Parameters {
|
||||||
|
parameter := paramOrRef.GetParameter()
|
||||||
|
if parameter == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if parameter.In != "header" && parameter.In != "cookie" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
in, ok := parameters[parameter.In]
|
||||||
|
if !ok {
|
||||||
|
in = make(map[string]string)
|
||||||
|
parameters[parameter.In] = in
|
||||||
|
}
|
||||||
|
in[parameter.Name] = fmt.Sprintf("%v", parameter.Required)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parameters) > 0 {
|
||||||
|
gfile.P(microServerHttpPackage.Ident("FillRequest"), `(ctx, req, `)
|
||||||
|
for pk, pv := range parameters {
|
||||||
|
params := make([]string, 0, len(pv)/2)
|
||||||
|
for k, v := range pv {
|
||||||
|
params = append(params, k, v)
|
||||||
|
}
|
||||||
|
gfile.P(microServerHttpPackage.Ident(strings.Title(pk)), `("`, strings.Join(params, `" ,"`), `"),`)
|
||||||
|
}
|
||||||
|
gfile.P(")")
|
||||||
|
}
|
||||||
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, req, rsp)")
|
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, req, rsp)")
|
||||||
}
|
}
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
@@ -593,6 +651,4 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
|
|||||||
gfile.P("Stream: true,")
|
gfile.P("Stream: true,")
|
||||||
}
|
}
|
||||||
gfile.P(`Handler: "rpc",`)
|
gfile.P(`Handler: "rpc",`)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
13
variables.go
13
variables.go
@@ -11,12 +11,13 @@ 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("github.com/unistack-org/micro/v3/api")
|
microApiPackage = protogen.GoImportPath("go.unistack.org/micro/v3/api")
|
||||||
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
|
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
|
||||||
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
|
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
|
||||||
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
|
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
|
||||||
microCodecPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/codec")
|
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
|
||||||
|
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec")
|
||||||
timePackage = protogen.GoImportPath("time")
|
timePackage = protogen.GoImportPath("time")
|
||||||
deprecationComment = "// Deprecated: Do not use."
|
deprecationComment = "// Deprecated: Do not use."
|
||||||
versionComment = "v3.4.2"
|
versionComment = "v3.5.3"
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user