-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
184 lines (149 loc) · 4.82 KB
/
converter.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dynamic_gui_config
import (
"errors"
"fmt"
"log"
"reflect"
"strconv"
)
var (
valueControlType = reflect.TypeOf((*ValueControl)(nil)).Elem()
onChangedType = reflect.TypeOf((*UpdateNotifier)(nil)).Elem()
)
// new
func MakeValueControl(object interface{}) (ValueControl, error) {
return MakeValueControlFromValue(reflect.ValueOf(object), structTagDefaults)
}
func MakeValueControlFromValue(value reflect.Value, properties StructTagProperties) (ValueControl, error) {
if !value.IsValid() {
return nil, errors.New("invalid value")
}
if !value.CanInterface() {
return nil, errors.New("cannot interface value")
}
if value.Type().Implements(valueControlType) {
return value.Interface().(ValueControl), nil
}
switch value.Kind() {
case reflect.Ptr:
return MakeValueControlFromValue(value.Elem(), properties)
case reflect.Struct:
return structBreakdown(value, properties)
case reflect.Array, reflect.Slice:
return arrayBreakdown(value, properties)
case reflect.Chan:
return chanBreakdown(value, properties)
default:
return fieldValueBreakdown(value, properties)
}
}
func chanBreakdown(value reflect.Value, properties StructTagProperties) (ValueControl, error) {
if value.IsNil() {
return nil, errors.New("chan type is nil")
}
if value.Type().ChanDir()|reflect.SendDir == 0 {
return nil, errors.New("chan cannot send")
}
vToSend := reflect.New(value.Type().Elem())
sendFn := func() {
value.TrySend(vToSend.Elem())
}
btnVc, err := MakeValueControlFromValue(reflect.ValueOf(&sendFn), properties)
if err != nil {
return nil, err
}
underlyingVc, err := MakeValueControlFromValue(vToSend, properties)
if err != nil {
return nil, err
}
return LabeledGuiField{Label: btnVc, Factory: underlyingVc}, nil
}
func fieldValueBreakdown(value reflect.Value, properties StructTagProperties) (ValueControl, error) {
if !value.CanAddr() {
return nil, errors.New(fmt.Sprintf("cannot take address of value, %s", value))
}
if bIn, ok := builtin[value.Kind()]; ok {
log.Printf("adding builtin object %s kind %s", value.Type(), value.Kind())
onchanged := func() {}
if value.Addr().Type().Implements(onChangedType) {
iface, ok := value.Addr().Interface().(UpdateNotifier)
if ok {
log.Printf("add onchanged handler for type %s", value.Type())
onchanged = iface.OnValueChanged
}
}
kindType, ok := typedKind[value.Kind()]
if ok && value.Addr().Type().ConvertibleTo(kindType) {
valueConverted := value.Addr().Convert(typedKind[value.Kind()])
if controlFactory, err := bIn(valueConverted.Interface(), properties, onchanged); err != nil {
return nil, errors.New(fmt.Sprintf("error: %s for type %s", err, value.Kind()))
} else {
return controlFactory, nil
}
} else {
return nil, errors.New(fmt.Sprintf("cannot convert %s to %s", value.Type(), kindType))
}
}
return nil, errors.New(fmt.Sprintf("no match for %s", value.Type()))
}
func arrayBreakdown(array reflect.Value, properties StructTagProperties) (ValueControl, error) {
result := make([]ValueControl, 0, array.Len())
for i := 0; i < array.Len(); i++ {
currentProperties := properties
currentProperties.Index = i
if properties.Labels != nil && len(properties.Labels) > i {
currentProperties.Name = properties.Labels[i]
}
if currentProperties.Name == "" {
currentProperties.Name = Label(strconv.Itoa(i))
}
if valueBreakdown, err := MakeValueControlFromValue(array.Index(i), currentProperties); err != nil {
log.Printf("ignoring %s[%d]: %s", array.Type(), i, err)
} else {
result = append(result, valueBreakdown)
}
}
if properties.Horizontal {
return horizontalValueControlArray(result), nil
} else {
return verticalValueControlArray(result), nil
}
}
func fieldBreakdown(field reflect.Value, structField reflect.StructField) (ValueControl, error) {
properties, err := ParseStructTag(structField.Tag.Get(tagkey))
if err != nil {
log.Printf("error parsing struct tag: %s", err)
}
if properties.Name == "" {
properties.Name = Label(structField.Name)
}
if factory, err := MakeValueControlFromValue(field, properties); err != nil {
return nil, err
} else {
if properties.Name == "omit" {
return factory, nil
} else {
return LabeledGuiField{
Label: properties.Name,
Factory: factory,
}, nil
}
}
}
func structBreakdown(value reflect.Value, properties StructTagProperties) (ValueControl, error) {
result := make([]ValueControl, 0, value.NumField())
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
structField := value.Type().Field(i)
if guifield, err := fieldBreakdown(field, structField); err == nil {
result = append(result, guifield)
} else {
log.Printf("Ignoring %s, error: %s, ", structField.Name, err)
}
}
if properties.Horizontal {
return horizontalValueControlArray(result), nil
} else {
return verticalValueControlArray(result), nil
}
}