use helper to detect import paths

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-02-23 23:54:53 +03:00
parent 8d4aac8b8b
commit 6b8fa89a69
6 changed files with 176 additions and 131 deletions

43
chi.go
View File

@@ -23,52 +23,49 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
chiPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".")
gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro")
gfile.P("package ", file.GoPackageName)
gfile.P()
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.Import(contextPackage)
gfile.Import(fmtPackage)
gfile.Import(httpPackage)
gfile.Import(reflectPackage)
gfile.Import(stringsPackage)
gfile.Import(chiPackage)
gfile.Import(chiMiddlewarePackage)
gfile.Import(microApiPackage)
gfile.P("type routeKey struct{}")
gfile.P("func RouteName(ctx context.Context) (string, bool) {")
gfile.P("func RouteName(ctx ", contextPackage.Ident("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("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return fmt.Errorf("handler has no methods: %T", h)`)
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}")
gfile.P("for _, ep := range eps {")
gfile.P(`idx := strings.Index(ep.Name, ".")`)
gfile.P(`idx := `, stringsPackage.Ident("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(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("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(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))")
gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))")
gfile.P("if !ok {")
gfile.P(`return fmt.Errorf("invalid handler: %#+v", m.Interface())`)
gfile.P(`return `, fmtPackage.Ident("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("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
gfile.P("}")
gfile.P("}")
gfile.P("return nil")