add gorilla and chi helpers

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-22 23:42:49 +03:00
parent 2c001628ff
commit c865c1f665
4 changed files with 275 additions and 21 deletions

79
chi.go Normal file
View File

@ -0,0 +1,79 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
var (
chiPackageFiles map[protogen.GoPackageName]struct{}
)
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
chiPackageFiles = make(map[protogen.GoPackageName]struct{})
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
if _, ok := chiPackageFiles[file.GoPackageName]; ok {
continue
}
chiPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".")
gfile.P("// Code generated by protoc-gen-micro")
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.P("import (")
gfile.P(`"context"`)
gfile.P(`"fmt"`)
gfile.P(`"net/http"`)
gfile.P(`"reflect"`)
gfile.P(`"strings"`)
gfile.P(`chi "github.com/go-chi/chi/v4"`)
gfile.P(`middleware "github.com/go-chi/chi/v4/middleware"`)
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
gfile.P(")")
gfile.P()
gfile.P("type routeKey struct{}")
gfile.P("func RouteName(ctx context.Context) (string, bool) {")
gfile.P("value, ok := ctx.Value(routeKey{}).(string)")
gfile.P("return value, ok")
gfile.P("}")
gfile.P()
gfile.P("func RegisterHandlers(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error {")
gfile.P("v := reflect.ValueOf(h)")
gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return fmt.Errorf("handler has no methods: %T", h)`)
gfile.P("}")
gfile.P("for _, ep := range eps {")
gfile.P(`idx := strings.Index(ep.Name, ".")`)
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
gfile.P(`return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)`)
gfile.P("}")
gfile.P("name := ep.Name[idx+1:]")
gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return fmt.Errorf("invalid handler, method %s not found", name)`)
gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))")
gfile.P("if !ok {")
gfile.P(`return fmt.Errorf("invalid handler: %#+v", m.Interface())`)
gfile.P("}")
gfile.P("for _, method := range ep.Method {")
gfile.P("r.With(middleware.WithValue(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
gfile.P("}")
gfile.P("}")
gfile.P("return nil")
gfile.P("}")
}
return nil
}

68
gorilla.go Normal file
View File

@ -0,0 +1,68 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
var (
gorillaPackageFiles map[protogen.GoPackageName]struct{}
)
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{})
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
if _, ok := gorillaPackageFiles[file.GoPackageName]; ok {
continue
}
gorillaPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".")
gfile.P("// Code generated by protoc-gen-micro")
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.P("import (")
gfile.P(`"fmt"`)
gfile.P(`"net/http"`)
gfile.P(`"reflect"`)
gfile.P(`"strings"`)
gfile.P(`mux "github.com/gorilla/mux"`)
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
gfile.P(")")
gfile.P()
gfile.P("func RegisterHandlers(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {")
gfile.P("v := reflect.ValueOf(h)")
gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return fmt.Errorf("handler has no methods: %T", h)`)
gfile.P("}")
gfile.P("for _, ep := range eps {")
gfile.P(`idx := strings.Index(ep.Name, ".")`)
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
gfile.P(`return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)`)
gfile.P("}")
gfile.P("name := ep.Name[idx+1:]")
gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return fmt.Errorf("invalid handler, method %s not found", name)`)
gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))")
gfile.P("if !ok {")
gfile.P(`return fmt.Errorf("invalid handler: %#+v", m.Interface())`)
gfile.P("}")
gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)")
gfile.P("}")
gfile.P("return nil")
gfile.P("}")
}
return nil
}

39
main.go
View File

