fixes for standalone mode #84

Merged
vtolstov merged 2 commits from reorg into v3 2023-07-31 00:55:33 +03:00
5 changed files with 31 additions and 15 deletions

View File

@ -38,14 +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, service)
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, service, component)
}
if genServer {
generateServiceServer(gfile, service)
g.generateServiceServer(gfile, file, service)
g.generateServiceServerMethods(gfile, service)
g.generateServiceRegister(gfile, service, component)
g.generateServiceRegister(gfile, file, service, component)
}
}
}

View File

@ -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") {

View File

@ -40,7 +40,6 @@ 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.generateServiceClientInterface(gfile, service)
g.generateServiceClientStreamInterface(gfile, service)

6
rpc.go
View File

@ -37,13 +37,13 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
}
for _, service := range file.Services {
if genClient {
g.generateServiceClient(gfile, service)
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, service, component)
}
if genServer {
generateServiceServer(gfile, service)
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)

28
util.go
View File

@ -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 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("}")