使用反射方式对any任意类型的数据是否为空判断, 可判断时间对象是否为空, 可判断所有数字类型,指针类型和结构体字符串是否为空
判断规则:
bool类型因为只有true和false 所以 全部视为非空
nil 类型全部视为空
所有数字类型的 零值全部视为空
对应指针类型数据,只要是非nil 则视为非空
对于时间类型结构体或者其他带有 IsZero()方法的结构体 动态调用IsZero()方法来判断是否为空
go代码实现
import ("reflect"
)// 使用反射方式对any任意类型的数据是否为空判断, 可判断时间对象是否为空, 可判断所有数字类型,指针类型和结构体字符串是否为空// 判断规则:
// bool类型因为只有true和false 所以 全部视为非空
// nil 类型全部视为空
// 所有数字类型的 零值全部视为空
// 对应指针类型数据,只要是非nil 则视为非空
// 对于时间类型结构体或者其他带有 IsZero()方法的结构体 动态调用IsZero()方法来判断是否为空
//
// @author: tekintian <tekintian@gmail.com>
// @see https://dev.tekin.cn
func IsEmpty(data interface{}) bool {rv := reflect.ValueOf(data)rvk := rv.Kind()switch rvk {case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:return rv.IsZero()case reflect.Uintptr, reflect.Map, reflect.Slice, reflect.Pointer, reflect.UnsafePointer:return rv.IsNil() // 指针类型数据全部调用 IsNil() 进行判断,非nil的指针视为非空case reflect.String:return rv.Len() == 0case reflect.Struct:if rv.MethodByName("IsZero").IsValid() {// 动态调用结构体中的 IsZero 方法rt := rv.MethodByName("IsZero").Call([]reflect.Value{}) // call后面的参数即是要调用方法的参数 []reflect.Value{} 表示无参数return len(rt) > 0 && rt[0].Bool() // IsZero方法的返回值只有一个bool,} else {// 对值的零值类型进行深度比较return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())}case reflect.Invalid: // nil值的reflect类型就是 Invalidreturn true // nil也算是空值}return false // 其他情况默认非空
}
单元测试用例
import ("gotms/utils""testing""time"
)type DemoSt struct {Name stringItems []DemoItemData interface{}Created time.Time
}
type DemoItem struct {Name string
}func TestIsEmpty(t *testing.T) {cases := []struct {input interface{}expected bool}{{input: time.Now(), expected: false},{input: DemoSt{}.Created, expected: true}, // 空时间对象 这个是未初始化的时间对象 视为空{input: time.Time{}, expected: true}, // 空时间对象 已经初始化{input: false, expected: false},{input: DemoSt{}.Items, expected: true}, // 这里的DemoSt{}.Items 是一个不可达的对象,即未初始化的对象 视为空{input: DemoSt{}.Data, expected: true}, // nil{input: "", expected: true},{input: 0.0, expected: true},{input: nil, expected: true},{input: complex(0, 0), expected: true}, // 复数的判断{input: complex(2, 1), expected: false},}for _, v := range cases {ret := utils.IsEmpty(v.input)if ret != v.expected {t.Fatalf("%v test failed: expected: %v, got: %v", v.input, v.expected, ret)}}
}