You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.6 KiB

2 years ago
package main
import (
1 year ago
"flag"
2 years ago
"fmt"
2 years ago
protogen2 "git.diulo.com/mogfee/kit/protogen"
"google.golang.org/protobuf/compiler/protogen"
2 years ago
"google.golang.org/protobuf/types/descriptorpb"
"strings"
2 years ago
)
func main() {
u := &Kit{
imports: map[string]string{},
}
1 year ago
var flags flag.FlagSet
flags.StringVar(&u.Prefix, "prefix", "", "API path prefix")
protogen.Options{
ParamFunc: flags.Set,
}.Run(u.Generate)
2 years ago
protogen.Options{}.Run(u.Generate)
}
type Kit struct {
1 year ago
Prefix string
2 years ago
imports map[string]string
}
func (u *Kit) addImports(imp string) {
u.imports[imp] = imp
}
2 years ago
func (u *Kit) packageName(f *protogen.File) string {
a := f.Desc.Options().(*descriptorpb.FileOptions)
p := strings.Split(*a.GoPackage, ";")
if len(p) == 2 {
return p[1]
}
return fmt.Sprintf("%s", f.Desc.Name())
}
2 years ago
func (u *Kit) Generate(plugin *protogen.Plugin) error {
if len(plugin.Files) < 1 {
return nil
}
u.addImports("context")
2 years ago
u.addImports("git.diulo.com/mogfee/kit/middleware")
u.addImports("git.diulo.com/mogfee/kit/errors")
u.addImports("git.diulo.com/mogfee/kit/response")
u.addImports("github.com/gin-gonic/gin")
2 years ago
for _, f := range plugin.Files {
if len(f.Services) == 0 {
continue
}
2 years ago
fname := f.GeneratedFilenamePrefix + ".http.go"
2 years ago
t := plugin.NewGeneratedFile(fname, f.GoImportPath)
2 years ago
t.P("package ", u.packageName(f))
2 years ago
t.P("import (")
for _, v := range u.imports {
t.P(`"` + v + `"`)
}
t.P(")")
for _, s := range f.Services {
serverName := s.GoName
2 years ago
t.P(`func Register`, serverName, `Handler(app *gin.Engine,srv `, serverName, `Server,m ...middleware.Middleware) {`)
2 years ago
for _, m := range s.Methods {
method, path := protogen2.GetProtoMethod(m)
if method == "" {
continue
}
1 year ago
if u.Prefix != "" {
path = u.Prefix + path
}
2 years ago
t.P(`app.`, method, `("`, path, `",http`, m.GoName, `Handler(srv,m...))`)
2 years ago
}
t.P(`}`)
}
for _, s := range f.Services {
2 years ago
serverName := s.GoName
2 years ago
for _, m := range s.Methods {
method, _ := protogen2.GetProtoMethod(m)
if method == "" {
continue
}
2 years ago
switch method {
case protogen2.METHOD_GET:
u.genGet(serverName, t, m)
case protogen2.METHOD_POST:
u.genPost(serverName, t, m)
case protogen2.METHOD_DELETE:
u.genDelete(serverName, t, m)
1 year ago
case protogen2.METHOD_PUT:
u.genPut(serverName, t, m)
2 years ago
}
2 years ago
}
}
}
return nil
}
2 years ago
func (u *Kit) genGet(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
t.P(`return func(c *gin.Context) {
var post `, m.Input.GoIdent.GoName, `
resp := response.New(c)
if err := resp.BindQuery(&post); err != nil {
resp.Error(err)
return
}
h := func(ctx context.Context, a any) (any, error) {
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
2 years ago
resp.Error(err)
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
2 years ago
}
2 years ago
}`)
t.P("}")
}
func (u *Kit) genPost(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
t.P(`return func(c *gin.Context) {
var post `, m.Input.GoIdent.GoName, `
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
h := func(ctx context.Context, a any) (any, error) {
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
if err != nil {
resp.Error(err)
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
}
}`)
t.P("}")
}
func (u *Kit) genDelete(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
t.P(`return func(c *gin.Context) {
var post `, m.Input.GoIdent.GoName, `
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
h := func(ctx context.Context, a any) (any, error) {
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
if err != nil {
resp.Error(err)
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
}
}`)
t.P("}")
2 years ago
}
1 year ago
func (u *Kit) genPut(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
t.P(`return func(c *gin.Context) {
var post `, m.Input.GoIdent.GoName, `
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
h := func(ctx context.Context, a any) (any, error) {
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
if err != nil {
resp.Error(err)
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
}
}`)
t.P("}")
}