minor changes for generator
* allow to split client and server generation * allow to override options for handler Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
c7ec840ac0
commit
fcd8dd1f7c
10
http.go
10
http.go
@ -4,7 +4,7 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) error {
|
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
|
||||||
for _, file := range plugin.Files {
|
for _, file := range plugin.Files {
|
||||||
if !file.Generate {
|
if !file.Generate {
|
||||||
continue
|
continue
|
||||||
@ -27,18 +27,26 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
|
|||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
gfile.Import(microApiPackage)
|
||||||
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
gfile.Import(microClientHttpPackage)
|
gfile.Import(microClientHttpPackage)
|
||||||
|
}
|
||||||
|
if genServer {
|
||||||
gfile.Import(microServerPackage)
|
gfile.Import(microServerPackage)
|
||||||
|
}
|
||||||
|
|
||||||
for _, service := range file.Services {
|
for _, service := range file.Services {
|
||||||
|
if genClient {
|
||||||
generateServiceClient(gfile, service)
|
generateServiceClient(gfile, service)
|
||||||
generateServiceClientMethods(gfile, service, true)
|
generateServiceClientMethods(gfile, service, true)
|
||||||
|
}
|
||||||
|
if genServer {
|
||||||
generateServiceServer(gfile, service)
|
generateServiceServer(gfile, service)
|
||||||
generateServiceServerMethods(gfile, service)
|
generateServiceServerMethods(gfile, service)
|
||||||
generateServiceRegister(gfile, service)
|
generateServiceRegister(gfile, service)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
20
main.go
20
main.go
@ -12,7 +12,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
flagDebug = flag.Bool("debug", false, "")
|
flagDebug = flag.Bool("debug", false, "")
|
||||||
flagStandalone = flag.Bool("standalone", false, "")
|
flagStandalone = flag.Bool("standalone", false, "")
|
||||||
flagComponents = flag.String("components", "micro|rpc", "")
|
flagComponents = flag.String("components", "micro|rpc|client|server", "")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -39,15 +39,27 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
|||||||
g.components = *flagComponents
|
g.components = *flagComponents
|
||||||
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
||||||
|
|
||||||
|
var genClient bool
|
||||||
|
var genServer bool
|
||||||
|
|
||||||
|
if strings.Contains(g.components, "server") {
|
||||||
|
genServer = true
|
||||||
|
}
|
||||||
|
if strings.Contains(g.components, "client") {
|
||||||
|
genClient = true
|
||||||
|
}
|
||||||
|
|
||||||
// Protoc passes a slice of File structs for us to process
|
// Protoc passes a slice of File structs for us to process
|
||||||
for _, component := range strings.Split(g.components, "|") {
|
for _, component := range strings.Split(g.components, "|") {
|
||||||
switch component {
|
switch component {
|
||||||
|
case "server", "client":
|
||||||
|
continue
|
||||||
case "micro":
|
case "micro":
|
||||||
err = g.microGenerate(component, plugin)
|
err = g.microGenerate(component, plugin, genClient, genServer)
|
||||||
case "http":
|
case "http":
|
||||||
err = g.httpGenerate(component, plugin)
|
err = g.httpGenerate(component, plugin, genClient, genServer)
|
||||||
case "grpc", "rpc":
|
case "grpc", "rpc":
|
||||||
err = g.rpcGenerate("rpc", plugin)
|
err = g.rpcGenerate("rpc", plugin, genClient, genServer)
|
||||||
case "gorilla":
|
case "gorilla":
|
||||||
err = g.gorillaGenerate(component, plugin)
|
err = g.gorillaGenerate(component, plugin)
|
||||||
case "chi":
|
case "chi":
|
||||||
|
9
micro.go
9
micro.go
@ -4,7 +4,7 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) error {
|
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
|
||||||
for _, file := range plugin.Files {
|
for _, file := range plugin.Files {
|
||||||
if !file.Generate {
|
if !file.Generate {
|
||||||
continue
|
continue
|
||||||
@ -28,16 +28,21 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
|
|||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
gfile.Import(microApiPackage)
|
||||||
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
|
}
|
||||||
// generate services
|
// generate services
|
||||||
for _, service := range file.Services {
|
for _, service := range file.Services {
|
||||||
generateServiceEndpoints(gfile, service)
|
generateServiceEndpoints(gfile, service)
|
||||||
|
if genClient {
|
||||||
generateServiceClientInterface(gfile, service)
|
generateServiceClientInterface(gfile, service)
|
||||||
generateServiceClientStreamInterface(gfile, service)
|
generateServiceClientStreamInterface(gfile, service)
|
||||||
|
}
|
||||||
|
if genServer {
|
||||||
generateServiceServerInterface(gfile, service)
|
generateServiceServerInterface(gfile, service)
|
||||||
generateServiceServerStreamInterface(gfile, service)
|
generateServiceServerStreamInterface(gfile, service)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
rpc.go
11
rpc.go
@ -4,7 +4,7 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error {
|
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
|
||||||
for _, file := range plugin.Files {
|
for _, file := range plugin.Files {
|
||||||
if !file.Generate {
|
if !file.Generate {
|
||||||
continue
|
continue
|
||||||
@ -27,17 +27,24 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error
|
|||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
gfile.Import(microApiPackage)
|
||||||
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
|
}
|
||||||
|
if genServer {
|
||||||
gfile.Import(microServerPackage)
|
gfile.Import(microServerPackage)
|
||||||
|
}
|
||||||
for _, service := range file.Services {
|
for _, service := range file.Services {
|
||||||
|
if genClient {
|
||||||
generateServiceClient(gfile, service)
|
generateServiceClient(gfile, service)
|
||||||
generateServiceClientMethods(gfile, service, false)
|
generateServiceClientMethods(gfile, service, false)
|
||||||
|
}
|
||||||
|
if genServer {
|
||||||
generateServiceServer(gfile, service)
|
generateServiceServer(gfile, service)
|
||||||
generateServiceServerMethods(gfile, service)
|
generateServiceServerMethods(gfile, service)
|
||||||
generateServiceRegister(gfile, service)
|
generateServiceRegister(gfile, service)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
13
util.go
13
util.go
@ -283,10 +283,11 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
|
|||||||
gfile.P(unexport(serviceName))
|
gfile.P(unexport(serviceName))
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||||
|
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||||
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
|
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
|
||||||
gfile.P("opts = append(opts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
|
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, opts...))")
|
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,11 +560,11 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
|
|||||||
path, meth, body := getEndpoint(rule)
|
path, meth, body := getEndpoint(rule)
|
||||||
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
|
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
|
||||||
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
|
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
|
||||||
if vmethod, ok := httpMethodMap[meth]; ok {
|
//if vmethod, ok := httpMethodMap[meth]; ok {
|
||||||
gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
|
// gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
|
||||||
} else {
|
//} else {
|
||||||
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
|
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
|
||||||
}
|
// }
|
||||||
if len(rule.GetGet()) == 0 && body != "" {
|
if len(rule.GetGet()) == 0 && body != "" {
|
||||||
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
|
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user