From 506f19310fe4a1f3353b51b8dddd52bead5e69d5 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 23 Oct 2021 23:21:10 +0300 Subject: [PATCH] some fixes Signed-off-by: Vasiliy Tolstov --- .gitignore | 2 +- util.go | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index b8b0d76..0ee6fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,5 @@ _cgo_export.* *~ *.swp *.swo -/protoc-gen-micro +/protoc-gen-go-micro diff --git a/util.go b/util.go index 9b0886c..e61a9aa 100644 --- a/util.go +++ b/util.go @@ -2,24 +2,27 @@ package main import ( "fmt" + "log" + "net/http" "strings" api_options "go.unistack.org/micro-proto/v3/api" - openapiv2_options "go.unistack.org/micro-proto/v3/openapiv2" + v2 "go.unistack.org/micro-proto/v3/openapiv2" + v3 "go.unistack.org/micro-proto/v3/openapiv3" "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/proto" ) var httpMethodMap = map[string]string{ - "GET": "MethodGet", - "HEAD": "MethodHead", - "POST": "MethodPost", - "PUT": "MethodPut", - "PATCH": "MethodPatch", - "DELETE": "MethodDelete", - "CONNECT": "MethodConnect", - "OPTIONS": "MethodOptions", - "TRACE": "MethodTrace", + http.MethodGet: "MethodGet", + http.MethodHead: "MethodHead", + http.MethodPost: "MethodPost", + http.MethodPut: "MethodPut", + http.MethodPatch: "MethodPatch", + http.MethodDelete: "MethodDelete", + http.MethodConnect: "MethodConnect", + http.MethodOptions: "MethodOptions", + http.MethodTrace: "MethodTrace", } func unexport(s string) string { @@ -49,10 +52,10 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog generateClientFuncSignature(gfile, serviceName, method) if http && method.Desc.Options() != nil { - if proto.HasExtension(method.Desc.Options(), openapiv2_options.E_Openapiv2Operation) { - opts := proto.GetExtension(method.Desc.Options(), openapiv2_options.E_Openapiv2Operation) + if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) { + opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation) if opts != nil { - r := opts.(*openapiv2_options.Operation) + r := opts.(*v2.Operation) gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")") for _, rsp := range r.Responses.ResponseCode { if schema := rsp.Value.GetJsonReference(); schema != nil { @@ -60,7 +63,7 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") { ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") } - if ref == "micro.codec.Frame" { + if ref == "micro.codec.Frame" || ref == ".micro.codec.Frame" { gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}") } else { gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}") @@ -68,11 +71,11 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog } } } - gfile.P("opts = append(opts,") gfile.P(microClientHttpPackage.Ident("ErrorMap"), "(errmap),") gfile.P(")") } + if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { gfile.P("opts = append(opts,") endpoints, _ := generateEndpoints(method) @@ -88,7 +91,16 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog } gfile.P(")") } + + // 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) + } + } + if rule, ok := getMicroApiMethod(method); ok { if rule.Timeout > 0 { gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(", timePackage.Ident("Second"), "*", rule.Timeout, "))")