Compare commits

...

4 Commits

Author SHA1 Message Date
f69088bd27 fallback to generate client and server
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-30 18:30:58 +03:00
be4eac21df fix example
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-26 16:04:08 +03:00
fcd8dd1f7c 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>
2021-03-26 15:00:55 +03:00
c7ec840ac0 remove some runtime allocaions
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-23 16:56:11 +03:00
5 changed files with 97 additions and 34 deletions

26
http.go
View File

@@ -4,7 +4,7 @@ import (
"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 {
if !file.Generate {
continue
@@ -27,16 +27,24 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
gfile.Import(microClientHttpPackage)
gfile.Import(microServerPackage)
if genClient {
gfile.Import(microClientPackage)
gfile.Import(microClientHttpPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, true)
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
if genClient {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, true)
}
if genServer {
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
}
}
}

26
main.go
View File

@@ -12,7 +12,7 @@ import (
var (
flagDebug = flag.Bool("debug", false, "")
flagStandalone = flag.Bool("standalone", false, "")
flagComponents = flag.String("components", "micro|rpc", "")
flagComponents = flag.String("components", "micro|rpc|http|client|server", "")
)
func main() {
@@ -39,15 +39,33 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.components = *flagComponents
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
}
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
if !genServer && !genClient {
genServer = true
genClient = true
}
}
// Protoc passes a slice of File structs for us to process
for _, component := range strings.Split(g.components, "|") {
switch component {
case "server", "client":
continue
case "micro":
err = g.microGenerate(component, plugin)
err = g.microGenerate(component, plugin, genClient, genServer)
case "http":
err = g.httpGenerate(component, plugin)
err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "rpc":
err = g.rpcGenerate("rpc", plugin)
err = g.rpcGenerate("rpc", plugin, genClient, genServer)
case "gorilla":
err = g.gorillaGenerate(component, plugin)
case "chi":

View File

@@ -4,7 +4,7 @@ import (
"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 {
if !file.Generate {
continue
@@ -28,15 +28,20 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
if genClient {
gfile.Import(microClientPackage)
}
// generate services
for _, service := range file.Services {
generateServiceEndpoints(gfile, service)
generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service)
generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service)
if genClient {
generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service)
}
if genServer {
generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service)
}
}
}

25
rpc.go
View File

@@ -4,7 +4,7 @@ import (
"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 {
if !file.Generate {
continue
@@ -27,15 +27,22 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
gfile.Import(microServerPackage)
if genClient {
gfile.Import(microClientPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, false)
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
if genClient {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, false)
}
if genServer {
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
}
}
}

35
util.go
View File

@@ -10,6 +10,20 @@ import (
"google.golang.org/protobuf/proto"
)
var (
httpMethodMap = map[string]string{
"GET": "MethodGet",
"HEAD": "MethodHead",
"POST": "MethodPost",
"PUT": "MethodPut",
"PATCH": "MethodPatch",
"DELETE": "MethodDelete",
"CONNECT": "MethodConnect",
"OPTIONS": "MethodOptions",
"TRACE": "MethodTrace",
}
)
func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:]
}
@@ -61,9 +75,15 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("opts = append(opts,")
endpoints, _ := generateEndpoints(method)
path, method, body := getEndpoint(endpoints[0])
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`)
if vmethod, ok := httpMethodMap[method]; ok {
gfile.P(microClientHttpPackage.Ident("Method"), `(`, httpPackage.Ident(vmethod), `),`)
} else {
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`)
}
gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`)
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`)
if body != "" {
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`)
}
gfile.P(")")
}
}
@@ -263,10 +283,11 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
gfile.P(unexport(serviceName))
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("opts = append(opts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
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("}")
}
@@ -539,8 +560,12 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
path, meth, body := getEndpoint(rule)
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
//if vmethod, ok := httpMethodMap[meth]; ok {
// gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
//} else {
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
if len(rule.GetGet()) == 0 {
// }
if len(rule.GetGet()) == 0 && body != "" {
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
}
if streaming {