Skip to content

Commit

Permalink
💥 update: replace interface{} to any in some files
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent 5dfa4ac commit 6b58d86
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
28 changes: 14 additions & 14 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -192,7 +192,7 @@ func newValidation(data DataFace) *Validation {
}

// v.pool = &sync.Pool{
// New: func() interface{} {
// New: func() any {
// return &Validation{
// v: v,
// }
Expand All @@ -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...)
Expand All @@ -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)
// }

Expand All @@ -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...)
}

Expand All @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ExampleStruct() {

func TestUtil_Func_valueToInt64(t *testing.T) {
noErrTests := []struct {
val interface{}
val any
strict bool
want int64
}{
Expand All @@ -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},
Expand Down
16 changes: 8 additions & 8 deletions validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions validating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
42 changes: 21 additions & 21 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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...))
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -424,15 +424,15 @@ 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
}

// 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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 6b58d86

Please sign in to comment.