Skip to content

Commit

Permalink
⬆️ chore: update dep to latest, replace some interface{} to any
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent b5f1569 commit 5dfa4ac
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 76 deletions.
55 changes: 31 additions & 24 deletions data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var (

type (
// MarshalFunc define
MarshalFunc func(v interface{}) ([]byte, error)
MarshalFunc func(v any) ([]byte, error)
// UnmarshalFunc define
UnmarshalFunc func(data []byte, ptr interface{}) error
UnmarshalFunc func(data []byte, ptr any) error
)

// DataFace data source interface definition
Expand All @@ -56,12 +56,12 @@ type (
// - struct
type DataFace interface {
Type() uint8
Src() interface{}
Get(key string) (val interface{}, exist bool)
Src() any
Get(key string) (val any, exist bool)
// TryGet value by key.
// if source data is struct, will return zero check
TryGet(key string) (val interface{}, exist, zero bool)
Set(field string, val interface{}) (interface{}, error)
TryGet(key string) (val any, exist, zero bool)
Set(field string, val any) (any, error)
// Create validation instance create func
Create(err ...error) *Validation
Validation(err ...error) *Validation
Expand All @@ -74,22 +74,22 @@ type DataFace interface {
// MapData definition
type MapData struct {
// Map the source map data
Map map[string]interface{}
Map map[string]any
// from reflect Map
value reflect.Value
// bodyJSON from the original JSON bytes/string.
// available for FromJSONBytes(), FormJSON().
bodyJSON []byte
// TODO map field value cache by key path
// cache map[string]interface{}
// cache map[string]any
}

/*************************************************************
* Map data operate
*************************************************************/

// Src get
func (d *MapData) Src() interface{} {
func (d *MapData) Src() any {
return d.Map
}

Expand All @@ -99,13 +99,13 @@ func (d *MapData) Type() uint8 {
}

// Set value by key
func (d *MapData) Set(field string, val interface{}) (interface{}, error) {
func (d *MapData) Set(field string, val any) (any, error) {
d.Map[field] = val
return val, nil
}

// Get value by key. support get value by path.
func (d *MapData) Get(field string) (interface{}, bool) {
func (d *MapData) Get(field string) (any, bool) {
// if fv, ok := d.fields[field]; ok {
// return fv, true
// }
Expand All @@ -114,7 +114,7 @@ func (d *MapData) Get(field string) (interface{}, bool) {
}

// TryGet value by key
func (d *MapData) TryGet(field string) (val interface{}, exist, zero bool) {
func (d *MapData) TryGet(field string) (val any, exist, zero bool) {
val, exist = maputil.GetByPath(field, d.Map)
return
}
Expand All @@ -134,7 +134,7 @@ func (d *MapData) Validation(err ...error) *Validation {

// BindJSON binds v to the JSON data in the request body.
// It calls json.Unmarshal and sets the value of v.
func (d *MapData) BindJSON(ptr interface{}) error {
func (d *MapData) BindJSON(ptr any) error {
if len(d.bodyJSON) == 0 {
return nil
}
Expand Down Expand Up @@ -187,7 +187,7 @@ type CustomMessagesFace interface {
// more struct tags define please see GlobalOption
type StructData struct {
// source struct data, from user setting
src interface{}
src any
// max depth for parse sub-struct. TODO WIP ...
// depth int
// from reflect source Struct
Expand All @@ -200,7 +200,7 @@ type StructData struct {
// cache field reflect value info. key is path. eg: top.sub
fieldValues map[string]reflect.Value
// TODO field reflect values cache
// fieldRftValues map[string]interface{}
// fieldRftValues map[string]any
// FilterTag name in the struct tags.
//
// see GlobalOption.FilterTag
Expand All @@ -226,7 +226,7 @@ var (
)

// Src get
func (d *StructData) Src() interface{} {
func (d *StructData) Src() any {
return d.src
}

Expand Down Expand Up @@ -494,19 +494,20 @@ func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg s
*************************************************************/

// Get value by field name. support get sub-value by path.
func (d *StructData) Get(field string) (val interface{}, exist bool) {
func (d *StructData) Get(field string) (val any, exist bool) {
val, exist, _ = d.TryGet(field)
return
}

// TryGet value by field name. support get sub-value by path.
func (d *StructData) TryGet(field string) (val interface{}, exist, zero bool) {
func (d *StructData) TryGet(field string) (val any, exist, zero bool) {
field = strutil.UpperFirst(field)
// try read from cache
if fv, ok := d.fieldValues[field]; ok {
return fv.Interface(), true, fv.IsZero()
}

// var isPtr bool
var fv reflect.Value
// want to get sub struct field.
if strings.IndexByte(field, '.') > 0 {
Expand Down Expand Up @@ -549,6 +550,7 @@ func (d *StructData) TryGet(field string) (val interface{}, exist, zero bool) {
return
}

// isPtr = fv.Kind() == reflect.Pointer
fv = removeValuePtr(fv)
if !fv.IsValid() {
return
Expand All @@ -574,7 +576,7 @@ func (d *StructData) TryGet(field string) (val interface{}, exist, zero bool) {
}

// is it a pointer
if fv.Kind() == reflect.Ptr {
if fv.Kind() == reflect.Pointer {
if fv.IsNil() { // fix: top-field is nil
return
}
Expand All @@ -593,6 +595,11 @@ func (d *StructData) TryGet(field string) (val interface{}, exist, zero bool) {

// cache field value info
d.fieldValues[field] = fv
// isZero := fv.IsZero()
// if isPtr {
// isZero = fv.Elem().IsZero()
// }

return fv.Interface(), true, fv.IsZero()
}
return
Expand All @@ -601,7 +608,7 @@ func (d *StructData) TryGet(field string) (val interface{}, exist, zero bool) {
// Set value by field name.
//
// Notice: `StructData.src` the incoming struct must be a pointer to set the value
func (d *StructData) Set(field string, val interface{}) (newVal interface{}, err error) {
func (d *StructData) Set(field string, val any) (newVal any, err error) {
field = strutil.UpperFirst(field)
if !d.HasField(field) { // field not found
return nil, ErrNoField
Expand Down Expand Up @@ -728,7 +735,7 @@ func newFormData() *FormData {
*************************************************************/

// Src data get
func (d *FormData) Src() interface{} {
func (d *FormData) Src() any {
return d.Form
}

Expand Down Expand Up @@ -797,7 +804,7 @@ func (d *FormData) Encode() string {
}

// Set sets the key to value. It replaces any existing values.
func (d *FormData) Set(field string, val interface{}) (newVal interface{}, err error) {
func (d *FormData) Set(field string, val any) (newVal any, err error) {
newVal = val
switch tpVal := val.(type) {
case string:
Expand All @@ -812,13 +819,13 @@ func (d *FormData) Set(field string, val interface{}) (newVal interface{}, err e
}

// TryGet value by key
func (d FormData) TryGet(key string) (val interface{}, exist, zero bool) {
func (d FormData) TryGet(key string) (val any, exist, zero bool) {
val, exist = d.Get(key)
return
}

// Get value by key
func (d FormData) Get(key string) (interface{}, bool) {
func (d FormData) Get(key string) (any, bool) {
// get form value
if vs, ok := d.Form[key]; ok && len(vs) > 0 {
return vs[0], true
Expand Down
10 changes: 5 additions & 5 deletions filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ var (
)

// AddFilters add global filters
func AddFilters(m map[string]interface{}) {
func AddFilters(m map[string]any) {
for name, filterFunc := range m {
AddFilter(name, filterFunc)
}
}

// AddFilter add global filter to the pkg.
func AddFilter(name string, filterFunc interface{}) {
func AddFilter(name string, filterFunc any) {
if filterValues == nil {
filterValues = make(map[string]reflect.Value)
}
Expand All @@ -36,14 +36,14 @@ func AddFilter(name string, filterFunc interface{}) {
*************************************************************/

// AddFilters to the Validation
func (v *Validation) AddFilters(m map[string]interface{}) {
func (v *Validation) AddFilters(m map[string]any) {
for name, filterFunc := range m {
v.AddFilter(name, filterFunc)
}
}

// AddFilter to the Validation.
func (v *Validation) AddFilter(name string, filterFunc interface{}) {
func (v *Validation) AddFilter(name string, filterFunc any) {
if v.filterValues == nil {
v.filterValues = make(map[string]reflect.Value)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *FilterRule) Fields() []string {
return r.fields
}

func callCustomFilter(fv reflect.Value, val interface{}, args []string) (interface{}, error) {
func callCustomFilter(fv reflect.Value, val any, args []string) (any, error) {
var rs []reflect.Value
if len(args) > 0 {
rs = CallByValue(fv, buildArgs(val, strings2Args(args))...)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.18

require (
github.com/gookit/filter v1.2.0
github.com/gookit/goutil v0.6.10
github.com/gookit/goutil v0.6.11
)

require (
github.com/gookit/color v1.5.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
)
15 changes: 8 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE=
github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE=
github.com/gookit/filter v1.2.0 h1:r7E01dHVkysb5WgzooiGsfblHGShEZCeGcyYM+5IpYU=
github.com/gookit/filter v1.2.0/go.mod h1:bXs9RcB4Blxwny970opiwABeIEqQ/gzOMmHBhKwBdms=
github.com/gookit/goutil v0.6.10 h1:iq7CXOf+fYLvrVAh3+ZoLgufGfK65TwbzE8NpnPGtyk=
github.com/gookit/goutil v0.6.10/go.mod h1:qqrPoX+Pm6YmxqqccgkNLPirTFX7UYMES1SK+fokqQU=
github.com/gookit/goutil v0.6.11 h1:615nIGRpQHFmgJ1oaA48q/z7bTx6KzMvHmKTsp21T2E=
github.com/gookit/goutil v0.6.11/go.mod h1:bU9ghaM9uW23x2+jB0WcywRsFGbIP0hvdIKYl2OMiog=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 comments on commit 5dfa4ac

Please sign in to comment.