/
field.go
135 lines (116 loc) · 3.49 KB
/
field.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
package markup
import (
"fmt"
"math"
"reflect"
"time"
"github.com/shimmerglass/bar3x/ui"
)
type field struct {
StructName string
Name string
Type reflect.Type
getter reflect.Value
setter reflect.Value
onChange reflect.Value
onChangeCbType reflect.Type
}
func newField(base reflect.Value, name string) (*field, error) {
f := &field{
Name: name,
StructName: base.Type().String(),
}
getter := base.MethodByName(name)
if !getter.IsValid() {
return nil, fmt.Errorf("type %q had no field %q", f.StructName, name)
}
if getter.Type().NumOut() != 1 {
return nil, fmt.Errorf("type %q field %q getter should return 1 value", f.StructName, name)
}
getterType := getter.Type().Out(0)
f.Type = getterType
f.getter = getter
setter := base.MethodByName("Set" + name)
if setter.IsValid() {
if setter.Type().NumIn() != 1 {
return nil, fmt.Errorf("%s: field %q setter should accept 1 argument", f.StructName, name)
}
setterType := setter.Type().In(0)
if setterType != getterType {
return nil, fmt.Errorf("%s: field %q setter should accept a %s", f.StructName, name, getterType)
}
f.setter = setter
}
onChange := base.MethodByName("On" + name + "Change")
if onChange.IsValid() {
if onChange.Type().NumIn() != 1 {
return nil, fmt.Errorf("%s: field %q onChange should accept 1 argument", f.StructName, name)
}
onChangeCb := onChange.Type().In(0)
if onChangeCb.Kind() != reflect.Func {
return nil, fmt.Errorf("%s: field %q onChange should accept a function", f.StructName, name)
}
if onChangeCb.NumIn() != 1 {
return nil, fmt.Errorf("%s: field %q onChange callback should accept 1 argument", f.StructName, name)
}
if onChangeCb.In(0) != getterType {
return nil, fmt.Errorf("%s: field %q onChange callback should accept a %s", f.StructName, name, getterType)
}
f.onChange = onChange
f.onChangeCbType = onChangeCb
}
return f, nil
}
func (f *field) Get() interface{} {
res := f.getter.Call([]reflect.Value{})
return res[0].Interface()
}
func (f *field) Set(v interface{}) error {
if !f.setter.IsValid() {
return fmt.Errorf("%s: field %q cannot be set: it is readonly", f.StructName, f.Name)
}
val := reflect.ValueOf(v)
// color special case
if f.Type.String() == "color.Color" && val.Kind() == reflect.String {
c, err := ui.ParseColor(v.(string))
if err != nil {
return fmt.Errorf("%s: cannot set field %q: %s", f.StructName, f.Name, err)
}
val = reflect.ValueOf(c)
}
// duration special case
if f.Type.String() == "time.Duration" && val.Kind() == reflect.String {
c, err := time.ParseDuration(v.(string))
if err != nil {
return fmt.Errorf("%s: cannot set field %q: %s", f.StructName, f.Name, err)
}
val = reflect.ValueOf(c)
}
// float special case
if f.Type.Kind() == reflect.Int && val.Kind() == reflect.Float64 {
val = reflect.ValueOf(int(math.Round(v.(float64))))
}
if !val.Type().AssignableTo(f.Type) {
return fmt.Errorf(
"%s: cannot set field %q (%s) with %s, expected %s",
f.StructName,
f.Name,
f.Type,
val.Type().String(),
f.Type.String(),
)
}
f.setter.Call([]reflect.Value{val})
return nil
}
func (f *field) Watch(cb func(interface{})) error {
if !f.onChange.IsValid() {
return fmt.Errorf("%s: type %q field %q cannot be watched (no onChange found)", f.StructName, f.Type, f.Name)
}
fn := reflect.MakeFunc(f.onChangeCbType, func(args []reflect.Value) []reflect.Value {
cb(args[0].Interface())
return nil
})
f.onChange.Call([]reflect.Value{fn})
return nil
}