From 2ec1af28847e44cf7786c37ea3bae0cfff168363 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 24 Oct 2021 00:52:39 +0300 Subject: [PATCH] add support for cookie and header variables Signed-off-by: Vasiliy Tolstov --- README.md | 6 ++--- util.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- variables.go | 3 ++- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b702bf5..2d46990 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/util.go b/util.go index e61a9aa..7157467 100644 --- a/util.go +++ b/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 } diff --git a/variables.go b/variables.go index 87f6198..c7e7397 100644 --- a/variables.go +++ b/variables.go @@ -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" )