@ -8,40 +8,39 @@ import (
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
) )
var (
flags flag.FlagSet
flagDebug *bool
flagComponents *string
flagPaths *string
flagModule *string
)
func init() {
flagDebug = flags.Bool("debug", false, "")
flagComponents = flags.String("components", "micro", "")
flagPaths = flag.String("paths", "", "")
flagModule = flag.String("module", "", "")
}
func main() { func main() {
var flags flag.FlagSet
flagDebug := flags.Bool("debug", false, "")
flagComponents := flags.String("components", "micro", "")
flagPaths := flag.String("paths", "", "")
flagModule := flag.String("module", "", "")
opts := &protogen.Options{ opts := &protogen.Options{
ParamFunc: flags.Set, ParamFunc: flags.Set,
} }
g := &Generator{ g := &Generator{}
debug: *flagDebug,
components: strings.Split(*flagComponents, "|"),
paths: *flagPaths,
module: *flagModule,
}
opts.Run(g.Generate) opts.Run(g.Generate)
} }
type Generator struct { type Generator struct {
debug bool
components []string
paths string
module string
} }
func (g *Generator) Generate(plugin *protogen.Plugin) error { func (g *Generator) Generate(plugin *protogen.Plugin) error {
var err error var err error
// 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 g.components { for _, component := range strings.Split(*flagComponents, "|") {
switch component { switch component {
case "micro": case "micro":
err = g.microGenerate(component, plugin) err = g.microGenerate(component, plugin)
@ -51,6 +50,10 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
err = g.rpcGenerate(component, plugin) err = g.rpcGenerate(component, plugin)
case "openapi", "swagger": case "openapi", "swagger":
err = g.openapiGenerate(component, plugin) err = g.openapiGenerate(component, plugin)
case "gorilla":
err = g.gorillaGenerate(component, plugin)
case "chi":
err = g.chiGenerate(component, plugin)
default: default:
err = fmt.Errorf("unknown component: %s", component) err = fmt.Errorf("unknown component: %s", component)
} }

110
micro.go
View File

@ -11,10 +11,12 @@ import (
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) error { func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) error {
for _, file := range plugin.Files { for _, file := range plugin.Files {
if !file.Generate { if !file.Generate {
continue continue
} }
if len(file.Services) == 0 {
continue
}
gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go" gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, ".")
@ -39,7 +41,11 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
// generate services // generate services
for _, service := range file.Services { for _, service := range file.Services {
generateService(gfile, service) generateServiceEndpoints(gfile, service)
generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service)
generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service)
} }
} }
@ -47,7 +53,105 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
return nil return nil
} }
func generateService(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServerSignature(serviceName string, method *protogen.Method) string {
methodName := string(method.GoName)
req := []string{"ctx context.Context"}
ret := "error"
if !method.Desc.IsStreamingClient() {
req = append(req, "req *"+string(method.Input.Desc.Name()))
}
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
req = append(req, "stream "+serviceName+"_"+methodName+"Stream")
}
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
req = append(req, "rsp *"+string(method.Output.Desc.Name()))
}
return methodName + "(" + strings.Join(req, ", ") + ") " + ret
}
func generateClientSignature(serviceName string, method *protogen.Method) string {
methodName := string(method.GoName)
req := ", req *" + string(method.Input.Desc.Name())
if method.Desc.IsStreamingClient() {
req = ""
}
rsp := "*" + string(method.Output.Desc.Name())
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
rsp = serviceName + "_" + methodName + "Service"
}
return fmt.Sprintf("%s(ctx context.Context%s, opts ...client.CallOption) (%s, error)", methodName, req, rsp)
}
func generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", serviceName, "Service interface {")
for _, method := range service.Methods {
gfile.P(generateClientSignature(serviceName, method))
}
gfile.P("}")
gfile.P()
}
func generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", serviceName, "Handler interface {")
for _, method := range service.Methods {
gfile.P(generateServerSignature(serviceName, method))
}
gfile.P("}")
gfile.P()
}
func generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service")
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
}
methodName := method.GoName
gfile.P("type ", serviceName, "_", methodName, "Service interface {")
gfile.P("Context() context.Context")
gfile.P("SendMsg(msg interface{}) error")
gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("RecvAndClose() (", method.Output.Desc.Name(), ", error)")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
gfile.P("Send(msg *", method.Input.Desc.Name(), ") error")
}
if method.Desc.IsStreamingServer() {
gfile.P("Recv() (msg *", method.Output.Desc.Name(), ", error)")
}
}
}
func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service")
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
}
methodName := method.GoName
gfile.P("type ", serviceName, "_", methodName, "Stream interface {")
gfile.P("Context() context.Context")
gfile.P("SendMsg(msg interface{}) error")
gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("SendAndClose(msg *", method.Output.Desc.Name(), ") error")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
gfile.P("Send(msg *", method.Output.Desc.Name(), ") error")
}
if method.Desc.IsStreamingServer() {
gfile.P("Recv() (msg *", method.Input.Desc.Name(), ", error)")
}
}
}
func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("//New", serviceName, "Endpoints provides api endpoints metdata for ", serviceName, " service") gfile.P("//New", serviceName, "Endpoints provides api endpoints metdata for ", serviceName, " service")
gfile.P("func New", serviceName, "Endpoints() []*api.Endpoint {") gfile.P("func New", serviceName, "Endpoints() []*api.Endpoint {")