replace map #72
17
chi.go
17
chi.go
@ -4,9 +4,7 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var chiPackageFiles map[protogen.GoPackageName]struct{}
|
||||||
chiPackageFiles map[protogen.GoPackageName]struct{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
|
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
|
||||||
chiPackageFiles = make(map[protogen.GoPackageName]struct{})
|
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(stringsPackage)
|
||||||
gfile.Import(chiPackage)
|
gfile.Import(chiPackage)
|
||||||
gfile.Import(chiMiddlewarePackage)
|
gfile.Import(chiMiddlewarePackage)
|
||||||
gfile.Import(microApiPackage)
|
|
||||||
|
|
||||||
gfile.P("type routeKey struct{}")
|
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("return value, ok")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
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("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||||
gfile.P("if v.NumMethod() < 1 {")
|
gfile.P("if v.NumMethod() < 1 {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("for _, ep := range eps {")
|
gfile.P("for _, ep := range eps {")
|
||||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
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(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("name := ep.Name[idx+1:]")
|
gfile.P(`name := ep.Name[idx+1:]`)
|
||||||
gfile.P("m := v.MethodByName(name)")
|
gfile.P("m := v.MethodByName(name)")
|
||||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
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("if !ok {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("for _, method := range ep.Method {")
|
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`)
|
||||||
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
|
|
||||||
gfile.P("}")
|
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("return nil")
|
gfile.P("return nil")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
|
15
gorilla.go
15
gorilla.go
@ -4,9 +4,7 @@ import (
|
|||||||
"google.golang.org/protobuf/compiler/protogen"
|
"google.golang.org/protobuf/compiler/protogen"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
||||||
gorillaPackageFiles map[protogen.GoPackageName]struct{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
|
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
|
||||||
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{})
|
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(httpPackage)
|
||||||
gfile.Import(reflectPackage)
|
gfile.Import(reflectPackage)
|
||||||
gfile.Import(stringsPackage)
|
gfile.Import(stringsPackage)
|
||||||
gfile.Import(microApiPackage)
|
|
||||||
gfile.Import(gorillaMuxPackage)
|
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("v := ", reflectPackage.Ident("ValueOf"), "(h)")
|
||||||
gfile.P("if v.NumMethod() < 1 {")
|
gfile.P("if v.NumMethod() < 1 {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("for _, ep := range eps {")
|
gfile.P("for _, ep := range eps {")
|
||||||
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
|
||||||
gfile.P("if idx < 1 || len(ep.Name) <= idx {")
|
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(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("name := ep.Name[idx+1:]")
|
gfile.P(`name := ep.Name[idx+1:]`)
|
||||||
gfile.P("m := v.MethodByName(name)")
|
gfile.P("m := v.MethodByName(name)")
|
||||||
gfile.P("if !m.IsValid() || m.IsZero() {")
|
gfile.P("if !m.IsValid() || m.IsZero() {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
|
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("if !ok {")
|
||||||
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
|
||||||
gfile.P("}")
|
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("}")
|
||||||
gfile.P("return nil")
|
gfile.P("return nil")
|
||||||
gfile.P("}")
|
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.P()
|
||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
|
||||||
|
|
||||||
if genClient {
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
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.P()
|
||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
|
||||||
if genClient {
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
}
|
}
|
||||||
// generate services
|
// generate services
|
||||||
for _, service := range file.Services {
|
for _, service := range file.Services {
|
||||||
|
g.generateServiceName(gfile, service)
|
||||||
g.generateServiceEndpoints(gfile, service)
|
g.generateServiceEndpoints(gfile, service)
|
||||||
if genClient {
|
if genClient {
|
||||||
g.generateServiceClientInterface(gfile, service)
|
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.P()
|
||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
|
||||||
if genClient {
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
}
|
}
|
||||||
|
63
util.go
63
util.go
@ -481,48 +481,16 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, servi
|
|||||||
serviceName := service.GoName
|
serviceName := service.GoName
|
||||||
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
||||||
gfile.P("type ", unexport(serviceName), " interface {")
|
gfile.P("type ", unexport(serviceName), " interface {")
|
||||||
var endpoints []*api_options.HttpRule
|
|
||||||
for _, method := range service.Methods {
|
for _, method := range service.Methods {
|
||||||
generateServerSignature(gfile, serviceName, method, true)
|
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("}")
|
||||||
gfile.P("type ", serviceName, " struct {")
|
gfile.P("type ", serviceName, " struct {")
|
||||||
gfile.P(unexport(serviceName))
|
gfile.P(unexport(serviceName))
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||||
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||||
for _, method := range service.Methods {
|
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
|
||||||
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("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
@ -863,9 +831,36 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
|
|||||||
gfile.P()
|
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
|
serviceName := service.GoName
|
||||||
gfile.P("var (")
|
gfile.P("var (")
|
||||||
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||||
gfile.P(")")
|
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")
|
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
|
||||||
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
|
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
|
||||||
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
|
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")
|
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata")
|
||||||
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
|
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
|
||||||
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
|
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
|
||||||
@ -22,5 +21,5 @@ var (
|
|||||||
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
|
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
|
||||||
timePackage = protogen.GoImportPath("time")
|
timePackage = protogen.GoImportPath("time")
|
||||||
deprecationComment = "// Deprecated: Do not use."
|
deprecationComment = "// Deprecated: Do not use."
|
||||||
versionComment = "v3.5.3"
|
versionComment = "v3.10.2"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user