widget/reflect.go

97 lines
1.5 KiB
Go
Raw Normal View History

package widget
import (
"reflect"
"strconv"
)
func GetString(A any) string {
if A == nil {
// log.Warn("getString() got nil")
return ""
}
var k reflect.Kind
k = reflect.TypeOf(A).Kind()
switch k {
case reflect.Int:
var i int
i = A.(int)
return strconv.Itoa(i)
case reflect.String:
return A.(string)
case reflect.Bool:
if A.(bool) == true {
return "true"
} else {
return "false"
}
default:
// log.Warn("getString uknown kind", k, "value =", A)
return ""
}
return ""
}
// work like unix os.Exec() ? everything other than 0 is false
func GetBool(A any) bool {
if A == nil {
// log.Warn("getBool() got nil")
return false
}
var k reflect.Kind
k = reflect.TypeOf(A).Kind()
switch k {
case reflect.Int:
var i int
i = A.(int)
if i == 0 {
return true
} else {
return false
}
case reflect.String:
if A.(string) == "true" {
return true
} else {
return false
}
case reflect.Bool:
return A.(bool)
default:
// log.Warn("getString uknown kind", k, "value =", A)
return false
}
return false
}
// work like unix? everything other than 0 is false
func GetInt(A any) int {
if A == nil {
// log.Warn("getBool() got nil")
return -1
}
var k reflect.Kind
k = reflect.TypeOf(A).Kind()
switch k {
case reflect.Int:
return A.(int)
case reflect.String:
tmp := A.(string)
i, _ := strconv.Atoi(tmp)
return i
case reflect.Bool:
if A.(bool) {
return 0
} else {
return -1
}
default:
// log.Warn("getString uknown kind", k, "value =", A)
return -1
}
return -1
}