From 388a58d998e740fd78bb3bebb04dff5c1e639167 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 22 Feb 2023 00:05:54 +0300 Subject: [PATCH] replace map Signed-off-by: Vasiliy Tolstov --- chi.go | 17 +++++--------- gorilla.go | 15 +++++-------- http.go | 1 - micro.go | 3 ++- rpc.go | 2 +- util.go | 63 ++++++++++++++++++++++++---------------------------- variables.go | 3 +-- 7 files changed, 45 insertions(+), 59 deletions(-) diff --git a/chi.go b/chi.go index b0b96a6..464a135 100644 --- a/chi.go +++ b/chi.go @@ -4,9 +4,7 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -var ( - chiPackageFiles map[protogen.GoPackageName]struct{} -) +var chiPackageFiles map[protogen.GoPackageName]struct{} func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error { chiPackageFiles = make(map[protogen.GoPackageName]struct{}) @@ -43,7 +41,6 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error gfile.Import(stringsPackage) gfile.Import(chiPackage) gfile.Import(chiMiddlewarePackage) - gfile.Import(microApiPackage) gfile.P("type routeKey struct{}") @@ -52,17 +49,17 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error gfile.P("return value, ok") gfile.P("}") gfile.P() - gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {") + gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {") gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)") gfile.P("if v.NumMethod() < 1 {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`) gfile.P("}") gfile.P("for _, ep := range eps {") gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`) - gfile.P("if idx < 1 || len(ep.Name) <= idx {") - gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`) + gfile.P(`if idx < 1 || len(ep.Name) <= idx {`) + gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`) gfile.P("}") - gfile.P("name := ep.Name[idx+1:]") + gfile.P(`name := ep.Name[idx+1:]`) gfile.P("m := v.MethodByName(name)") gfile.P("if !m.IsValid() || m.IsZero() {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`) @@ -71,9 +68,7 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error gfile.P("if !ok {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`) gfile.P("}") - gfile.P("for _, method := range ep.Method {") - gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)") - gfile.P("}") + gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`) gfile.P("}") gfile.P("return nil") gfile.P("}") diff --git a/gorilla.go b/gorilla.go index 6147cdd..ff7b2dc 100644 --- a/gorilla.go +++ b/gorilla.go @@ -4,9 +4,7 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -var ( - gorillaPackageFiles map[protogen.GoPackageName]struct{} -) +var gorillaPackageFiles map[protogen.GoPackageName]struct{} func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error { gorillaPackageFiles = make(map[protogen.GoPackageName]struct{}) @@ -40,20 +38,19 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e gfile.Import(httpPackage) gfile.Import(reflectPackage) gfile.Import(stringsPackage) - gfile.Import(microApiPackage) gfile.Import(gorillaMuxPackage) - gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {") + gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {") gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)") gfile.P("if v.NumMethod() < 1 {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`) gfile.P("}") gfile.P("for _, ep := range eps {") gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`) - gfile.P("if idx < 1 || len(ep.Name) <= idx {") - gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`) + gfile.P(`if idx < 1 || len(ep.Name) <= idx {`) + gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`) gfile.P("}") - gfile.P("name := ep.Name[idx+1:]") + gfile.P(`name := ep.Name[idx+1:]`) gfile.P("m := v.MethodByName(name)") gfile.P("if !m.IsValid() || m.IsZero() {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`) @@ -62,7 +59,7 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e gfile.P("if !ok {") gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`) gfile.P("}") - gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)") + gfile.P(`r.HandleFunc(ep.Path, rh).Methods(ep.Method).Name(ep.Name)`) gfile.P("}") gfile.P("return nil") gfile.P("}") diff --git a/http.go b/http.go index 8fc04ab..e854cf1 100644 --- a/http.go +++ b/http.go @@ -28,7 +28,6 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC gfile.P() gfile.Import(contextPackage) - gfile.Import(microApiPackage) if genClient { gfile.Import(microClientPackage) diff --git a/micro.go b/micro.go index dd82fb3..6c33b23 100644 --- a/micro.go +++ b/micro.go @@ -33,12 +33,13 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen gfile.P() gfile.Import(contextPackage) - gfile.Import(microApiPackage) + if genClient { gfile.Import(microClientPackage) } // generate services for _, service := range file.Services { + g.generateServiceName(gfile, service) g.generateServiceEndpoints(gfile, service) if genClient { g.generateServiceClientInterface(gfile, service) diff --git a/rpc.go b/rpc.go index ee36775..049ecac 100644 --- a/rpc.go +++ b/rpc.go @@ -28,7 +28,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl gfile.P() gfile.Import(contextPackage) - gfile.Import(microApiPackage) + if genClient { gfile.Import(microClientPackage) } diff --git a/util.go b/util.go index 6f04368..be68e57 100644 --- a/util.go +++ b/util.go @@ -481,48 +481,16 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, servi serviceName := service.GoName gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") gfile.P("type ", unexport(serviceName), " interface {") - var endpoints []*api_options.HttpRule for _, method := range service.Methods { generateServerSignature(gfile, serviceName, method, true) - if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { - if ep, _ := generateEndpoints(method); ep != nil { - endpoints = append(endpoints, ep...) - } - } } - gfile.P("}") gfile.P("type ", serviceName, " struct {") gfile.P(unexport(serviceName)) gfile.P("}") gfile.P("h := &", unexport(serviceName), "Server{sh}") gfile.P("var nopts []", microServerPackage.Ident("HandlerOption")) - for _, method := range service.Methods { - if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { - if endpoints, streaming := generateEndpoints(method); endpoints != nil { - gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerMetadata"), "(", "map[string]map[string]string{") - for _, ep := range endpoints { - path, method, body := getEndpoint(ep) - gfile.P(`"`, path, `":map[string]string{`) - if vmethod, ok := httpMethodMap[method]; ok { - gfile.P(`"Method": `, httpPackage.Ident(vmethod), `,`) - } else { - gfile.P(`"Method": "`, method, `",`) - } - if body != "" { - gfile.P(`"Body": "`, body, `",`) - } - if streaming { - gfile.P(`"Stream": "true",`) - } else { - gfile.P(`"Stream": "false",`) - } - gfile.P(`},`) - } - gfile.P("}", "))") - } - } - } + gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))") gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))") gfile.P("}") @@ -863,9 +831,36 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro gfile.P() } -func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) { +func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) { serviceName := service.GoName gfile.P("var (") gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`) gfile.P(")") } + +func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) { + serviceName := service.GoName + + gfile.P("var (") + gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{") + + for _, method := range service.Methods { + if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { + if endpoints, streaming := generateEndpoints(method); endpoints != nil { + for _, ep := range endpoints { + epath, emethod, ebody := getEndpoint(ep) + gfile.P("{") + gfile.P(`Name: "`, serviceName+"."+method.GoName, `",`) + gfile.P(`Path: "`, epath, `",`) + gfile.P(`Method: "`, emethod, `",`) + gfile.P(`Body: "`, ebody, `",`) + gfile.P(`Stream: `, streaming, `,`) + gfile.P("},") + } + } + } + } + + gfile.P("}") + gfile.P(")") +} diff --git a/variables.go b/variables.go index 64fa863..6fc5e31 100644 --- a/variables.go +++ b/variables.go @@ -11,7 +11,6 @@ var ( gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux") chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5") chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware") - microApiPackage = protogen.GoImportPath("go.unistack.org/micro/v3/api") microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata") microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client") microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server") @@ -22,5 +21,5 @@ var ( grpcPackage = protogen.GoImportPath("google.golang.org/grpc") timePackage = protogen.GoImportPath("time") deprecationComment = "// Deprecated: Do not use." - versionComment = "v3.5.3" + versionComment = "v3.10.2" )