1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
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
}
|