optimize base code for binding/render

pull/196/head
Michael Li 2 years ago
parent 8e88500d8a
commit 3c14ced542

@ -4,7 +4,6 @@ package v1
import ( import (
"errors" "errors"
"net/http"
gin "github.com/gin-gonic/gin" gin "github.com/gin-gonic/gin"
) )
@ -92,10 +91,14 @@ func RegisterWebCoreServant(e *gin.Engine, s WebCore, b WebCoreBinding, r WebCor
type UnimplementedWebCoreServant struct{} type UnimplementedWebCoreServant struct{}
// UnimplementedWebCoreBinding can be embedded to have forward compatible implementations. // 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. // 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 { func (UnimplementedWebCoreServant) Chain() gin.HandlersChain {
return nil return nil
@ -119,26 +122,28 @@ func (UnimplementedWebCoreServant) Logout(c *gin.Context) error {
func (UnimplementedWebCoreServant) mustEmbedUnimplementedWebCoreServant() {} func (UnimplementedWebCoreServant) mustEmbedUnimplementedWebCoreServant() {}
func (UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, error) { func (b *UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, error) {
return nil, errors.New("method BindLogin not implemented") 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) { func (r *UnimplementedWebCoreRender) RenderIndex(c *gin.Context, err error) {
c.String(http.StatusInternalServerError, "method RenderLogout not implemented") r.RenderAny(c, nil, err)
} }
func (UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err error) { func (r *UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err error) {
c.String(http.StatusInternalServerError, "method RenderLogout not implemented") r.RenderAny(c, nil, err)
} }
func (UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err error) { func (r *UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err error) {
c.String(http.StatusInternalServerError, "method RenderLogin not implemented") r.RenderAny(c, data, err)
} }
func (UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err error) { func (r *UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err error) {
c.String(http.StatusInternalServerError, "method RenderLogout not implemented") r.RenderAny(c, nil, err)
} }
func (UnimplementedWebCoreRender) mustEmbedUnimplementedWebCoreRender() {} func (r *UnimplementedWebCoreRender) mustEmbedUnimplementedWebCoreRender() {}

@ -1,8 +1,13 @@
package core package core
import ( import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/xerror"
) )
type BaseServant struct { type BaseServant struct {
@ -24,3 +29,45 @@ func (BaseServant) userFrom(c *gin.Context) (*model.User, bool) {
} }
return nil, false 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)
}
}

@ -1,7 +1,7 @@
package localoss package localoss
import ( import (
"github.com/rocboss/paopao-ce/internal/servants/core/xerror" "github.com/rocboss/paopao-ce/pkg/xerror"
) )
var ( var (

@ -18,12 +18,12 @@ type webCoreSrv struct {
type webCoreBinding struct { type webCoreBinding struct {
core.BaseBinding core.BaseBinding
api.UnimplementedWebCoreBinding *api.UnimplementedWebCoreBinding
} }
type webCoreRender struct { type webCoreRender struct {
core.BaseRender core.BaseRender
api.UnimplementedWebCoreRender *api.UnimplementedWebCoreRender
} }
func newWebCoreSrv() api.WebCore { func newWebCoreSrv() api.WebCore {
@ -31,9 +31,17 @@ func newWebCoreSrv() api.WebCore {
} }
func newWebCoreBinding() api.WebCoreBinding { func newWebCoreBinding() api.WebCoreBinding {
return &webCoreBinding{} return &webCoreBinding{
UnimplementedWebCoreBinding: &api.UnimplementedWebCoreBinding{
BindAny: core.BindAny,
},
}
} }
func newWebCoreRender() api.WebCoreRender { func newWebCoreRender() api.WebCoreRender {
return &webCoreRender{} return &webCoreRender{
UnimplementedWebCoreRender: &api.UnimplementedWebCoreRender{
RenderAny: core.RenderAny,
},
}
} }

@ -1,7 +1,7 @@
package web package web
import ( import (
"github.com/rocboss/paopao-ce/internal/servants/core/xerror" "github.com/rocboss/paopao-ce/pkg/xerror"
) )
var ( var (

@ -3,6 +3,11 @@ package xerror
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
)
var (
codes = map[int]string{}
) )
type Error struct { type Error struct {
@ -11,7 +16,27 @@ type Error struct {
details []string 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 { func NewError(code int, msg string) *Error {
if _, ok := codes[code]; ok { if _, ok := codes[code]; ok {
Loading…
Cancel
Save