From 59922913a9a2b0fa857598523c3ae6cc44141907 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 31 Jul 2023 00:54:44 +0300 Subject: [PATCH] fixes for standalone mode Signed-off-by: Vasiliy Tolstov --- http.go | 5 ++++- main.go | 4 ++-- micro.go | 3 --- rpc.go | 4 +++- util.go | 28 ++++++++++++++++++++++------ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/http.go b/http.go index af1bbbb..68fb791 100644 --- a/http.go +++ b/http.go @@ -38,12 +38,15 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC } for _, service := range file.Services { + g.generateServiceEndpoints(gfile, service, component) if genClient { + g.generateServiceClient(gfile, file, service) g.generateServiceClientMethods(gfile, service, component) } if genServer { + g.generateServiceServer(gfile, file, service) g.generateServiceServerMethods(gfile, service) - g.generateServiceRegister(gfile, service, component) + g.generateServiceRegister(gfile, file, service, component) } } } diff --git a/main.go b/main.go index c816eae..7e14fc8 100644 --- a/main.go +++ b/main.go @@ -63,8 +63,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error { g.reflection = *flagReflection plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) - var genClient bool - var genServer bool + genClient := true + genServer := true var genNone bool if strings.Contains(g.components, "server") { diff --git a/micro.go b/micro.go index 14b192f..762e375 100644 --- a/micro.go +++ b/micro.go @@ -40,14 +40,11 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen // generate services for _, service := range file.Services { g.generateServiceName(gfile, service) - g.generateServiceEndpoints(gfile, service, component) if genClient { - g.generateServiceClient(gfile, service) g.generateServiceClientInterface(gfile, service) g.generateServiceClientStreamInterface(gfile, service) } if genServer { - g.generateServiceServer(gfile, service) g.generateServiceServerInterface(gfile, service) g.generateServiceServerStreamInterface(gfile, service) } diff --git a/rpc.go b/rpc.go index 5e960a8..38c2942 100644 --- a/rpc.go +++ b/rpc.go @@ -37,11 +37,13 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl } for _, service := range file.Services { if genClient { + g.generateServiceClient(gfile, file, service) g.generateServiceClientMethods(gfile, service, component) } if genServer { + g.generateServiceServer(gfile, file, service) g.generateServiceServerMethods(gfile, service) - g.generateServiceRegister(gfile, service, component) + g.generateServiceRegister(gfile, file, service, component) } if component == "grpc" && g.reflection { g.generateServiceDesc(gfile, file, service) diff --git a/util.go b/util.go index b34b714..b2b7d8d 100644 --- a/util.go +++ b/util.go @@ -31,7 +31,7 @@ func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] } -func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) { +func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { serviceName := service.GoName // if rule, ok := getMicroApiService(service); ok { // gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", ")) @@ -41,7 +41,11 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, service gfile.P("name string") gfile.P("}") - gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {") + if g.standalone { + gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", file.GoImportPath.Ident(serviceName), "Client {") + } else { + gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {") + } gfile.P("return &", unexport(serviceName), "Client{c: c, name: name}") gfile.P("}") gfile.P() @@ -319,10 +323,14 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, } } -func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Service) { +func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { serviceName := service.GoName gfile.P("type ", unexport(serviceName), "Server struct {") - gfile.P(serviceName, "Server") + if g.standalone { + gfile.P(file.GoImportPath.Ident(serviceName), "Server") + } else { + gfile.P(serviceName, "Server") + } gfile.P("}") gfile.P() } @@ -477,9 +485,13 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, } } -func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service, component string) { +func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) { serviceName := service.GoName - gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") + if g.standalone { + gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") + } else { + gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {") + } gfile.P("type ", unexport(serviceName), " interface {") for _, method := range service.Methods { generateServerSignature(gfile, serviceName, method, true) @@ -491,7 +503,11 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, servi gfile.P("h := &", unexport(serviceName), "Server{sh}") gfile.P("var nopts []", microServerPackage.Ident("HandlerOption")) if component == "http" { + // if g.standalone { + // gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", file.GoImportPath.Ident(serviceName), "ServerEndpoints))") + // } else { gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))") + // } } gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))") gfile.P("}")