44 lines
549 B
Go
44 lines
549 B
Go
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
|
|
}
|