3 Star 16 Fork 3

Gitee 极速下载 / go-tagexpr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/bytedance/go-tagexpr/
克隆/下载
func.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
package binding
import (
"errors"
"fmt"
"reflect"
"time"
)
// JSONUnmarshaler is the interface implemented by types
// that can unmarshal a JSON description of themselves.
type JSONUnmarshaler func(data []byte, v interface{}) error
var (
jsonUnmarshalFunc func(data []byte, v interface{}) error
)
// ResetJSONUnmarshaler reset the JSON Unmarshal function.
// NOTE: verifyingRequired is true if the required tag is supported.
func ResetJSONUnmarshaler(fn JSONUnmarshaler) {
jsonUnmarshalFunc = fn
}
var typeUnmarshalFuncs = make(map[reflect.Type]func(string, bool) (reflect.Value, error))
// MustRegTypeUnmarshal registers unmarshalor function of type.
// NOTE:
// panic if exist error.
func MustRegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error)) {
err := RegTypeUnmarshal(t, fn)
if err != nil {
panic(err)
}
}
// RegTypeUnmarshal registers unmarshalor function of type.
func RegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error)) error {
// check
switch t.Kind() {
case reflect.String, reflect.Bool,
reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8,
reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
return errors.New("registration type cannot be a basic type")
case reflect.Ptr:
return errors.New("registration type cannot be a pointer type")
}
// test
vv, err := fn("", true)
if err != nil {
return fmt.Errorf("test fail: %s", err)
}
if tt := vv.Type(); tt != t {
return fmt.Errorf("test fail: expect return value type is %s, but got %s", t.String(), tt.String())
}
typeUnmarshalFuncs[t] = fn
return nil
}
func init() {
MustRegTypeUnmarshal(reflect.TypeOf(time.Time{}), func(v string, emptyAsZero bool) (reflect.Value, error) {
if v == "" && emptyAsZero {
return reflect.ValueOf(time.Time{}), nil
}
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(t), nil
})
}
1
https://gitee.com/mirrors/go-tagexpr.git
git@gitee.com:mirrors/go-tagexpr.git
mirrors
go-tagexpr
go-tagexpr
v2.7.4

搜索帮助

53164aa7 5694891 3bd8fe86 5694891