126 lines
2.0 KiB
Go
126 lines
2.0 KiB
Go
package widget
|
|
|
|
/*
|
|
widget is a low level package that just defines the gui package
|
|
interaction with the toolkits.
|
|
|
|
For simplicity, it seems to make sense to put type conversion functions
|
|
here for now
|
|
*/
|
|
|
|
import (
|
|
"reflect"
|
|
"strconv"
|
|
)
|
|
|
|
/*
|
|
// TODO: this syntax or the other syntax?
|
|
|
|
func convertString(val any) string {
|
|
switch v := val.(type) {
|
|
case bool:
|
|
n.B = val.(bool)
|
|
case string:
|
|
n.label = val.(string)
|
|
n.S = val.(string)
|
|
case int:
|
|
n.I = val.(int)
|
|
default:
|
|
log.Error(errors.New("Set() unknown type"), "v =", v)
|
|
}
|
|
}
|
|
*/
|
|
|
|
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, err := strconv.Atoi(tmp)
|
|
if err != nil {
|
|
return -1
|
|
}
|
|
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
|
|
}
|