add support for cookie and header variables

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-10-24 00:52:39 +03:00
parent a63ea1ec4d
commit 2ec1af2884
3 changed files with 65 additions and 8 deletions

View File

@ -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`

64
util.go
View File

@ -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"), `(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
} }

View File

@ -15,8 +15,9 @@ var (
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")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec") 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.5.2" versionComment = "v3.5.3"
) )