github.com/gogf/gf/v2@v2.7.4/internal/utils/utils_is.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package utils
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/gogf/gf/v2/internal/empty"
    13  )
    14  
    15  // IsNil checks whether `value` is nil, especially for interface{} type value.
    16  func IsNil(value interface{}) bool {
    17  	return empty.IsNil(value)
    18  }
    19  
    20  // IsEmpty checks whether `value` is empty.
    21  func IsEmpty(value interface{}) bool {
    22  	return empty.IsEmpty(value)
    23  }
    24  
    25  // IsInt checks whether `value` is type of int.
    26  func IsInt(value interface{}) bool {
    27  	switch value.(type) {
    28  	case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64:
    29  		return true
    30  	}
    31  	return false
    32  }
    33  
    34  // IsUint checks whether `value` is type of uint.
    35  func IsUint(value interface{}) bool {
    36  	switch value.(type) {
    37  	case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64:
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  // IsFloat checks whether `value` is type of float.
    44  func IsFloat(value interface{}) bool {
    45  	switch value.(type) {
    46  	case float32, *float32, float64, *float64:
    47  		return true
    48  	}
    49  	return false
    50  }
    51  
    52  // IsSlice checks whether `value` is type of slice.
    53  func IsSlice(value interface{}) bool {
    54  	var (
    55  		reflectValue = reflect.ValueOf(value)
    56  		reflectKind  = reflectValue.Kind()
    57  	)
    58  	for reflectKind == reflect.Ptr {
    59  		reflectValue = reflectValue.Elem()
    60  		reflectKind = reflectValue.Kind()
    61  	}
    62  	switch reflectKind {
    63  	case reflect.Slice, reflect.Array:
    64  		return true
    65  	}
    66  	return false
    67  }
    68  
    69  // IsMap checks whether `value` is type of map.
    70  func IsMap(value interface{}) bool {
    71  	var (
    72  		reflectValue = reflect.ValueOf(value)
    73  		reflectKind  = reflectValue.Kind()
    74  	)
    75  	for reflectKind == reflect.Ptr {
    76  		reflectValue = reflectValue.Elem()
    77  		reflectKind = reflectValue.Kind()
    78  	}
    79  	switch reflectKind {
    80  	case reflect.Map:
    81  		return true
    82  	}
    83  	return false
    84  }
    85  
    86  // IsStruct checks whether `value` is type of struct.
    87  func IsStruct(value interface{}) bool {
    88  	var reflectType = reflect.TypeOf(value)
    89  	if reflectType == nil {
    90  		return false
    91  	}
    92  	var reflectKind = reflectType.Kind()
    93  	for reflectKind == reflect.Ptr {
    94  		reflectType = reflectType.Elem()
    95  		reflectKind = reflectType.Kind()
    96  	}
    97  	switch reflectKind {
    98  	case reflect.Struct:
    99  		return true
   100  	}
   101  	return false
   102  }