初始版本
This commit is contained in:
43
convx/convx.go
Normal file
43
convx/convx.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package convx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Int(v any) int {
|
||||
return int(Int64(v))
|
||||
}
|
||||
|
||||
func Int64(v any) int64 {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
return int64(n)
|
||||
case int64:
|
||||
return n
|
||||
case float64:
|
||||
return int64(n)
|
||||
case json.Number:
|
||||
i, _ := n.Int64()
|
||||
return i
|
||||
case string:
|
||||
i, _ := strconv.ParseInt(n, 10, 64)
|
||||
return i
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func ValueOrZero(v, invalid int) int {
|
||||
if v == invalid {
|
||||
return 0
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func Ternary[T any](cond bool, yes, no T) T {
|
||||
if cond {
|
||||
return yes
|
||||
}
|
||||
return no
|
||||
}
|
||||
Reference in New Issue
Block a user