github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/is.go (about)

     1  package ztype
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/sohaha/zlsgo/zreflect"
     7  )
     8  
     9  // IsByte Is []byte
    10  func IsByte(v interface{}) bool {
    11  	return GetType(v) == "[]byte"
    12  }
    13  
    14  // IsString Is String
    15  func IsString(v interface{}) bool {
    16  	return GetType(v) == "string"
    17  }
    18  
    19  // IsBool Is Bool
    20  func IsBool(v interface{}) bool {
    21  	return GetType(v) == "bool"
    22  }
    23  
    24  // IsFloat64 Is float64
    25  func IsFloat64(v interface{}) bool {
    26  	return GetType(v) == "float64"
    27  }
    28  
    29  // IsFloat32 Is float32
    30  func IsFloat32(v interface{}) bool {
    31  	return GetType(v) == "float32"
    32  }
    33  
    34  // IsUint64 Is uint64
    35  func IsUint64(v interface{}) bool {
    36  	return GetType(v) == "uint64"
    37  }
    38  
    39  // IsUint32 Is uint32
    40  func IsUint32(v interface{}) bool {
    41  	return GetType(v) == "uint32"
    42  }
    43  
    44  // IsUint16 Is uint16
    45  func IsUint16(v interface{}) bool {
    46  	return GetType(v) == "uint16"
    47  }
    48  
    49  // IsUint8 Is uint8
    50  func IsUint8(v interface{}) bool {
    51  	return GetType(v) == "uint8"
    52  }
    53  
    54  // IsUint Is uint
    55  func IsUint(v interface{}) bool {
    56  	return GetType(v) == "uint"
    57  }
    58  
    59  // IsInt64 Is int64
    60  func IsInt64(v interface{}) bool {
    61  	return GetType(v) == "int64"
    62  }
    63  
    64  // IsInt32 Is int32
    65  func IsInt32(v interface{}) bool {
    66  	return GetType(v) == "int32"
    67  }
    68  
    69  // IsInt16 Is int16
    70  func IsInt16(v interface{}) bool {
    71  	return GetType(v) == "int16"
    72  }
    73  
    74  // IsInt8 Is int8
    75  func IsInt8(v interface{}) bool {
    76  	return GetType(v) == "int8"
    77  }
    78  
    79  // IsInt Is int
    80  func IsInt(v interface{}) bool {
    81  	return GetType(v) == "int"
    82  }
    83  
    84  // IsStruct Is Struct
    85  func IsStruct(v interface{}) bool {
    86  	r := reflectPtr(zreflect.ValueOf(v))
    87  	return r.Kind() == reflect.Struct
    88  }
    89  
    90  // IsInterface Is interface{}
    91  func IsInterface(v interface{}) bool {
    92  	r := reflectPtr(zreflect.ValueOf(v))
    93  	return r.Kind() == reflect.Invalid
    94  }
    95  
    96  func IsEmpty(value interface{}) bool {
    97  	if value == nil {
    98  		return true
    99  	}
   100  	switch value := value.(type) {
   101  	case int:
   102  		return value == 0
   103  	case int8:
   104  		return value == 0
   105  	case int16:
   106  		return value == 0
   107  	case int32:
   108  		return value == 0
   109  	case int64:
   110  		return value == 0
   111  	case uint:
   112  		return value == 0
   113  	case uint8:
   114  		return value == 0
   115  	case uint16:
   116  		return value == 0
   117  	case uint32:
   118  		return value == 0
   119  	case uint64:
   120  		return value == 0
   121  	case float32:
   122  		return value == 0
   123  	case float64:
   124  		return value == 0
   125  	case bool:
   126  		return !value
   127  	case string:
   128  		return value == ""
   129  	case []byte:
   130  		return len(value) == 0
   131  	default:
   132  		// Finally using reflect.
   133  		rv := zreflect.ValueOf(value)
   134  		switch rv.Kind() {
   135  		case reflect.Chan,
   136  			reflect.Map,
   137  			reflect.Slice,
   138  			reflect.Array:
   139  			return rv.Len() == 0
   140  
   141  		case reflect.Func,
   142  			reflect.Ptr,
   143  			reflect.Interface,
   144  			reflect.UnsafePointer:
   145  			if rv.IsNil() {
   146  				return true
   147  			}
   148  		}
   149  	}
   150  	return false
   151  }