From 3c14ced54267284182e2898c824ee51909a50178 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 11 Nov 2022 22:08:10 +0800 Subject: [PATCH] optimize base code for binding/render --- internal/mirc/auto/api/v1/site.go | 35 ++++++++------ internal/servants/core/core.go | 47 +++++++++++++++++++ internal/servants/localoss/xerror.go | 2 +- internal/servants/web/core.go | 16 +++++-- internal/servants/web/xerror.go | 2 +- .../servants/core => pkg}/xerror/common.go | 0 .../servants/core => pkg}/xerror/xerror.go | 27 ++++++++++- 7 files changed, 107 insertions(+), 22 deletions(-) rename {internal/servants/core => pkg}/xerror/common.go (100%) rename {internal/servants/core => pkg}/xerror/xerror.go (78%) diff --git a/internal/mirc/auto/api/v1/site.go b/internal/mirc/auto/api/v1/site.go index 03c50281..f10eeb54 100644 --- a/internal/mirc/auto/api/v1/site.go +++ b/internal/mirc/auto/api/v1/site.go @@ -4,7 +4,6 @@ package v1 import ( "errors" - "net/http" gin "github.com/gin-gonic/gin" ) @@ -92,10 +91,14 @@ func RegisterWebCoreServant(e *gin.Engine, s WebCore, b WebCoreBinding, r WebCor type UnimplementedWebCoreServant struct{} // UnimplementedWebCoreBinding can be embedded to have forward compatible implementations. -type UnimplementedWebCoreBinding struct{} +type UnimplementedWebCoreBinding struct { + BindAny func(*gin.Context, any) error +} // UnimplementedWebCoreRender can be embedded to have forward compatible implementations. -type UnimplementedWebCoreRender struct{} +type UnimplementedWebCoreRender struct { + RenderAny func(*gin.Context, any, error) +} func (UnimplementedWebCoreServant) Chain() gin.HandlersChain { return nil @@ -119,26 +122,28 @@ func (UnimplementedWebCoreServant) Logout(c *gin.Context) error { func (UnimplementedWebCoreServant) mustEmbedUnimplementedWebCoreServant() {} -func (UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, error) { - return nil, errors.New("method BindLogin not implemented") +func (b *UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, error) { + obj := new(LoginReq) + err := b.BindAny(c, obj) + return obj, err } -func (UnimplementedWebCoreBinding) mustEmbedUnimplementedWebCoreBinding() {} +func (b *UnimplementedWebCoreBinding) mustEmbedUnimplementedWebCoreBinding() {} -func (UnimplementedWebCoreRender) RenderIndex(c *gin.Context, err error) { - c.String(http.StatusInternalServerError, "method RenderLogout not implemented") +func (r *UnimplementedWebCoreRender) RenderIndex(c *gin.Context, err error) { + r.RenderAny(c, nil, err) } -func (UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err error) { - c.String(http.StatusInternalServerError, "method RenderLogout not implemented") +func (r *UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err error) { + r.RenderAny(c, nil, err) } -func (UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err error) { - c.String(http.StatusInternalServerError, "method RenderLogin not implemented") +func (r *UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err error) { + r.RenderAny(c, data, err) } -func (UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err error) { - c.String(http.StatusInternalServerError, "method RenderLogout not implemented") +func (r *UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err error) { + r.RenderAny(c, nil, err) } -func (UnimplementedWebCoreRender) mustEmbedUnimplementedWebCoreRender() {} +func (r *UnimplementedWebCoreRender) mustEmbedUnimplementedWebCoreRender() {} diff --git a/internal/servants/core/core.go b/internal/servants/core/core.go index 6bedfa92..7e744dab 100644 --- a/internal/servants/core/core.go +++ b/internal/servants/core/core.go @@ -1,8 +1,13 @@ package core import ( + "fmt" + "net/http" + "os" + "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/internal/model" + "github.com/rocboss/paopao-ce/pkg/xerror" ) type BaseServant struct { @@ -24,3 +29,45 @@ func (BaseServant) userFrom(c *gin.Context) (*model.User, bool) { } return nil, false } + +func BindAny(c *gin.Context, obj any) error { + var errs xerror.ValidErrors + err := c.ShouldBind(obj) + if err != nil { + return fmt.Errorf("bind errs: %w", xerror.InvalidParams.WithDetails(errs.Error())) + } + return nil +} + +func RenderAny(c *gin.Context, data any, err error) { + if err == nil { + hostname, _ := os.Hostname() + if data == nil { + data = gin.H{ + "code": 0, + "msg": "success", + "tracehost": hostname, + } + } else { + data = gin.H{ + "code": 0, + "msg": "success", + "data": data, + "tracehost": hostname, + } + } + c.JSON(http.StatusOK, data) + } else { + // TODO: error process logic + resp := gin.H{"code": http.StatusInternalServerError, "msg": err.Error()} + // xerr := &xerror.Error{} + // if errors.As(err, xerr) { + // resp["code"], resp["msg"] = xerr.Code(), xerr.Msg() + // details := xerr.Details() + // if len(details) > 0 { + // resp["details"] = details + // } + // } + c.JSON(http.StatusInternalServerError, resp) + } +} diff --git a/internal/servants/localoss/xerror.go b/internal/servants/localoss/xerror.go index 3014a991..7b656b7d 100644 --- a/internal/servants/localoss/xerror.go +++ b/internal/servants/localoss/xerror.go @@ -1,7 +1,7 @@ package localoss import ( - "github.com/rocboss/paopao-ce/internal/servants/core/xerror" + "github.com/rocboss/paopao-ce/pkg/xerror" ) var ( diff --git a/internal/servants/web/core.go b/internal/servants/web/core.go index 701e9fb2..4a4ccd32 100644 --- a/internal/servants/web/core.go +++ b/internal/servants/web/core.go @@ -18,12 +18,12 @@ type webCoreSrv struct { type webCoreBinding struct { core.BaseBinding - api.UnimplementedWebCoreBinding + *api.UnimplementedWebCoreBinding } type webCoreRender struct { core.BaseRender - api.UnimplementedWebCoreRender + *api.UnimplementedWebCoreRender } func newWebCoreSrv() api.WebCore { @@ -31,9 +31,17 @@ func newWebCoreSrv() api.WebCore { } func newWebCoreBinding() api.WebCoreBinding { - return &webCoreBinding{} + return &webCoreBinding{ + UnimplementedWebCoreBinding: &api.UnimplementedWebCoreBinding{ + BindAny: core.BindAny, + }, + } } func newWebCoreRender() api.WebCoreRender { - return &webCoreRender{} + return &webCoreRender{ + UnimplementedWebCoreRender: &api.UnimplementedWebCoreRender{ + RenderAny: core.RenderAny, + }, + } } diff --git a/internal/servants/web/xerror.go b/internal/servants/web/xerror.go index 0398f648..305d1d0c 100644 --- a/internal/servants/web/xerror.go +++ b/internal/servants/web/xerror.go @@ -1,7 +1,7 @@ package web import ( - "github.com/rocboss/paopao-ce/internal/servants/core/xerror" + "github.com/rocboss/paopao-ce/pkg/xerror" ) var ( diff --git a/internal/servants/core/xerror/common.go b/pkg/xerror/common.go similarity index 100% rename from internal/servants/core/xerror/common.go rename to pkg/xerror/common.go diff --git a/internal/servants/core/xerror/xerror.go b/pkg/xerror/xerror.go similarity index 78% rename from internal/servants/core/xerror/xerror.go rename to pkg/xerror/xerror.go index b1b8a23a..5053b62f 100644 --- a/internal/servants/core/xerror/xerror.go +++ b/pkg/xerror/xerror.go @@ -3,6 +3,11 @@ package xerror import ( "fmt" "net/http" + "strings" +) + +var ( + codes = map[int]string{} ) type Error struct { @@ -11,7 +16,27 @@ type Error struct { details []string } -var codes = map[int]string{} +type ValidError struct { + Message string +} + +type ValidErrors []*ValidError + +func (v *ValidError) Error() string { + return v.Message +} + +func (v ValidErrors) Error() string { + return strings.Join(v.Errors(), ",") +} + +func (v ValidErrors) Errors() []string { + var errs []string + for _, err := range v { + errs = append(errs, err.Error()) + } + return errs +} func NewError(code int, msg string) *Error { if _, ok := codes[code]; ok {