diff --git a/Makefile b/Makefile index c14606f..6f036ce 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,16 @@ -kit: - go build -o ~/go/bin/protoc-gen-kit cmd/kit/main.go -gin-kit: - go build -o ~/go/bin/protoc-gen-gin-kit cmd/gin-kit/main.go -ts: - go build -o ~/go/bin/protoc-gen-ts cmd/ts/main.go all: make auth make kit make gin-kit make ts make gen +kit: + go build -o ~/go/bin/protoc-gen-kit cmd/kit/main.go +gin-kit: + go build -o ~/go/bin/protoc-gen-gin-kit cmd/gin-kit/main.go +ts: + go build -o ~/go/bin/protoc-gen-ts cmd/ts/main.go + testts: make ts gen: diff --git a/api/user_http.pb.go b/api/user_http.pb.go index 4aa8306..e8946e6 100644 --- a/api/user_http.pb.go +++ b/api/user_http.pb.go @@ -2,8 +2,8 @@ package user import ( "context" - "git.diulo.com/mogfee/kit/middleware/jwt" "git.diulo.com/mogfee/kit/transport/http" + "git.diulo.com/mogfee/kit/middleware/jwt" ) type UserHTTPServer interface { diff --git a/cmd/gin-kit/main.go b/cmd/gin-kit/main.go index 7d74add..f0fb73c 100644 --- a/cmd/gin-kit/main.go +++ b/cmd/gin-kit/main.go @@ -79,7 +79,8 @@ func (u *Kit) Generate(plugin *protogen.Plugin) error { u.genPost(serverName, t, m) case protogen2.METHOD_DELETE: u.genDelete(serverName, t, m) - + case protogen2.METHOD_PUT: + u.genPut(serverName, t, m) } } } @@ -162,3 +163,29 @@ func (u *Kit) genDelete(serverName string, t *protogen.GeneratedFile, m *protoge }`) t.P("}") } + +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("}") +} diff --git a/protogen/protogen.go b/protogen/protogen.go index 92833e0..0cf3d31 100644 --- a/protogen/protogen.go +++ b/protogen/protogen.go @@ -12,6 +12,7 @@ const ( METHOD_GET = "GET" METHOD_POST = "POST" METHOD_DELETE = "DELETE" + METHOD_PUT = "PUT" ) func GetProtoMethod(m *protogen.Method) (method string, path string) { @@ -26,6 +27,8 @@ func GetProtoMethod(m *protogen.Method) (method string, path string) { return METHOD_POST, vvv.Post } else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Delete); ok { return METHOD_DELETE, vvv.Delete + } else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Put); ok { + return METHOD_PUT, vvv.Put } } }