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 (
"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() {}

@ -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)
}
}

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

@ -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,
},
}
}

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

@ -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 {
Loading…
Cancel
Save