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.
 
 
 

152 lines
4.2 KiB

package main
import (
protogen2 "git.diulo.com/mogfee/protoc-gen-kit/pkg/protogen"
"google.golang.org/protobuf/compiler/protogen"
)
func main() {
u := &Kit{
imports: map[string]string{},
}
protogen.Options{}.Run(u.Generate)
}
type Kit struct {
imports map[string]string
}
func (u *Kit) addImports(imp string) {
u.imports[imp] = imp
}
func (u *Kit) Generate(plugin *protogen.Plugin) error {
if len(plugin.Files) < 1 {
return nil
}
u.addImports("context")
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/middleware")
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/pkg/response")
u.addImports("git.diulo.com/mogfee/protoc-gen-kit/pkg/xerrors")
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 {
serverName := s.GoName
t.P(`func Register`, serverName, `Handler(app *gin.Engine,srv `, serverName, `Server,m ...middleware.Middleware) {`)
for _, m := range s.Methods {
method, path := protogen2.GetProtoMethod(m)
if method == "" {
continue
}
t.P(`app.`, method, `("`, path, `",http`, m.GoName, `Handler(srv,m...))`)
}
t.P(`}`)
}
for _, s := range f.Services {
serverName := s.GoName
for _, m := range s.Methods {
method, _ := protogen2.GetProtoMethod(m)
if method == "" {
continue
}
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)
}
}
}
}
return nil
}
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)
if err != nil {
resp.Error(err)
} 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, `"))
}
}
}`)
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(xerrors.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(xerrors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
}
}`)
t.P("}")
}