github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/reflect.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // IsNil tells whether v is nil or the underlying data is nil.
     9  func IsNil(v any) bool {
    10  	if v == nil {
    11  		return true
    12  	}
    13  	ef := EfaceOf(&v)
    14  	if ef.RType.Kind() == reflect.Slice {
    15  		return *(*unsafe.Pointer)(ef.Word) == nil
    16  	}
    17  	return ef.Word == nil
    18  }
    19  
    20  // IsIntType tells whether kind is an integer.
    21  func IsIntType(kind reflect.Kind) (isInt, isSigned bool) {
    22  	switch kind {
    23  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    24  		return true, true
    25  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    26  		return true, false
    27  	}
    28  	return false, false
    29  }
    30  
    31  // ReflectInt returns v's underlying value as int64.
    32  // It panics if v is not a integer value.
    33  func ReflectInt(v reflect.Value) int64 {
    34  	switch v.Kind() {
    35  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    36  		return v.Int()
    37  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    38  		return int64(v.Uint())
    39  	}
    40  
    41  	// shall not happen, type should be pre-checked
    42  	panic("bug: not int type")
    43  }