Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f814b489db | |||
2ec1af2884 |
@@ -10,7 +10,7 @@ This project is a generator plugin for the Google Protocol Buffers compiler (`pr
|
||||
## Usage
|
||||
|
||||
```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
|
||||
@@ -22,5 +22,5 @@ $> protoc --micro_out=debug=true,components="micro|http":. input.proto
|
||||
## 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-micro**: `go get github.com/unistack-org/protoc-gen-go-micro/v3`
|
||||
* 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`
|
||||
|
16
openapiv3.go
16
openapiv3.go
@@ -222,6 +222,15 @@ func (g *openapiv3Generator) buildOperationV3(
|
||||
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.
|
||||
pathParameters := make([]string, 0)
|
||||
if matches := g.namePattern.FindStringSubmatch(path); matches != nil {
|
||||
@@ -244,6 +253,10 @@ func (g *openapiv3Generator) buildOperationV3(
|
||||
}
|
||||
// Add the path parameters to the operation parameters.
|
||||
for _, pathParameter := range pathParameters {
|
||||
if _, ok := sparameters[pathParameter]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
parameters = append(parameters,
|
||||
&v3.ParameterOrReference{
|
||||
Oneof: &v3.ParameterOrReference_Parameter{
|
||||
@@ -268,6 +281,9 @@ func (g *openapiv3Generator) buildOperationV3(
|
||||
for _, field := range inputMessage.Fields {
|
||||
fieldName := string(field.Desc.Name())
|
||||
if !contains(coveredParameters, fieldName) {
|
||||
if _, ok := sparameters[fieldName]; ok {
|
||||
continue
|
||||
}
|
||||
// Get the field description from the comments.
|
||||
fieldDescription := g.filterCommentString(field.Comments.Leading)
|
||||
parameters = append(parameters,
|
||||
|
64
util.go
64
util.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -92,13 +91,39 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
|
||||
gfile.P(")")
|
||||
}
|
||||
|
||||
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)
|
||||
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 {
|
||||
@@ -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})")
|
||||
}
|
||||
} 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("}")
|
||||
@@ -593,6 +651,4 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
|
||||
gfile.P("Stream: true,")
|
||||
}
|
||||
gfile.P(`Handler: "rpc",`)
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -15,8 +15,9 @@ var (
|
||||
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")
|
||||
timePackage = protogen.GoImportPath("time")
|
||||
deprecationComment = "// Deprecated: Do not use."
|
||||
versionComment = "v3.5.2"
|
||||
versionComment = "v3.5.3"
|
||||
)
|
||||
|
Reference in New Issue
Block a user