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