mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-08 00:10:05 +08:00
Feature/cover unit test (#81)
This commit is contained in:
@@ -1,72 +1,9 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func toBool(i any) bool {
|
||||
if i == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return false
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case bool:
|
||||
return v
|
||||
case int:
|
||||
return v != 0
|
||||
case int8:
|
||||
return v != 0
|
||||
case int16:
|
||||
return v != 0
|
||||
case int32:
|
||||
return v != 0
|
||||
case int64:
|
||||
return v != 0
|
||||
case uint:
|
||||
return v != 0
|
||||
case uint8:
|
||||
return v != 0
|
||||
case uint16:
|
||||
return v != 0
|
||||
case uint32:
|
||||
return v != 0
|
||||
case uint64:
|
||||
return v != 0
|
||||
case uintptr:
|
||||
return v != 0
|
||||
case float32:
|
||||
return v != 0
|
||||
case float64:
|
||||
return v != 0
|
||||
case complex64:
|
||||
return v != 0
|
||||
case complex128:
|
||||
return v != 0
|
||||
case string:
|
||||
if v == "true" {
|
||||
return true
|
||||
} else if v == "false" {
|
||||
return false
|
||||
}
|
||||
return v != ""
|
||||
case fmt.Stringer:
|
||||
return v.String() != ""
|
||||
case interface{ IsNil() bool }:
|
||||
return !v.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func toBoolReflect(i any) bool {
|
||||
if i == nil {
|
||||
return false
|
||||
|
Reference in New Issue
Block a user