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.

153 lines
4.2 KiB

2 years ago
package main
import (
2 years ago
protogen2 "git.diulo.com/mogfee/protoc-gen-kit/pkg/protogen"
2 years ago
"google.golang.org/protobuf/compiler/protogen"
)
func main() {
2 years ago
u := &Kit{
2 years ago
imports: map[string]string{},
}
protogen.Options{}.Run(u.Generate)
}
2 years ago
type Kit struct {
2 years ago
imports map[string]string
}
2 years ago
func (u *Kit) addImports(imp string) {
2 years ago
u.imports[imp] = imp
}
2 years ago
func (u *Kit) Generate(plugin *protogen.Plugin) error {
2 years ago
if len(plugin.Files) < 1 {
return nil
}
2 years ago
u.addImports("context")
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/middleware")
2 years ago
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/pkg/response")
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/pkg/xerrors")
2 years ago
u.addImports("github.com/gin-gonic/gin")
for _, f := range plugin.Files {
if len(f.Services) == 0 {
continue
}
fname := f.GeneratedFilenamePrefix + ".gin.go"
t := plugin.NewGeneratedFile(fname, f.GoImportPath)
t.P("package " + f.Desc.Name())
t.P("import (")
for _, v := range u.imports {
t.P(`"` + v + `"`)
}
t.P(")")
for _, s := range f.Services {
2 years ago
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 {
2 years ago
method, path := protogen2.GetProtoMethod(m)
2 years ago
if method == "" {
continue
}
2 years ago
t.P(`app.`, method, `("`, path, `",http`, m.GoName, `Handler(srv,m...))`)
2 years ago
}
2 years ago
t.P(`}`)
2 years ago
}
for _, s := range f.Services {
2 years ago
serverName := s.GoName
2 years ago
for _, m := range s.Methods {
2 years ago
method, _ := protogen2.GetProtoMethod(m)
2 years ago
if method == "" {
continue
}
2 years ago
switch method {
2 years ago
case protogen2.METHOD_GET:
2 years ago
u.genGet(serverName, t, m)
2 years ago
case protogen2.METHOD_POST:
2 years ago
u.genPost(serverName, t, m)
2 years ago
case protogen2.METHOD_DELETE:
2 years ago
u.genDelete(serverName, t, m)
2 years ago
}
}
}
}
return nil
}
2 years ago
func (u *Kit) genGet(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
2 years ago
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
var post `, m.Input.GoIdent.GoName, `
2 years ago
resp := response.New(c)
if err := resp.BindQuery(&post); err != nil {
resp.Error(err)
return
}
2 years ago
h := func(ctx context.Context, a any) (any, error) {
2 years ago
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
2 years ago
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
resp.Error(err)
2 years ago
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(xerrors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
2 years ago
}
2 years ago
}`)
2 years ago
t.P("}")
}
2 years ago
func (u *Kit) genPost(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
2 years ago
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
var post `, m.Input.GoIdent.GoName, `
2 years ago
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
2 years ago
h := func(ctx context.Context, a any) (any, error) {
2 years ago
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
2 years ago
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
resp.Error(err)
2 years ago
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(xerrors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
2 years ago
}
2 years ago
}`)
2 years ago
t.P("}")
}
2 years ago
func (u *Kit) genDelete(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
2 years ago
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
var post `, m.Input.GoIdent.GoName, `
2 years ago
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
2 years ago
h := func(ctx context.Context, a any) (any, error) {
2 years ago
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
2 years ago
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
2 years ago
if err != nil {
resp.Error(err)
2 years ago
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(xerrors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
2 years ago
}
2 years ago
}`)
2 years ago
t.P("}")
}