240 lines
4.9 KiB
Go
240 lines
4.9 KiB
Go
package goref
|
|
|
|
import "reflect"
|
|
|
|
func IsInt(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Int
|
|
}
|
|
|
|
func IsInt8(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Int8
|
|
}
|
|
|
|
func IsInt16(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Int16
|
|
}
|
|
|
|
func IsInt32(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Int32
|
|
}
|
|
|
|
func IsInt64(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Int64
|
|
}
|
|
|
|
func IsUint(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uint
|
|
}
|
|
|
|
func IsUint8(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uint8
|
|
}
|
|
|
|
func IsUint16(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uint16
|
|
}
|
|
|
|
func IsUint32(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uint32
|
|
}
|
|
|
|
func IsUint64(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uint64
|
|
}
|
|
|
|
func IsInteger(val any) bool {
|
|
typeKind := reflect.TypeOf(val).Kind()
|
|
return typeKind >= reflect.Int && typeKind <= reflect.Uint64
|
|
}
|
|
|
|
func IsFloat32(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Float32
|
|
}
|
|
|
|
func IsFloat64(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Float64
|
|
}
|
|
|
|
func IsFloat(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Float32 || typeOf.Kind() == reflect.Float64
|
|
}
|
|
|
|
func IsNumber(val any) bool {
|
|
return IsInteger(val) || IsFloat(val)
|
|
}
|
|
|
|
func IsBool(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Bool
|
|
}
|
|
|
|
func IsString(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.String
|
|
}
|
|
|
|
func IsArray(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Array
|
|
}
|
|
|
|
func IsSlice(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Slice
|
|
}
|
|
|
|
func IsList(val any) bool {
|
|
return IsArray(val) || IsSlice(val)
|
|
}
|
|
|
|
func IsMap(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Map
|
|
}
|
|
|
|
func IsStruct(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Struct
|
|
}
|
|
|
|
func IsInterface(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Interface
|
|
}
|
|
|
|
func IsFunc(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Func
|
|
}
|
|
|
|
func IsPointer(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Pointer
|
|
}
|
|
|
|
func IsUnsafePointer(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.UnsafePointer
|
|
}
|
|
|
|
func IsUintptr(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Uintptr
|
|
}
|
|
|
|
func IsChannel(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
return typeOf.Kind() == reflect.Chan
|
|
}
|
|
|
|
func IsBaseStruct(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
if typeOf.Kind() == reflect.Ptr {
|
|
typeOf = typeOf.Elem()
|
|
}
|
|
return typeOf.Kind() == reflect.Struct
|
|
}
|
|
|
|
func IsBaseMap(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
if typeOf.Kind() == reflect.Ptr {
|
|
typeOf = typeOf.Elem()
|
|
}
|
|
return typeOf.Kind() == reflect.Map
|
|
}
|
|
|
|
func IsBaseString(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
if typeOf.Kind() == reflect.Ptr {
|
|
typeOf = typeOf.Elem()
|
|
}
|
|
return typeOf.Kind() == reflect.String
|
|
}
|
|
|
|
func IsBaseSlice(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
if typeOf.Kind() == reflect.Ptr {
|
|
typeOf = typeOf.Elem()
|
|
}
|
|
return typeOf.Kind() == reflect.Slice
|
|
}
|
|
|
|
func IsBaseArray(val any) bool {
|
|
typeOf := reflect.TypeOf(val)
|
|
if typeOf.Kind() == reflect.Ptr {
|
|
typeOf = typeOf.Elem()
|
|
}
|
|
return typeOf.Kind() == reflect.Array
|
|
}
|
|
|
|
func IsBaseList(val any) bool {
|
|
return IsBaseSlice(val) || IsBaseArray(val)
|
|
}
|
|
|
|
func GetListBaseType(val any) reflect.Type {
|
|
to := reflect.TypeOf(val)
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
if to.Kind() != reflect.Slice && to.Kind() != reflect.Array {
|
|
panic("invalid type: " + to.String())
|
|
}
|
|
to = to.Elem()
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
return to
|
|
}
|
|
|
|
func GetMapValueBaseType(val any) reflect.Type {
|
|
to := reflect.TypeOf(val)
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
if to.Kind() != reflect.Map {
|
|
panic("invalid type: " + to.String())
|
|
}
|
|
to = to.Elem()
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
return to
|
|
}
|
|
|
|
func GetBaseType(val any) reflect.Type {
|
|
to := reflect.TypeOf(val)
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
if to.Kind() == reflect.Map || to.Kind() == reflect.Array || to.Kind() == reflect.Slice || to.Kind() == reflect.Chan {
|
|
to = to.Elem()
|
|
}
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
return to
|
|
}
|
|
|
|
func IsBasicType(val any) bool {
|
|
rt := GetBaseType(val)
|
|
switch rt.Kind() {
|
|
case reflect.Chan, reflect.Struct, reflect.Array, reflect.Func, reflect.Map, reflect.Slice, reflect.Interface:
|
|
return false
|
|
default:
|
|
return true
|
|
}
|
|
}
|