Skip to content

Commit

Permalink
修复gin request 初始化时 验证器的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lshaofan committed Mar 10, 2023
1 parent b95761a commit 753c9b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
88 changes: 53 additions & 35 deletions application/dto/request/gin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package request

import (
"mime/multipart"
"reflect"
"strings"
"sync"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/zh"
Expand All @@ -9,81 +14,94 @@ import (
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"github.com/lshaofan/go-framework/application/dto/response"
"github.com/lshaofan/go-framework/infrastructure/dao"
"mime/multipart"
"reflect"
"strings"
)

type GinRequest struct {
c *gin.Context
c *gin.Context
}

func NewGinRequest(c *gin.Context) *GinRequest {
return &GinRequest{c: c}
}

type Validate struct {
uni *ut.UniversalTranslator
validate *validator.Validate
trans ut.Translator
}

func NewGinRequest(c *gin.Context) *GinRequest {
//注册翻译器
zh_ := zh.New()
uni := ut.New(zh_, zh_)
trans, _ := uni.GetTranslator("zh")
//获取gin的校验器
validate := binding.Validator.Engine().(*validator.Validate)
//注册翻译器
_ = zh_translations.RegisterDefaultTranslations(validate, trans)
return &GinRequest{
c: c,
uni: uni,
trans: trans,
validate: validate,
}
var (
validate *Validate
once sync.Once
)

func NewValidate() *Validate {
once.Do(func() {
validate = &Validate{}
//注册翻译器
zh_ := zh.New()
uni := ut.New(zh_, zh_)
trans, _ := uni.GetTranslator("zh")
//获取gin的校验器
val := binding.Validator.Engine().(*validator.Validate)
//注册翻译器
_ = zh_translations.RegisterDefaultTranslations(val, trans)
validate.validate = val
validate.uni = uni
validate.trans = trans
})
return validate
}

// TranslateToString tag 为结构体的tag定义的验证字段来源 如:json form uri query 返回第一条验证错误信息的字符串
func (g *GinRequest) TranslateToString(tag string, err error, obj interface{}) string {
v := NewValidate()
getObj := reflect.TypeOf(obj)
var result string
errors := err.(validator.ValidationErrors)
for _, err := range errors {
//TODO 如果翻译为空,就返回参数错误 ,按时不需要后续出现为空err情况再说
//if err.Translate(g.trans) == "" {
// result = "参数错误"
// return result
//}
if f, exist := getObj.Elem().FieldByName(err.Field()); exist && f.Tag.Get(tag) != "" {
result = f.Tag.Get(tag) + strings.Replace(err.Translate(g.trans), err.Field(), "", -1)
} else {
result = err.Translate(g.trans)
// 判断err 是否是 validator.ValidationErrors 类型
if _, ok := err.(validator.ValidationErrors); !ok {
result = err.Error()
} else {

errors := err.(validator.ValidationErrors)
for _, err := range errors {
if f, exist := getObj.Elem().FieldByName(err.Field()); exist && f.Tag.Get(tag) != "" {
result = f.Tag.Get(tag) + strings.Replace(err.Translate(v.trans), err.Field(), "", -1)
} else {
result = err.Translate(v.trans)
}
return result
}
return result
}

return result
}

// TranslateToMap Translate 翻译错误信息 tag 为结构体的tag定义的验证字段来源 如:json form uri query 返回所有验证错误信息的map
func (g *GinRequest) TranslateToMap(tag string, err error, obj interface{}) map[string][]string {
v := NewValidate()
getObj := reflect.TypeOf(obj)
var result = make(map[string][]string)
errors := err.(validator.ValidationErrors)

for _, err := range errors {
if f, exist := getObj.Elem().FieldByName(err.Field()); exist {

str := f.Tag.Get(tag) + strings.Replace(err.Translate(g.trans), err.Field(), "", -1)
str := f.Tag.Get(tag) + strings.Replace(err.Translate(v.trans), err.Field(), "", -1)
result[err.Field()] = append(result[err.Field()], str)
} else {
result[err.Field()] = append(result[err.Field()], err.Translate(g.trans))
result[err.Field()] = append(result[err.Field()], err.Translate(v.trans))
}
}
return result
}

// Translate TranslateToMap Translate 翻译错误信息
func (g *GinRequest) Translate(err error) map[string][]string {
v := NewValidate()
var result = make(map[string][]string)
errors := err.(validator.ValidationErrors)
for _, err := range errors {
result[err.Field()] = append(result[err.Field()], err.Translate(g.trans))
result[err.Field()] = append(result[err.Field()], err.Translate(v.trans))
}
return result
}
Expand Down
1 change: 0 additions & 1 deletion application/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func (s *Server) Init() error {
s.etcdConfig.DialTimeout = s.etcdDialTimeout
}
}

s.ctx = context.Background()
s.etcdClient, err = clientv3.New(*s.etcdConfig)
if err != nil {
Expand Down

0 comments on commit 753c9b3

Please sign in to comment.