From 6b58d8694dddeaf5a4e989f579ea8a9d652852f7 Mon Sep 17 00:00:00 2001 From: Inhere Date: Mon, 24 Jul 2023 21:28:53 +0800 Subject: [PATCH] :boom: update: replace interface{} to any in some files --- validate.go | 28 ++++++++++++++-------------- validate_test.go | 4 ++-- validating.go | 16 ++++++++-------- validating_test.go | 8 ++++---- validation.go | 42 +++++++++++++++++++++--------------------- validation_test.go | 12 ++++++------ 6 files changed, 55 insertions(+), 55 deletions(-) diff --git a/validate.go b/validate.go index c59b85c..93881cc 100644 --- a/validate.go +++ b/validate.go @@ -17,8 +17,8 @@ import ( "github.com/gookit/goutil/reflects" ) -// M is short name for map[string]interface{} -type M map[string]interface{} +// M is short name for map[string]any +type M map[string]any // MS is short name for map[string]string type MS map[string]string @@ -151,11 +151,11 @@ func newValidation(data DataFace) *Validation { // trans: StdTranslator, trans: NewTranslator(), // validated data - safeData: make(map[string]interface{}), + safeData: make(map[string]any), // validator names validators: make(map[string]int8), // filtered data - filteredData: make(map[string]interface{}), + filteredData: make(map[string]any), // default config StopOnError: gOpt.StopOnError, SkipOnEmpty: gOpt.SkipOnEmpty, @@ -192,7 +192,7 @@ func newValidation(data DataFace) *Validation { } // v.pool = &sync.Pool{ - // New: func() interface{} { + // New: func() any { // return &Validation{ // v: v, // } @@ -209,16 +209,16 @@ func newValidation(data DataFace) *Validation { // New create a Validation instance // data support: // - DataFace -// - M/map[string]interface{} +// - M/map[string]any // - SValues/url.Values/map[string][]string // - struct ptr -func New(data interface{}, scene ...string) *Validation { +func New(data any, scene ...string) *Validation { switch td := data.(type) { case DataFace: return NewValidation(td, scene...) case M: return FromMap(td).Create().SetScene(scene...) - case map[string]interface{}: + case map[string]any: return FromMap(td).Create().SetScene(scene...) case SValues: return FromURLValues(url.Values(td)).Create().SetScene(scene...) @@ -232,18 +232,18 @@ func New(data interface{}, scene ...string) *Validation { } // NewWithOptions new Validation with options -// func NewWithOptions(data interface{}, fn func(opt *GlobalOption)) *Validation { +// func NewWithOptions(data any, fn func(opt *GlobalOption)) *Validation { // fn(gOpt) // return New(data) // } // Map validation create -func Map(m map[string]interface{}, scene ...string) *Validation { +func Map(m map[string]any, scene ...string) *Validation { return FromMap(m).Create().SetScene(scene...) } // MapWithRules validation create and with rules -// func MapWithRules(m map[string]interface{}, rules MS) *Validation { +// func MapWithRules(m map[string]any, rules MS) *Validation { // return FromMap(m).Create().StringRules(rules) // } @@ -253,7 +253,7 @@ func JSON(s string, scene ...string) *Validation { } // Struct validation create -func Struct(s interface{}, scene ...string) *Validation { +func Struct(s any, scene ...string) *Validation { return mustNewValidation(FromStruct(s)).SetScene(scene...) } @@ -278,7 +278,7 @@ func mustNewValidation(d DataFace, err error) *Validation { *************************************************************/ // FromMap build data instance. -func FromMap(m map[string]interface{}) *MapData { +func FromMap(m map[string]any) *MapData { data := &MapData{} if m != nil { data.Map = m @@ -294,7 +294,7 @@ func FromJSON(s string) (*MapData, error) { // FromJSONBytes string build data instance. func FromJSONBytes(bs []byte) (*MapData, error) { - mp := map[string]interface{}{} + mp := map[string]any{} if err := json.Unmarshal(bs, &mp); err != nil { return nil, err } diff --git a/validate_test.go b/validate_test.go index e97eced..25c5bd1 100644 --- a/validate_test.go +++ b/validate_test.go @@ -50,7 +50,7 @@ func ExampleStruct() { func TestUtil_Func_valueToInt64(t *testing.T) { noErrTests := []struct { - val interface{} + val any strict bool want int64 }{ @@ -68,7 +68,7 @@ func TestUtil_Func_valueToInt64(t *testing.T) { func TestUtil_Func_getVariadicKind(t *testing.T) { noErrTests := []struct { - val interface{} + val any want reflect.Kind }{ {"invalid", reflect.Invalid}, diff --git a/validating.go b/validating.go index 96b2778..3a46fdf 100644 --- a/validating.go +++ b/validating.go @@ -69,7 +69,7 @@ func (v *Validation) Validate(scene ...string) bool { v.hasValidated = true if v.hasError { // clear safe data on error. - v.safeData = make(map[string]interface{}) + v.safeData = make(map[string]any) } return v.IsSuccess() @@ -238,7 +238,7 @@ type value struct { // // - field: the field name. eg: "name", "details.sub.*.field" // - name: the validator name. eg: "required", "min" -func (r *Rule) valueValidate(field, name string, val interface{}, v *Validation) (ok bool) { +func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok bool) { // "-" OR "safe" mark field value always is safe. if name == "-" || name == "safe" { return true @@ -348,7 +348,7 @@ func (r *Rule) valueValidate(field, name string, val interface{}, v *Validation) } // convert input field value type, is validator func first argument. -func convValAsFuncArg0Type(arg0Kind, valKind reflect.Kind, val interface{}) (interface{}, bool) { +func convValAsFuncArg0Type(arg0Kind, valKind reflect.Kind, val any) (any, bool) { // ak, err := basicKind(rftVal) bk, err := basicKindV2(valKind) if err != nil { @@ -363,7 +363,7 @@ func convValAsFuncArg0Type(arg0Kind, valKind reflect.Kind, val interface{}) (int return val, true } -func callValidator(v *Validation, fm *funcMeta, field string, val interface{}, args []interface{}) (ok bool) { +func callValidator(v *Validation, fm *funcMeta, field string, val any, args []any) (ok bool) { // use `switch` can avoid using reflection to call methods and improve speed // fm.name please see pkg var: validatorValues switch fm.name { @@ -441,7 +441,7 @@ func callValidator(v *Validation, fm *funcMeta, field string, val interface{}, a } // convert args data type -func convertArgsType(v *Validation, fm *funcMeta, field string, args []interface{}) (ok bool) { +func convertArgsType(v *Validation, fm *funcMeta, field string, args []any) (ok bool) { if len(args) == 0 { return true } @@ -457,7 +457,7 @@ func convertArgsType(v *Validation, fm *funcMeta, field string, args []interface lastTyp = getVariadicKind(ft.In(lastArgIndex)) } - // only one args and type is interface{} + // only one args and type is any if lastArgIndex == 1 && lastTyp == reflect.Interface { return true } @@ -524,12 +524,12 @@ func convertArgsType(v *Validation, fm *funcMeta, field string, args []interface return true } -func callValidatorValue(fv reflect.Value, val interface{}, args []interface{}) bool { +func callValidatorValue(fv reflect.Value, val any, args []any) bool { // build params for the validator func. argNum := len(args) argIn := make([]reflect.Value, argNum+1) - // if val is interface{}(nil): rftVal.IsValid()==false + // if val is any(nil): rftVal.IsValid()==false // if val is typed(nil): rftVal.IsValid()==true rftVal := reflect.ValueOf(val) // fix: #125 fv.Call() will panic on rftVal.Kind() is Invalid diff --git a/validating_test.go b/validating_test.go index 689b809..96ca57d 100644 --- a/validating_test.go +++ b/validating_test.go @@ -171,7 +171,7 @@ func TestVariadicArgs(t *testing.T) { v := New(M{ "age": 2, }) - v.AddValidator("checkAge", func(val interface{}, ints ...int) bool { + v.AddValidator("checkAge", func(val any, ints ...int) bool { return Enum(val, ints) }) v.StringRule("age", "required|checkAge:1,2,3,4") @@ -180,7 +180,7 @@ func TestVariadicArgs(t *testing.T) { v = New(M{ "age": 2, }) - v.AddValidator("checkAge", func(val interface{}, ints ...interface{}) bool { + v.AddValidator("checkAge", func(val any, ints ...any) bool { return Enum(val, ints) }) v.StringRule("age", "required|checkAge:1,2,3,4") @@ -263,7 +263,7 @@ func TestRequired_AllItemsPassed(t *testing.T) { } func TestRequired_MissingField(t *testing.T) { - m := map[string]interface{}{ + m := map[string]any{ "names": []string{"John", "Jane", "abc"}, "coding": []map[string]any{ { @@ -351,7 +351,7 @@ func TestValidate_sliceValue_1dotStar(t *testing.T) { } func TestRequired_MissingParentField(t *testing.T) { - m := map[string]interface{}{ + m := map[string]any{ "names": []string{"John", "Jane", "abc"}, "coding": []map[string]any{ { diff --git a/validation.go b/validation.go index f06cb7d..5a9f102 100644 --- a/validation.go +++ b/validation.go @@ -55,7 +55,7 @@ type Validation struct { // CachingRules bool // save user custom set default values - defValues map[string]interface{} + defValues map[string]any // mark has error occurs hasError bool // mark is filtered @@ -109,8 +109,8 @@ func (v *Validation) ResetResult() { v.hasFiltered = false v.hasValidated = false // result data - v.safeData = make(map[string]interface{}) - v.filteredData = make(map[string]interface{}) + v.safeData = make(map[string]any) + v.filteredData = make(map[string]any) } // Reset the Validation instance. @@ -184,7 +184,7 @@ func (v *Validation) SetScene(scene ...string) *Validation { *************************************************************/ // AddValidators to the Validation -func (v *Validation) AddValidators(m map[string]interface{}) *Validation { +func (v *Validation) AddValidators(m map[string]any) *Validation { for name, checkFunc := range m { v.AddValidator(name, checkFunc) } @@ -194,11 +194,11 @@ func (v *Validation) AddValidators(m map[string]interface{}) *Validation { // AddValidator to the Validation. checkFunc must return a bool. // Usage: // -// v.AddValidator("myFunc", func(val interface{}) bool { +// v.AddValidator("myFunc", func(val any) bool { // // do validate val ... // return true // }) -func (v *Validation) AddValidator(name string, checkFunc interface{}) *Validation { +func (v *Validation) AddValidator(name string, checkFunc any) *Validation { fv := checkValidatorFunc(name, checkFunc) v.validators[name] = 2 // custom @@ -353,7 +353,7 @@ func (v *Validation) AddError(field, validator, msg string) { } // AddErrorf add a formatted error message -func (v *Validation) AddErrorf(field, msgFormat string, args ...interface{}) { +func (v *Validation) AddErrorf(field, msgFormat string, args ...any) { v.AddError(field, validateError, fmt.Sprintf(msgFormat, args...)) } @@ -393,7 +393,7 @@ func (v *Validation) RawVal(key string) any { // try to get value by key. // // If v.data is StructData, will return zero check -func (v *Validation) tryGet(key string) (val interface{}, exist, zero bool) { +func (v *Validation) tryGet(key string) (val any, exist, zero bool) { if v.data == nil { return } @@ -424,7 +424,7 @@ func (v *Validation) tryGet(key string) (val interface{}, exist, zero bool) { } // Get value by key. -func (v *Validation) Get(key string) (val interface{}, exist bool) { +func (v *Validation) Get(key string) (val any, exist bool) { val, exist, _ = v.tryGet(key) return } @@ -432,7 +432,7 @@ func (v *Validation) Get(key string) (val interface{}, exist bool) { // GetWithDefault get field value by key. // // On not found, if it has default value, will return default-value. -func (v *Validation) GetWithDefault(key string) (val interface{}, exist, isDefault bool) { +func (v *Validation) GetWithDefault(key string) (val any, exist, isDefault bool) { var zero bool val, exist, zero = v.tryGet(key) if exist && !zero { @@ -448,12 +448,12 @@ func (v *Validation) GetWithDefault(key string) (val interface{}, exist, isDefau } // Filtered get filtered value by key -func (v *Validation) Filtered(key string) interface{} { +func (v *Validation) Filtered(key string) any { return v.filteredData[key] } // Safe get safe value by key -func (v *Validation) Safe(key string) (val interface{}, ok bool) { +func (v *Validation) Safe(key string) (val any, ok bool) { if v.data == nil { // check input data return } @@ -463,24 +463,24 @@ func (v *Validation) Safe(key string) (val interface{}, ok bool) { } // SafeVal get safe value by key -func (v *Validation) SafeVal(key string) interface{} { +func (v *Validation) SafeVal(key string) any { val, _ := v.Safe(key) return val } // GetSafe get safe value by key -func (v *Validation) GetSafe(key string) interface{} { +func (v *Validation) GetSafe(key string) any { val, _ := v.Safe(key) return val } // BindStruct binding safe data to an struct. -func (v *Validation) BindStruct(ptr interface{}) error { +func (v *Validation) BindStruct(ptr any) error { return v.BindSafeData(ptr) } // BindSafeData binding safe data to an struct. -func (v *Validation) BindSafeData(ptr interface{}) error { +func (v *Validation) BindSafeData(ptr any) error { if len(v.safeData) == 0 { // no safe data. return nil } @@ -495,7 +495,7 @@ func (v *Validation) BindSafeData(ptr interface{}) error { } // Set value by key -func (v *Validation) Set(field string, val interface{}) error { +func (v *Validation) Set(field string, val any) error { // check input data if v.data == nil { return ErrEmptyData @@ -506,7 +506,7 @@ func (v *Validation) Set(field string, val interface{}) error { } // only update set value by key for struct -func (v *Validation) updateValue(field string, val interface{}) (interface{}, error) { +func (v *Validation) updateValue(field string, val any) (any, error) { // data source is struct if v.data.Type() == sourceStruct { return v.data.Set(strings.TrimSuffix(field, ".*"), val) @@ -517,16 +517,16 @@ func (v *Validation) updateValue(field string, val interface{}) (interface{}, er } // SetDefValue set a default value of given field -func (v *Validation) SetDefValue(field string, val interface{}) { +func (v *Validation) SetDefValue(field string, val any) { if v.defValues == nil { - v.defValues = make(map[string]interface{}) + v.defValues = make(map[string]any) } v.defValues[field] = val } // GetDefValue get default value of the field -func (v *Validation) GetDefValue(field string) (interface{}, bool) { +func (v *Validation) GetDefValue(field string) (any, bool) { defVal, ok := v.defValues[field] return defVal, ok } diff --git a/validation_test.go b/validation_test.go index c0e8a49..5d21107 100644 --- a/validation_test.go +++ b/validation_test.go @@ -73,7 +73,7 @@ func TestValidation_max_invalidArg(t *testing.T) { // v.AddRule("age", "max", []string{"a"}) is.False(v.Validate()) // is.Contains(v.Errors.String(), "cannot convert invalid to arg#1(int64)") - // since 1.3.2+ max, min input params is update to interface{} + // since 1.3.2+ max, min input params is update to any is.Contains(v.Errors.String(), "max: age max value is ") } @@ -661,16 +661,16 @@ func TestAddValidator(t *testing.T) { AddValidator("myCheck", func() bool { return false }) }) is.Panics(func() { - AddValidator("myCheck", func(val interface{}) {}) + AddValidator("myCheck", func(val any) {}) }) is.Contains(Validators(), "min") - AddValidator("myCheck0", func(val interface{}) bool { + AddValidator("myCheck0", func(val any) bool { return true }) AddValidators(M{ - "myCheck1": func(val interface{}) bool { + "myCheck1": func(val any) bool { return true }, }) @@ -686,11 +686,11 @@ func TestAddValidator(t *testing.T) { v.AddValidator("myFunc2", func() {}) }) - v.AddValidator("myFunc3", func(val interface{}) bool { + v.AddValidator("myFunc3", func(val any) bool { return true }) v.AddValidators(M{ - "myFunc4": func(val interface{}) bool { + "myFunc4": func(val any) bool { return true }, })