1 Star 1 Fork 0

iceinto / ipkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bool.go 906 Bytes
一键复制 编辑 原始数据 按行查看 历史
package iconv
import (
"reflect"
"strings"
)
// Bool converts `any` to bool.
// It returns false if `any` is: false, "", 0, "false", "off", "no", empty slice/map.
func Bool(any interface{}) bool {
if any == nil {
return false
}
switch value := any.(type) {
case bool:
return value
case []byte:
if _, ok := emptyStringMap[strings.ToLower(string(value))]; ok {
return false
}
return true
case string:
if _, ok := emptyStringMap[strings.ToLower(value)]; ok {
return false
}
return true
default:
rv := reflect.ValueOf(any)
switch rv.Kind() {
case reflect.Ptr:
return !rv.IsNil()
case reflect.Map:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
return rv.Len() != 0
case reflect.Struct:
return true
default:
s := strings.ToLower(String(any))
if _, ok := emptyStringMap[s]; ok {
return false
}
return true
}
}
}
1
https://gitee.com/iceinto/ipkg.git
git@gitee.com:iceinto/ipkg.git
iceinto
ipkg
ipkg
v1.0.2

搜索帮助