fix: 解析工具Number/Boolean修复
This commit is contained in:
@@ -17,46 +17,53 @@ import (
|
||||
)
|
||||
|
||||
// Number 解析数值型
|
||||
func Number(str any) int64 {
|
||||
switch str := str.(type) {
|
||||
func Number(value any) int64 {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
if str == "" {
|
||||
if v == "" {
|
||||
return 0
|
||||
}
|
||||
num, err := strconv.ParseInt(str, 10, 64)
|
||||
num, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return num
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
return reflect.ValueOf(str).Int()
|
||||
case int, int8, int16, int32, int64:
|
||||
return reflect.ValueOf(v).Int()
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
return int64(reflect.ValueOf(v).Uint())
|
||||
case float32, float64:
|
||||
return int64(reflect.ValueOf(str).Float())
|
||||
return int64(reflect.ValueOf(v).Float())
|
||||
case bool:
|
||||
if v {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Boolean 解析布尔型
|
||||
func Boolean(str any) bool {
|
||||
switch str := str.(type) {
|
||||
func Boolean(value any) bool {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
if str == "" || str == "false" || str == "0" {
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// 尝试将字符串解析为数字
|
||||
if num, err := strconv.ParseFloat(str, 64); err == nil {
|
||||
return num != 0
|
||||
}
|
||||
return true
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
num := reflect.ValueOf(str).Int()
|
||||
return b
|
||||
case int, int8, int16, int32, int64:
|
||||
num := reflect.ValueOf(v).Int()
|
||||
return num != 0
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
num := int64(reflect.ValueOf(v).Uint())
|
||||
return num != 0
|
||||
case float32, float64:
|
||||
num := reflect.ValueOf(str).Float()
|
||||
num := reflect.ValueOf(v).Float()
|
||||
return num != 0
|
||||
case bool:
|
||||
return str
|
||||
return v
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user