add support for cookie and header variables
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
a63ea1ec4d
commit
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`
|
||||
|
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"), `(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"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user