-
Notifications
You must be signed in to change notification settings - Fork 1
/
setter.go
69 lines (54 loc) · 1.13 KB
/
setter.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
// Cr. https://github.com/nlepage/golang-wasm/tree/master/js/bind
package citizenfx
import (
"reflect"
"syscall/js"
)
func isSetter(t string, ft reflect.Type) bool {
return isProperty(t) && ft.NumIn() == 1 && ft.NumOut() == 0
}
type setter struct {
name string
parent func() js.Value
}
func (s setter) set(v interface{}) {
s.parent().Set(s.name, v)
}
type floatSetter struct {
setter
}
func (s floatSetter) set(f float64) {
s.setter.set(f)
}
type intSetter struct {
setter
}
func (s intSetter) set(f int) {
s.setter.set(f)
}
type boolSetter struct {
setter
}
func (s boolSetter) set(f bool) {
s.setter.set(f)
}
type stringSetter struct {
setter
}
func (s stringSetter) set(f string) {
s.setter.set(f)
}
func bindSetter(name string, t reflect.Type, parent func() js.Value) reflect.Value {
s := setter{name, parent}
switch t.Kind() {
case reflect.Float64:
return reflect.ValueOf(floatSetter{s}.set)
case reflect.Int:
return reflect.ValueOf(intSetter{s}.set)
case reflect.Bool:
return reflect.ValueOf(boolSetter{s}.set)
case reflect.String:
return reflect.ValueOf(stringSetter{s}.set)
}
panic("FIXME") //FIXME
}