/
mapbinding.go
534 lines (437 loc) · 9.91 KB
/
mapbinding.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package binding
import (
"errors"
"reflect"
"fyne.io/fyne/v2"
)
// DataMap is the base interface for all bindable data maps.
//
// Since: 2.0
type DataMap interface {
DataItem
GetItem(string) (DataItem, error)
Keys() []string
}
// ExternalUntypedMap is a map data binding with all values untyped (interface{}), connected to an external data source.
//
// Since: 2.0
type ExternalUntypedMap interface {
UntypedMap
Reload() error
}
// UntypedMap is a map data binding with all values Untyped (interface{}).
//
// Since: 2.0
type UntypedMap interface {
DataMap
Delete(string)
Get() (map[string]interface{}, error)
GetValue(string) (interface{}, error)
Set(map[string]interface{}) error
SetValue(string, interface{}) error
}
// NewUntypedMap creates a new, empty map binding of string to interface{}.
//
// Since: 2.0
func NewUntypedMap() UntypedMap {
return &mapBase{items: make(map[string]reflectUntyped), val: &map[string]interface{}{}}
}
// BindUntypedMap creates a new map binding of string to interface{} based on the data passed.
// If your code changes the content of the map this refers to you should call Reload() to inform the bindings.
//
// Since: 2.0
func BindUntypedMap(d *map[string]interface{}) ExternalUntypedMap {
if d == nil {
return NewUntypedMap().(ExternalUntypedMap)
}
m := &mapBase{items: make(map[string]reflectUntyped), val: d, updateExternal: true}
for k := range *d {
m.setItem(k, bindUntypedMapValue(d, k, m.updateExternal))
}
return m
}
// Struct is the base interface for a bound struct type.
//
// Since: 2.0
type Struct interface {
DataMap
GetValue(string) (interface{}, error)
SetValue(string, interface{}) error
Reload() error
}
// BindStruct creates a new map binding of string to interface{} using the struct passed as data.
// The key for each item is a string representation of each exported field with the value set as an interface{}.
// Only exported fields are included.
//
// Since: 2.0
func BindStruct(i interface{}) Struct {
if i == nil {
return NewUntypedMap().(Struct)
}
t := reflect.TypeOf(i)
if t.Kind() != reflect.Ptr ||
(reflect.TypeOf(reflect.ValueOf(i).Elem()).Kind() != reflect.Struct) {
fyne.LogError("Invalid type passed to BindStruct, must be pointer to struct", nil)
return NewUntypedMap().(Struct)
}
s := &boundStruct{orig: i}
s.items = make(map[string]reflectUntyped)
s.val = &map[string]interface{}{}
s.updateExternal = true
v := reflect.ValueOf(i).Elem()
t = v.Type()
for j := 0; j < v.NumField(); j++ {
f := v.Field(j)
if !f.CanSet() {
continue
}
key := t.Field(j).Name
s.items[key] = bindReflect(f)
(*s.val)[key] = f.Interface()
}
return s
}
type reflectUntyped interface {
DataItem
get() (interface{}, error)
set(interface{}) error
}
type mapBase struct {
base
updateExternal bool
items map[string]reflectUntyped
val *map[string]interface{}
}
func (b *mapBase) GetItem(key string) (DataItem, error) {
b.lock.RLock()
defer b.lock.RUnlock()
if v, ok := b.items[key]; ok {
return v, nil
}
return nil, errKeyNotFound
}
func (b *mapBase) Keys() []string {
b.lock.Lock()
defer b.lock.Unlock()
ret := make([]string, len(b.items))
i := 0
for k := range b.items {
ret[i] = k
i++
}
return ret
}
func (b *mapBase) Delete(key string) {
b.lock.Lock()
defer b.lock.Unlock()
delete(b.items, key)
b.trigger()
}
func (b *mapBase) Get() (map[string]interface{}, error) {
b.lock.RLock()
defer b.lock.RUnlock()
if b.val == nil {
return map[string]interface{}{}, nil
}
return *b.val, nil
}
func (b *mapBase) GetValue(key string) (interface{}, error) {
b.lock.RLock()
defer b.lock.RUnlock()
if i, ok := b.items[key]; ok {
return i.get()
}
return nil, errKeyNotFound
}
func (b *mapBase) Reload() error {
b.lock.Lock()
defer b.lock.Unlock()
return b.doReload()
}
func (b *mapBase) Set(v map[string]interface{}) error {
b.lock.Lock()
defer b.lock.Unlock()
if b.val == nil { // was not initialized with a blank value, recover
b.val = &v
b.trigger()
return nil
}
*b.val = v
return b.doReload()
}
func (b *mapBase) SetValue(key string, d interface{}) error {
b.lock.Lock()
defer b.lock.Unlock()
if i, ok := b.items[key]; ok {
return i.set(d)
}
(*b.val)[key] = d
item := bindUntypedMapValue(b.val, key, b.updateExternal)
b.setItem(key, item)
return nil
}
func (b *mapBase) doReload() (retErr error) {
changed := false
// add new
for key := range *b.val {
found := false
for newKey := range b.items {
if newKey == key {
found = true
}
}
if !found {
b.setItem(key, bindUntypedMapValue(b.val, key, b.updateExternal))
changed = true
}
}
// remove old
for key := range b.items {
found := false
for newKey := range *b.val {
if newKey == key {
found = true
break
}
}
if !found {
delete(b.items, key)
changed = true
}
}
if changed {
b.trigger()
}
for k, item := range b.items {
var err error
if b.updateExternal {
err = item.(*boundExternalMapValue).setIfChanged((*b.val)[k])
} else {
err = item.(*boundMapValue).set((*b.val)[k])
}
if err != nil {
retErr = err
}
}
return
}
func (b *mapBase) setItem(key string, d reflectUntyped) {
b.items[key] = d
b.trigger()
}
type boundStruct struct {
mapBase
orig interface{}
}
func (b *boundStruct) Reload() (retErr error) {
b.lock.Lock()
defer b.lock.Unlock()
v := reflect.ValueOf(b.orig).Elem()
t := v.Type()
for j := 0; j < v.NumField(); j++ {
f := v.Field(j)
if !f.CanSet() {
continue
}
kind := f.Kind()
if kind == reflect.Slice || kind == reflect.Struct {
fyne.LogError("Data binding does not yet support slice or struct elements in a struct", nil)
continue
}
key := t.Field(j).Name
old := (*b.val)[key]
if f.Interface() == old {
continue
}
var err error
switch kind {
case reflect.Bool:
err = b.items[key].(*reflectBool).Set(f.Bool())
case reflect.Float32, reflect.Float64:
err = b.items[key].(*reflectFloat).Set(f.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
err = b.items[key].(*reflectInt).Set(int(f.Int()))
case reflect.String:
err = b.items[key].(*reflectString).Set(f.String())
}
if err != nil {
retErr = err
}
(*b.val)[key] = f.Interface()
}
return
}
func bindUntypedMapValue(m *map[string]interface{}, k string, external bool) reflectUntyped {
if external {
ret := &boundExternalMapValue{old: (*m)[k]}
ret.val = m
ret.key = k
return ret
}
return &boundMapValue{val: m, key: k}
}
type boundMapValue struct {
base
val *map[string]interface{}
key string
}
func (b *boundMapValue) get() (interface{}, error) {
if v, ok := (*b.val)[b.key]; ok {
return v, nil
}
return nil, errKeyNotFound
}
func (b *boundMapValue) set(val interface{}) error {
(*b.val)[b.key] = val
b.trigger()
return nil
}
type boundExternalMapValue struct {
boundMapValue
old interface{}
}
func (b *boundExternalMapValue) setIfChanged(val interface{}) error {
if val == b.old {
return nil
}
b.old = val
return b.set(val)
}
type boundReflect struct {
base
val reflect.Value
}
func (b *boundReflect) get() (interface{}, error) {
return b.val.Interface(), nil
}
func (b *boundReflect) set(val interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("unable to set bool in data binding")
}
}()
b.val.Set(reflect.ValueOf(val))
b.trigger()
return nil
}
type reflectBool struct {
boundReflect
}
func (r *reflectBool) Get() (val bool, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("invalid bool value in data binding")
}
}()
val = r.val.Bool()
return
}
func (r *reflectBool) Set(b bool) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("unable to set bool in data binding")
}
}()
r.val.SetBool(b)
r.trigger()
return
}
func bindReflectBool(f reflect.Value) reflectUntyped {
r := &reflectBool{}
r.val = f
return r
}
type reflectFloat struct {
boundReflect
}
func (r *reflectFloat) Get() (val float64, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("invalid float64 value in data binding")
}
}()
val = r.val.Float()
return
}
func (r *reflectFloat) Set(f float64) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("unable to set float64 in data binding")
}
}()
r.val.SetFloat(f)
r.trigger()
return
}
func bindReflectFloat(f reflect.Value) reflectUntyped {
r := &reflectFloat{}
r.val = f
return r
}
type reflectInt struct {
boundReflect
}
func (r *reflectInt) Get() (val int, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("invalid int value in data binding")
}
}()
val = int(r.val.Int())
return
}
func (r *reflectInt) Set(i int) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("unable to set int in data binding")
}
}()
r.val.SetInt(int64(i))
r.trigger()
return
}
func bindReflectInt(f reflect.Value) reflectUntyped {
r := &reflectInt{}
r.val = f
return r
}
type reflectString struct {
boundReflect
}
func (r *reflectString) Get() (val string, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("invalid string value in data binding")
}
}()
val = r.val.String()
return
}
func (r *reflectString) Set(s string) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("unable to set string in data binding")
}
}()
r.val.SetString(s)
r.trigger()
return
}
func bindReflectString(f reflect.Value) reflectUntyped {
r := &reflectString{}
r.val = f
return r
}
func bindReflect(field reflect.Value) reflectUntyped {
switch field.Kind() {
case reflect.Bool:
return bindReflectBool(field)
case reflect.Float32, reflect.Float64:
return bindReflectFloat(field)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return bindReflectInt(field)
case reflect.String:
return bindReflectString(field)
}
return &boundReflect{val: field}
}