2021-02-22 23:42:49 +03:00
|
|
|
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"
|
2021-02-25 14:19:09 +03:00
|
|
|
|
|
|
|
path := file.GoImportPath
|
|
|
|
if g.standalone {
|
|
|
|
path = "."
|
|
|
|
}
|
|
|
|
gfile := plugin.NewGeneratedFile(gname, path)
|
2021-02-22 23:42:49 +03:00
|
|
|
|
2021-06-22 01:34:06 +03:00
|
|
|
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
|
|
|
|
gfile.P("// protoc-gen-go-micro version: " + versionComment)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("package ", file.GoPackageName)
|
|
|
|
gfile.P()
|
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.Import(contextPackage)
|
|
|
|
gfile.Import(fmtPackage)
|
|
|
|
gfile.Import(httpPackage)
|
|
|
|
gfile.Import(reflectPackage)
|
|
|
|
gfile.Import(stringsPackage)
|
|
|
|
gfile.Import(chiPackage)
|
|
|
|
gfile.Import(chiMiddlewarePackage)
|
|
|
|
gfile.Import(microApiPackage)
|
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("type routeKey struct{}")
|
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("func RouteName(ctx ", contextPackage.Ident("Context"), ") (string, bool) {")
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("value, ok := ctx.Value(routeKey{}).(string)")
|
|
|
|
gfile.P("return value, ok")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
|
|
|
|
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("if v.NumMethod() < 1 {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("for _, ep := range eps {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("name := ep.Name[idx+1:]")
|
|
|
|
gfile.P("m := v.MethodByName(name)")
|
|
|
|
gfile.P("if !m.IsValid() || m.IsZero() {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("}")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))")
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("if !ok {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("for _, method := range ep.Method {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
|
2021-02-22 23:42:49 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("return nil")
|
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|