mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-26 20:11:11 +08:00
feat: Context bind handling default values
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/tag"
|
||||
)
|
||||
|
||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||
@@ -157,7 +158,26 @@ func (p *Context) Set(key string, val interface{}) {
|
||||
// Bind binds the request body into provided type `i`. The default binder
|
||||
// does it based on Content-Type header.
|
||||
func (p *Context) Bind(i interface{}) error {
|
||||
return p.EchoContext.Bind(i)
|
||||
|
||||
if err := p.EchoContext.Bind(i); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set tag defaults
|
||||
// type Example struct {
|
||||
// Page int `default:"1"`
|
||||
// Size int `default:"10"`
|
||||
// IsBool bool `default:"true"`
|
||||
// Keyword string `default:"hello world"`
|
||||
// Number float64 `default:"1.23"`
|
||||
// Status [3]int `default:"1,2"`
|
||||
// TimeRange []string `default:"12:30,13:30"`
|
||||
// Data map[string]interface{} `default:"age:18,name:zero"`
|
||||
// }
|
||||
// {Page:1 Size:10 Keyword:hello world Number:1.23 Status:[1 2 0] TimeRange:[12:30 13:30] Data:map[age:18 name:zero]}
|
||||
tag.SetDefaults(reflect.ValueOf(i).Elem())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates provided `i`. It is usually called after `Context#Bind()`.
|
||||
@@ -350,7 +370,26 @@ func (p *Context) ClientIP() string {
|
||||
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
|
||||
// If none of the content types above are matched, it will return a ErrUnprocessableEntity error
|
||||
func (p *Context) BodyParser(i interface{}) error {
|
||||
return p.EchoContext.Bind(i)
|
||||
|
||||
if err := p.EchoContext.Bind(i); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set tag defaults
|
||||
// type Example struct {
|
||||
// Page int `default:"1"`
|
||||
// Size int `default:"10"`
|
||||
// IsBool bool `default:"true"`
|
||||
// Keyword string `default:"hello world"`
|
||||
// Number float64 `default:"1.23"`
|
||||
// Status [3]int `default:"1,2"`
|
||||
// TimeRange []string `default:"12:30,13:30"`
|
||||
// Data map[string]interface{} `default:"age:18,name:zero"`
|
||||
// }
|
||||
// {Page:1 Size:10 Keyword:hello world Number:1.23 Status:[1 2 0] TimeRange:[12:30 13:30] Data:map[age:18 name:zero]}
|
||||
tag.SetDefaults(reflect.ValueOf(i).Elem())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取请求头数据
|
||||
|
@@ -22,7 +22,7 @@ const (
|
||||
AppName = "QuarkGo"
|
||||
|
||||
// 版本号
|
||||
Version = "2.4.0"
|
||||
Version = "2.4.1"
|
||||
|
||||
// 包名
|
||||
PkgName = "github.com/quarkcloudio/quark-go/v2"
|
||||
|
110
pkg/utils/tag/default.go
Normal file
110
pkg/utils/tag/default.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package tag
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 设置默认值
|
||||
func SetDefaults(value reflect.Value) {
|
||||
|
||||
// 解引用指针类型
|
||||
if value.Kind() == reflect.Ptr {
|
||||
if value.IsNil() {
|
||||
value.Set(reflect.New(value.Type().Elem()))
|
||||
}
|
||||
value = value.Elem()
|
||||
}
|
||||
|
||||
typ := value.Type()
|
||||
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
field := value.Field(i)
|
||||
// 嵌套结构体递归处理
|
||||
if field.Kind() == reflect.Struct {
|
||||
SetDefaults(field)
|
||||
}
|
||||
// 检查字段标签中存在default标签并且当前值是该字段类型的零值
|
||||
defaultValue := typ.Field(i).Tag.Get("default")
|
||||
if defaultValue != "" && reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()) {
|
||||
setValue(field, defaultValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setSliceFieldValue(field reflect.Value, defaultValue string) {
|
||||
|
||||
values := strings.Split(defaultValue, ",")
|
||||
slice := reflect.MakeSlice(field.Type(), len(values), len(values))
|
||||
|
||||
for key, value := range values {
|
||||
setValue(slice.Index(key), value)
|
||||
}
|
||||
field.Set(slice)
|
||||
}
|
||||
|
||||
func setArrayFieldValue(field reflect.Value, defaultValue string) {
|
||||
|
||||
for key, value := range strings.Split(defaultValue, ",") {
|
||||
if key >= field.Len() {
|
||||
continue
|
||||
}
|
||||
setValue(field.Index(key), value)
|
||||
}
|
||||
}
|
||||
|
||||
func setMapFieldValue(field reflect.Value, defaultValue string) {
|
||||
|
||||
mapType := field.Type()
|
||||
mapInstance := reflect.MakeMap(mapType)
|
||||
|
||||
for _, pair := range strings.Split(defaultValue, ",") {
|
||||
kv := strings.Split(pair, ":")
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
key := reflect.New(mapType.Key()).Elem()
|
||||
value := reflect.New(mapType.Elem()).Elem()
|
||||
|
||||
setValue(key, kv[0])
|
||||
setValue(value, kv[1])
|
||||
|
||||
mapInstance.SetMapIndex(key, value)
|
||||
}
|
||||
|
||||
field.Set(mapInstance)
|
||||
}
|
||||
|
||||
// 设置字段值
|
||||
func setValue(field reflect.Value, defaultValue string) {
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.String:
|
||||
field.SetString(defaultValue)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
if intValue, err := strconv.ParseInt(defaultValue, 10, 64); err == nil {
|
||||
field.SetInt(intValue)
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
if uintValue, err := strconv.ParseUint(defaultValue, 10, 64); err == nil {
|
||||
field.SetUint(uintValue)
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
if floatValue, err := strconv.ParseFloat(defaultValue, 64); err == nil {
|
||||
field.SetFloat(floatValue)
|
||||
}
|
||||
case reflect.Bool:
|
||||
if boolValue, err := strconv.ParseBool(defaultValue); err == nil {
|
||||
field.SetBool(boolValue)
|
||||
}
|
||||
case reflect.Array:
|
||||
setArrayFieldValue(field, defaultValue)
|
||||
case reflect.Slice:
|
||||
setSliceFieldValue(field, defaultValue)
|
||||
case reflect.Map:
|
||||
setMapFieldValue(field, defaultValue)
|
||||
default:
|
||||
field.Set(reflect.ValueOf(defaultValue))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user