github.com/avicd/go-utilx@v0.1.0/refx/assert.go (about)

     1  package refx
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  func IsAnyOf(vl any, kinds ...reflect.Kind) bool {
     8  	if len(kinds) > 0 {
     9  		valKind := IndirectKind(vl)
    10  		for _, kind := range kinds {
    11  			switch kind {
    12  			case reflect.Pointer:
    13  				if IsPointer(vl) {
    14  					return true
    15  				}
    16  			case reflect.Interface:
    17  				if IsInterface(vl) {
    18  					return true
    19  				}
    20  			default:
    21  				if valKind == kind {
    22  					return true
    23  				}
    24  			}
    25  		}
    26  	}
    27  	return false
    28  }
    29  
    30  func IsInterface(vl any) bool {
    31  	return KindOf(vl) == reflect.Interface
    32  }
    33  
    34  func IsPointer(vl any) bool {
    35  	return KindOf(vl) == reflect.Pointer
    36  }
    37  
    38  func IsNil(vl any) bool {
    39  	kind := KindOf(vl)
    40  	switch kind {
    41  	case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
    42  		return ValueOf(vl).IsNil()
    43  	}
    44  	return kind == reflect.Invalid
    45  }
    46  
    47  func IsBasic(vl any) bool {
    48  	kind := IndirectKind(vl)
    49  	switch kind {
    50  	case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
    51  		return false
    52  	}
    53  	return true
    54  }
    55  
    56  func IsError(vl any) bool {
    57  	switch vl.(type) {
    58  	case error:
    59  		return true
    60  	default:
    61  		ref := TypeOf(func(err error) {})
    62  		if TypeOf(vl) == ref.In(0) {
    63  			return true
    64  		}
    65  	}
    66  	return false
    67  }
    68  
    69  func IsList(vl any) bool {
    70  	kind := IndirectKind(vl)
    71  	switch kind {
    72  	case reflect.Slice, reflect.Array:
    73  		return true
    74  	}
    75  	return false
    76  }
    77  
    78  func IsInteger(vl any) bool {
    79  	kind := IndirectKind(vl)
    80  	return kind >= reflect.Int && kind <= reflect.Int64
    81  }
    82  
    83  func IsUInteger(vl any) bool {
    84  	kind := IndirectKind(vl)
    85  	return kind >= reflect.Uint && kind <= reflect.Uint64
    86  }
    87  
    88  func IsGeneralInt(vl any) bool {
    89  	kind := IndirectKind(vl)
    90  	return kind >= reflect.Int && kind <= reflect.Uint64
    91  }
    92  
    93  func IsFloat(vl any) bool {
    94  	kind := IndirectKind(vl)
    95  	return kind >= reflect.Float32 && kind <= reflect.Float64
    96  }
    97  
    98  func IsNumber(vl any) bool {
    99  	kind := IndirectKind(vl)
   100  	return kind >= reflect.Int && kind <= reflect.Uint64 ||
   101  		kind >= reflect.Float32 && kind <= reflect.Float64
   102  }
   103  
   104  func IsInvalid(vl any) bool {
   105  	return KindOf(vl) == reflect.Invalid
   106  }
   107  
   108  func IsBool(vl any) bool {
   109  	return IndirectKind(vl) == reflect.Bool
   110  }
   111  func IsInt(vl any) bool {
   112  	return IndirectKind(vl) == reflect.Int
   113  }
   114  func IsInt8(vl any) bool {
   115  	return IndirectKind(vl) == reflect.Int8
   116  }
   117  func IsInt16(vl any) bool {
   118  	return IndirectKind(vl) == reflect.Int16
   119  }
   120  func IsInt32(vl any) bool {
   121  	return IndirectKind(vl) == reflect.Int32
   122  }
   123  func IsInt64(vl any) bool {
   124  	return IndirectKind(vl) == reflect.Int64
   125  }
   126  func IsUint(vl any) bool {
   127  	return IndirectKind(vl) == reflect.Uint
   128  }
   129  func IsUint8(vl any) bool {
   130  	return IndirectKind(vl) == reflect.Uint8
   131  }
   132  func IsUint16(vl any) bool {
   133  	return IndirectKind(vl) == reflect.Uint16
   134  }
   135  func IsUint32(vl any) bool {
   136  	return IndirectKind(vl) == reflect.Uint32
   137  }
   138  func IsUint64(vl any) bool {
   139  	return IndirectKind(vl) == reflect.Uint64
   140  }
   141  func IsUintptr(vl any) bool {
   142  	return IndirectKind(vl) == reflect.Uintptr
   143  }
   144  func IsFloat32(vl any) bool {
   145  	return IndirectKind(vl) == reflect.Float32
   146  }
   147  func IsFloat64(vl any) bool {
   148  	return IndirectKind(vl) == reflect.Float64
   149  }
   150  func IsComplex64(vl any) bool {
   151  	return IndirectKind(vl) == reflect.Complex64
   152  }
   153  func IsComplex128(vl any) bool {
   154  	return IndirectKind(vl) == reflect.Complex128
   155  }
   156  func IsArray(vl any) bool {
   157  	return IndirectKind(vl) == reflect.Array
   158  }
   159  func IsChan(vl any) bool {
   160  	return IndirectKind(vl) == reflect.Chan
   161  }
   162  func IsFunc(vl any) bool {
   163  	return IndirectKind(vl) == reflect.Func
   164  }
   165  func IsMap(vl any) bool {
   166  	return IndirectKind(vl) == reflect.Map
   167  }
   168  func IsSlice(vl any) bool {
   169  	return IndirectKind(vl) == reflect.Slice
   170  }
   171  func IsString(vl any) bool {
   172  	return IndirectKind(vl) == reflect.String
   173  }
   174  func IsStruct(vl any) bool {
   175  	return IndirectKind(vl) == reflect.Struct
   176  }
   177  func IsUnsafePointer(vl any) bool {
   178  	return IndirectKind(vl) == reflect.UnsafePointer
   179  }