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.

171 lines
4.3 KiB

2 years ago
package main
import (
"github.com/golang/protobuf/proto"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"log"
)
//go build && protoc --unknow_out=./proto --go_out=./proto/ --go-grpc_out=./proto proto/user.proto -I ./proto
// protoc --unknow_out=./proto proto/user.proto
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("git.echinacities.com/mogfee/protoc-gen-kit/response")
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
t.P(`func Register`, serverName, `Handler(app *gin.Engine,srv `, serverName, `Server) {`)
2 years ago
for _, m := range s.Methods {
method, path := u.getMethod(m)
2 years ago
if method == "" {
continue
}
t.P(`app.`, method, `("`, path, `",http`, m.GoName, `Handler(srv))`)
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 {
method, _ := u.getMethod(m)
2 years ago
if method == "" {
continue
}
2 years ago
switch method {
case method_get:
2 years ago
u.genGet(serverName, t, m)
2 years ago
case method_post:
2 years ago
u.genPost(serverName, t, m)
2 years ago
case 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) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
post := `, m.Input.GoIdent.GoName, `{}
resp := response.New(c)
if err := resp.BindQuery(&post); err != nil {
resp.Error(err)
return
}
result, err := srv.`, m.GoName, `(c, &post)
if err != nil {
resp.Error(err)
return
}
2 years ago
resp.Success(result)
2 years ago
}`)
2 years ago
t.P("}")
}
2 years ago
func (u *Kit) genPost(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
post := `, m.Input.GoIdent.GoName, `{}
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
result, err := srv.`, m.GoName, `(c, &post)
if err != nil {
resp.Error(err)
return
}
2 years ago
resp.Success(result)
2 years ago
}`)
2 years ago
t.P("}")
}
2 years ago
func (u *Kit) genDelete(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server)func(c *gin.Context){")
2 years ago
t.P(`return func(c *gin.Context) {
2 years ago
post := `, m.Input.GoIdent.GoName, `{}
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
result, err := srv.`, m.GoName, `(c, &post)
if err != nil {
resp.Error(err)
return
}
2 years ago
resp.Success(result)
2 years ago
}`)
2 years ago
t.P("}")
}
2 years ago
//func (Kit) getAuth(m *protogen.Method) *auth.AuthInfo {
// if op, ok := m.Desc.Options().(*descriptorpb.MethodOptions); ok {
// if opts, err := proto.GetExtension(op, auth.E_Auth); err != nil {
// log.Println(err)
// } else {
// if vv, ok := opts.(*auth.AuthInfo); ok {
// return vv
// }
// }
// }
// return &auth.AuthInfo{}
//}
2 years ago
func (Kit) getMethod(m *protogen.Method) (method string, path string) {
2 years ago
if op, ok := m.Desc.Options().(*descriptorpb.MethodOptions); ok {
if opts, err := proto.GetExtension(op, annotations.E_Http); err != nil {
log.Println(err)
} else {
if vv, ok := opts.(*annotations.HttpRule); ok {
if vvv, ok := vv.Pattern.(*annotations.HttpRule_Get); ok {
return method_get, vvv.Get
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Post); ok {
return method_post, vvv.Post
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Delete); ok {
return method_delete, vvv.Delete
}
}
}
}
return "", ""
}
const (
method_get = "GET"
method_post = "POST"
method_delete = "DELETE"
)