diff --git a/main.go b/main.go index 8e301ad..9609397 100644 --- a/main.go +++ b/main.go @@ -42,25 +42,33 @@ func (u *Kit) Generate(plugin *protogen.Plugin) error { t.P(`"` + v + `"`) } t.P(")") - t.P(`func NewGin(app *gin.Engine,srv UserServer) {`) for _, s := range f.Services { + serverName := s.GoName + t.P(`func Register`, serverName, `Handler(app *gin.Engine,srv `, serverName, `Server) {`) for _, m := range s.Methods { method, path := u.getMethod(m) - t.P(`app.`, method, `("`, path, `",http`, m.GoName, `(srv))`) + if method == "" { + continue + } + t.P(`app.`, method, `("`, path, `",http`, m.GoName, `Handler(srv))`) } + t.P(`}`) } - t.P(`}`) for _, s := range f.Services { + serverName := s.GoName for _, m := range s.Methods { method, _ := u.getMethod(m) + if method == "" { + continue + } switch method { case method_get: - u.genGet(t, m) + u.genGet(serverName, t, m) case method_post: - u.genPost(t, m) + u.genPost(serverName, t, m) case method_delete: - u.genDelete(t, m) + u.genDelete(serverName, t, m) } } @@ -69,8 +77,8 @@ func (u *Kit) Generate(plugin *protogen.Plugin) error { return nil } -func (u *Kit) genGet(t *protogen.GeneratedFile, m *protogen.Method) { - t.P("func http", m.GoName, "(srv UserServer)func(c *gin.Context){") +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){") t.P(`return func(c *gin.Context) { post := `, m.Input.GoIdent.GoName, `{} resp := response.New(c) @@ -83,12 +91,12 @@ func (u *Kit) genGet(t *protogen.GeneratedFile, m *protogen.Method) { resp.Error(err) return } - c.JSON(200, result) + resp.Success(result) }`) t.P("}") } -func (u *Kit) genPost(t *protogen.GeneratedFile, m *protogen.Method) { - t.P("func http", m.GoName, "(srv UserServer)func(c *gin.Context){") +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){") t.P(`return func(c *gin.Context) { post := `, m.Input.GoIdent.GoName, `{} resp := response.New(c) @@ -101,12 +109,12 @@ func (u *Kit) genPost(t *protogen.GeneratedFile, m *protogen.Method) { resp.Error(err) return } - c.JSON(200, result) + resp.Success(result) }`) t.P("}") } -func (u *Kit) genDelete(t *protogen.GeneratedFile, m *protogen.Method) { - t.P("func http", m.GoName, "(srv UserServer)func(c *gin.Context){") +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){") t.P(`return func(c *gin.Context) { post := `, m.Input.GoIdent.GoName, `{} resp := response.New(c) @@ -119,7 +127,7 @@ func (u *Kit) genDelete(t *protogen.GeneratedFile, m *protogen.Method) { resp.Error(err) return } - c.JSON(200, result) + resp.Success(result) }`) t.P("}") } diff --git a/proto/user.proto b/proto/user.proto index 1a314b0..db67a9b 100644 --- a/proto/user.proto +++ b/proto/user.proto @@ -20,12 +20,7 @@ service user{ body:"*" }; } - rpc delete(loginRequest)returns(loginResponse){ - option (google.api.http) = { - delete: "/api/v1/user/delete", - body:"*" - }; - } + rpc delete(loginRequest)returns(loginResponse); } message loginRequest{ string username = 1 [(validate.rules).string = {min_len:4,max_len:10}]; diff --git a/proto/v1/user.gin.go b/proto/v1/user.gin.go index 5202888..50c069f 100644 --- a/proto/v1/user.gin.go +++ b/proto/v1/user.gin.go @@ -5,12 +5,11 @@ import ( "github.com/gin-gonic/gin" ) -func NewGin(app *gin.Engine, srv UserServer) { - app.GET("/api/v1/user/list", httpList(srv)) - app.POST("/api/v1/user/login", httpLogin(srv)) - app.DELETE("/api/v1/user/delete", httpDelete(srv)) +func RegisterUserHandler(app *gin.Engine, srv UserServer) { + app.GET("/api/v1/user/list", httpListHandler(srv)) + app.POST("/api/v1/user/login", httpLoginHandler(srv)) } -func httpList(srv UserServer) func(c *gin.Context) { +func httpListHandler(srv UserServer) func(c *gin.Context) { return func(c *gin.Context) { post := LoginRequest{} resp := response.New(c) @@ -23,10 +22,10 @@ func httpList(srv UserServer) func(c *gin.Context) { resp.Error(err) return } - c.JSON(200, result) + resp.Success(result) } } -func httpLogin(srv UserServer) func(c *gin.Context) { +func httpLoginHandler(srv UserServer) func(c *gin.Context) { return func(c *gin.Context) { post := LoginRequest{} resp := response.New(c) @@ -39,22 +38,6 @@ func httpLogin(srv UserServer) func(c *gin.Context) { resp.Error(err) return } - c.JSON(200, result) - } -} -func httpDelete(srv UserServer) func(c *gin.Context) { - return func(c *gin.Context) { - post := LoginRequest{} - resp := response.New(c) - if err := resp.BindJSON(&post); err != nil { - resp.Error(err) - return - } - result, err := srv.Delete(c, &post) - if err != nil { - resp.Error(err) - return - } - c.JSON(200, result) + resp.Success(result) } } diff --git a/proto/v1/user.pb.go b/proto/v1/user.pb.go index 8929304..5b13815 100644 --- a/proto/v1/user.pb.go +++ b/proto/v1/user.pb.go @@ -153,7 +153,7 @@ var file_user_proto_rawDesc = []byte{ 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x66, 0x69, 0x72, 0x73, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x25, 0x0a, 0x0d, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0xbe, 0x02, 0x0a, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0x9e, 0x02, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x62, 0x0a, 0x04, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x1e, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x77, 0x65, 0x62, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x63, @@ -167,13 +167,11 @@ var file_user_proto_rawDesc = []byte{ 0x75, 0x73, 0x65, 0x72, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1d, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x17, 0x22, 0x12, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x3a, 0x01, - 0x2a, 0x12, 0x69, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x1e, 0x2e, 0x63, 0x6f, + 0x2a, 0x12, 0x49, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x1e, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x77, 0x65, 0x62, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x77, 0x65, 0x62, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x6c, - 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1e, 0x82, 0xd3, - 0xe4, 0x93, 0x02, 0x18, 0x2a, 0x13, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, - 0x65, 0x72, 0x2f, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x3a, 0x01, 0x2a, 0x42, 0x09, 0x5a, 0x07, + 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x75, 0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } diff --git a/response/json.go b/response/json.go new file mode 100644 index 0000000..561de14 --- /dev/null +++ b/response/json.go @@ -0,0 +1,54 @@ +package response + +import ( + "encoding/json" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" + "reflect" +) + +var ( + // MarshalOptions is a configurable JSON format marshaller. + MarshalOptions = protojson.MarshalOptions{ + EmitUnpopulated: true, + } + // UnmarshalOptions is a configurable JSON format parser. + UnmarshalOptions = protojson.UnmarshalOptions{ + DiscardUnknown: true, + } +) + +type codec struct { +} + +func (codec) Marshal(v interface{}) ([]byte, error) { + switch m := v.(type) { + case json.Marshaler: + return m.MarshalJSON() + case proto.Message: + return MarshalOptions.Marshal(m) + default: + return json.Marshal(m) + } +} + +func (codec) Unmarshal(data []byte, v interface{}) error { + switch m := v.(type) { + case json.Unmarshaler: + return m.UnmarshalJSON(data) + case proto.Message: + return UnmarshalOptions.Unmarshal(data, m) + default: + rv := reflect.ValueOf(v) + for rv := rv; rv.Kind() == reflect.Ptr; { + if rv.IsNil() { + rv.Set(reflect.New(rv.Type().Elem())) + } + rv = rv.Elem() + } + if m, ok := reflect.Indirect(rv).Interface().(proto.Message); ok { + return UnmarshalOptions.Unmarshal(data, m) + } + return json.Unmarshal(data, m) + } +} diff --git a/response/response.go b/response/response.go index 1070792..aff96e5 100644 --- a/response/response.go +++ b/response/response.go @@ -8,6 +8,10 @@ import ( "net/http" ) +var ( + code codec +) + type result struct { ctx *gin.Context } @@ -72,7 +76,29 @@ type ValidateError interface { Reason() string ErrorName() string } +type MyRender struct { + Data any +} + +func (m *MyRender) Render(writer http.ResponseWriter) error { + jsonBytes, err := code.Marshal(m.Data) + if err != nil { + return err + } + _, err = writer.Write(jsonBytes) + return err +} + +func (m *MyRender) WriteContentType(w http.ResponseWriter) { + header := w.Header() + if val := header["Content-Type"]; len(val) == 0 { + header["Content-Type"] = []string{"application/json; charset=utf-8"} + } +} +func (s *result) Success(data any) { + s.ctx.Render(http.StatusOK, &MyRender{Data: data}) +} func (s *result) Result(httpCode int, reason string, message string, metadata map[string]string) { s.ctx.JSON(httpCode, gin.H{ "status": httpCode, diff --git a/run/main.go b/run/main.go index 24722d2..d4f7767 100644 --- a/run/main.go +++ b/run/main.go @@ -12,6 +12,6 @@ func main() { app.Use(func(c *gin.Context) { c.Set("userId", 11) }) - user.NewGin(app, &srv) + user.RegisterUserHandler(app, &srv) app.Run("localhost:8888") } diff --git a/service/service.go b/service/service.go index 2cc9356..5c965c2 100644 --- a/service/service.go +++ b/service/service.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "fmt" user "git.echinacities.com/mogfee/protoc-gen-kit/proto/v1" "git.echinacities.com/mogfee/protoc-gen-kit/xerrors" ) @@ -21,11 +20,11 @@ func (*UserService) Login(ctx context.Context, req *user.LoginRequest) (*user.Lo return &user.LoginResponse{Token: string(b)}, nil } func (*UserService) List(ctx context.Context, req *user.LoginRequest) (*user.LoginResponse, error) { - fmt.Println(ctx.Value("userId")) - return nil, xerrors.InternalServer("InternalServer", "B") + //fmt.Println(ctx.Value("userId")) + //return nil, xerrors.InternalServer("InternalServer", "B") - b, _ := json.Marshal(req) - return &user.LoginResponse{Token: string(b)}, nil + //b, _ := json.Marshal(req) + return &user.LoginResponse{Token: ""}, nil } func (*UserService) Delete(ctx context.Context, req *user.LoginRequest) (*user.LoginResponse, error) { return nil, errors.New("bad err")