From dd872a03b3b8248601508ff10b69e8c86a9e66d2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 23 Jun 2021 00:23:38 +0300 Subject: [PATCH] use plain struct for endpoints Signed-off-by: Vasiliy Tolstov --- chi.go | 1 + gorilla.go | 1 + http.go | 1 + micro.go | 1 + rpc.go | 1 + util.go | 15 ++++++++++----- variables.go | 2 +- 7 files changed, 16 insertions(+), 6 deletions(-) diff --git a/chi.go b/chi.go index bf7ddf9..b0b96a6 100644 --- a/chi.go +++ b/chi.go @@ -32,6 +32,7 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// protoc-gen-go-micro version: " + versionComment) + gfile.P() gfile.P("package ", file.GoPackageName) gfile.P() diff --git a/gorilla.go b/gorilla.go index 5b812ca..6147cdd 100644 --- a/gorilla.go +++ b/gorilla.go @@ -32,6 +32,7 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// protoc-gen-go-micro version: " + versionComment) + gfile.P() gfile.P("package ", file.GoPackageName) gfile.P() diff --git a/http.go b/http.go index 2e992b3..03cdaf3 100644 --- a/http.go +++ b/http.go @@ -23,6 +23,7 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// protoc-gen-go-micro version: " + versionComment) gfile.P("// source: ", file.Proto.GetName()) + gfile.P() gfile.P("package ", file.GoPackageName) gfile.P() diff --git a/micro.go b/micro.go index 1235978..0daf737 100644 --- a/micro.go +++ b/micro.go @@ -24,6 +24,7 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// protoc-gen-go-micro version: " + versionComment) gfile.P("// source: ", file.Proto.GetName()) + gfile.P() gfile.P("package ", file.GoPackageName) gfile.P() diff --git a/rpc.go b/rpc.go index 3143249..157114c 100644 --- a/rpc.go +++ b/rpc.go @@ -23,6 +23,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.") gfile.P("// protoc-gen-go-micro version: " + versionComment) gfile.P("// source: ", file.Proto.GetName()) + gfile.P() gfile.P("package ", file.GoPackageName) gfile.P() diff --git a/util.go b/util.go index 94a640c..003b058 100644 --- a/util.go +++ b/util.go @@ -284,8 +284,8 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se gfile.P("}") gfile.P("h := &", unexport(serviceName), "Server{sh}") gfile.P("var nopts []", microServerPackage.Ident("HandlerOption")) - gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {") - gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))") + gfile.P("for _, endpoint := range ", serviceName, "Endpoints {") + gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(&endpoint))") gfile.P("}") gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))") gfile.P("}") @@ -448,8 +448,8 @@ func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) { serviceName := service.GoName - gfile.P("func New", serviceName, "Endpoints() []*", microApiPackage.Ident("Endpoint"), " {") - gfile.P("return []*", microApiPackage.Ident("Endpoint"), "{") + gfile.P("var (") + gfile.P(serviceName, "Endpoints", "=", "[]", microApiPackage.Ident("Endpoint"), "{") for _, method := range service.Methods { if method.Desc.Options() == nil { continue @@ -457,13 +457,18 @@ func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.S if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { endpoints, streaming := generateEndpoints(method) for _, endpoint := range endpoints { - gfile.P("&", microApiPackage.Ident("Endpoint"), "{") + gfile.P(microApiPackage.Ident("Endpoint"), "{") generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming) gfile.P("},") } } } gfile.P("}") + gfile.P(")") + gfile.P() + + gfile.P("func New", serviceName, "Endpoints()", "[]", microApiPackage.Ident("Endpoint"), "{") + gfile.P("return ", serviceName, "Endpoints") gfile.P("}") gfile.P() } diff --git a/variables.go b/variables.go index bff2c10..ef57f27 100644 --- a/variables.go +++ b/variables.go @@ -17,5 +17,5 @@ var ( microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3") timePackage = protogen.GoImportPath("time") deprecationComment = "// Deprecated: Do not use." - versionComment = "v3.4.0" + versionComment = "v3.4.1" )