commit
2f9003161b
17
chi.go
17
chi.go
@ -4,9 +4,7 @@ import (
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
var (
|
||||
chiPackageFiles map[protogen.GoPackageName]struct{}
|
||||
)
|
||||
var chiPackageFiles map[protogen.GoPackageName]struct{}
|
||||
|
||||
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
|
||||
chiPackageFiles = make(map[protogen.GoPackageName]struct{})
|
||||
@ -43,7 +41,6 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.Import(stringsPackage)
|
||||
gfile.Import(chiPackage)
|
||||
gfile.Import(chiMiddlewarePackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
gfile.P("type routeKey struct{}")
|
||||
|
||||
@ -52,17 +49,17 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.P("return value, ok")
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
|
||||
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
|
||||
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||
gfile.P("if v.NumMethod() < 1 {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, ep := range eps {")
|
||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
|
||||
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("name := ep.Name[idx+1:]")
|
||||
gfile.P(`name := ep.Name[idx+1:]`)
|
||||
gfile.P("m := v.MethodByName(name)")
|
||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
||||
@ -71,9 +68,7 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
||||
gfile.P("if !ok {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, method := range ep.Method {")
|
||||
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
|
||||
gfile.P("}")
|
||||
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`)
|
||||
gfile.P("}")
|
||||
gfile.P("return nil")
|
||||
gfile.P("}")
|
||||
|
15
gorilla.go
15
gorilla.go
@ -4,9 +4,7 @@ import (
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
var (
|
||||
gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
||||
)
|
||||
var gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
||||
|
||||
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
|
||||
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{})
|
||||
@ -40,20 +38,19 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
|
||||
gfile.Import(httpPackage)
|
||||
gfile.Import(reflectPackage)
|
||||
gfile.Import(stringsPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
gfile.Import(gorillaMuxPackage)
|
||||
|
||||
gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
|
||||
gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
|
||||
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||
gfile.P("if v.NumMethod() < 1 {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||
gfile.P("}")
|
||||
gfile.P("for _, ep := range eps {")
|
||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
|
||||
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("name := ep.Name[idx+1:]")
|
||||
gfile.P(`name := ep.Name[idx+1:]`)
|
||||
gfile.P("m := v.MethodByName(name)")
|
||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
||||
@ -62,7 +59,7 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
|
||||
gfile.P("if !ok {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||
gfile.P("}")
|
||||
gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)")
|
||||
gfile.P(`r.HandleFunc(ep.Path, rh).Methods(ep.Method).Name(ep.Name)`)
|
||||
gfile.P("}")
|
||||
gfile.P("return nil")
|
||||
gfile.P("}")
|
||||
|
1
http.go
1
http.go
@ -28,7 +28,6 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
|
3
micro.go
3
micro.go
@ -33,12 +33,13 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
}
|
||||
// generate services
|
||||
for _, service := range file.Services {
|
||||
g.generateServiceName(gfile, service)
|
||||
g.generateServiceEndpoints(gfile, service)
|
||||
if genClient {
|
||||
g.generateServiceClientInterface(gfile, service)
|
||||
|
2
rpc.go
2
rpc.go
@ -28,7 +28,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(contextPackage)
|
||||
gfile.Import(microApiPackage)
|
||||
|
||||
if genClient {
|
||||
gfile.Import(microClientPackage)
|
||||
}
|
||||
|
63
util.go
63
util.go
@ -481,48 +481,16 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, servi
|
||||
serviceName := service.GoName
|
||||
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
||||
gfile.P("type ", unexport(serviceName), " interface {")
|
||||
var endpoints []*api_options.HttpRule
|
||||
for _, method := range service.Methods {
|
||||
generateServerSignature(gfile, serviceName, method, true)
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
if ep, _ := generateEndpoints(method); ep != nil {
|
||||
endpoints = append(endpoints, ep...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gfile.P("}")
|
||||
gfile.P("type ", serviceName, " struct {")
|
||||
gfile.P(unexport(serviceName))
|
||||
gfile.P("}")
|
||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||
for _, method := range service.Methods {
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
||||
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerMetadata"), "(", "map[string]map[string]string{")
|
||||
for _, ep := range endpoints {
|
||||
path, method, body := getEndpoint(ep)
|
||||
gfile.P(`"`, path, `":map[string]string{`)
|
||||
if vmethod, ok := httpMethodMap[method]; ok {
|
||||
gfile.P(`"Method": `, httpPackage.Ident(vmethod), `,`)
|
||||
} else {
|
||||
gfile.P(`"Method": "`, method, `",`)
|
||||
}
|
||||
if body != "" {
|
||||
gfile.P(`"Body": "`, body, `",`)
|
||||
}
|
||||
if streaming {
|
||||
gfile.P(`"Stream": "true",`)
|
||||
} else {
|
||||
gfile.P(`"Stream": "false",`)
|
||||
}
|
||||
gfile.P(`},`)
|
||||
}
|
||||
gfile.P("}", "))")
|
||||
}
|
||||
}
|
||||
}
|
||||
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
|
||||
|
||||
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
||||
gfile.P("}")
|
||||
@ -863,9 +831,36 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
|
||||
gfile.P()
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||
gfile.P(")")
|
||||
}
|
||||
|
||||
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
|
||||
|
||||
for _, method := range service.Methods {
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
||||
for _, ep := range endpoints {
|
||||
epath, emethod, ebody := getEndpoint(ep)
|
||||
gfile.P("{")
|
||||
gfile.P(`Name: "`, serviceName+"."+method.GoName, `",`)
|
||||
gfile.P(`Path: "`, epath, `",`)
|
||||
gfile.P(`Method: "`, emethod, `",`)
|
||||
gfile.P(`Body: "`, ebody, `",`)
|
||||
gfile.P(`Stream: `, streaming, `,`)
|
||||
gfile.P("},")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gfile.P("}")
|
||||
gfile.P(")")
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ var (
|
||||
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
|
||||
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
|
||||
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
|
||||
microApiPackage = protogen.GoImportPath("go.unistack.org/micro/v3/api")
|
||||
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata")
|
||||
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
|
||||
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
|
||||
@ -22,5 +21,5 @@ var (
|
||||
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
|
||||
timePackage = protogen.GoImportPath("time")
|
||||
deprecationComment = "// Deprecated: Do not use."
|
||||
versionComment = "v3.5.3"
|
||||
versionComment = "v3.10.2"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user