-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
170 lines (154 loc) · 3.87 KB
/
decode.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
package style
import (
"io"
"io/ioutil"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
func trimWS(s string) string {
return strings.Trim(s, " \r\n\t")
}
func parseRaw(s string) (map[string]string, error) {
raw := map[string]string{}
styleStrs := strings.Split(s, ";")
for _, styleStr := range styleStrs {
styleStr = trimWS(styleStr)
if len(styleStr) == 0 {
continue
}
styleKV := strings.Split(styleStr, ":")
if len(styleKV) != 2 {
return nil, errors.Errorf("invalid style syntax (%s) must be of (key:val)", styleStr)
}
raw[trimWS(styleKV[0])] = trimWS(styleKV[1])
}
return raw, nil
}
type Unmarshaler interface {
UnmarshalStyle(v string) error
}
type MutateFnc func(styles *Styles)
func MutateNone(styles *Styles) {}
type Mutator struct {
fncs []MutateFnc
}
func (m *Mutator) Append(other *Mutator) {
for _, f := range other.fncs {
m.fncs = append(m.fncs, f)
}
}
func (m *Mutator) Mutate(styles *Styles) {
for _, fnc := range m.fncs {
fnc(styles)
}
}
func DecodeMutator(r io.Reader) (*Mutator, error) {
m := &Mutator{
fncs: []MutateFnc{},
}
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.Wrap(err, "read-all")
}
raw, err := parseRaw(string(b))
if err != nil {
return nil, errors.Wrap(err, "parse-raw")
}
protoType := reflect.TypeOf(Styles{})
for k, v := range raw {
fnc, found, err := makeMutateFnc(protoType, k, v, []int{})
if err != nil {
return nil, errors.Wrapf(err, "make-mutate-fnc (%s, %s)", k, v)
}
if !found {
continue
}
m.fncs = append(m.fncs, fnc)
}
return m, nil
}
func makeMutateFnc(rt reflect.Type, key, val string, indexPath []int) (MutateFnc, bool, error) {
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
currIndexPath := appendedCopy(indexPath, i)
if t := field.Tag.Get("style"); t == key {
var setValue func(reflect.Value)
um, impls := reflect.New(field.Type).Interface().(Unmarshaler)
if impls {
err := um.UnmarshalStyle(val)
if err != nil {
return nil, false, err
}
setValue = func(rv reflect.Value) {
rv.Set(reflect.ValueOf(um).Elem())
}
} else {
var err error
setValue, err = makeSetValueFnc(field.Type.Kind(), val)
if err != nil {
return nil, false, errors.Wrapf(err, "make set value func (%s, %s)", key, val)
}
}
return func(s *Styles) {
rVal := reflect.ValueOf(s).Elem()
for _, fIdx := range currIndexPath {
rVal = rVal.Field(fIdx)
}
setValue(rVal)
}, true, nil
}
if field.Type.Kind() == reflect.Struct {
fnc, found, err := makeMutateFnc(field.Type, key, val, currIndexPath)
if err != nil {
return nil, true, err
} else if found {
return fnc, true, nil
}
}
}
return nil, false, nil
}
func makeSetValueFnc(kind reflect.Kind, styleValue string) (func(v reflect.Value), error) {
switch kind {
case reflect.String:
return func(v reflect.Value) {
v.SetString(styleValue)
}, nil
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(styleValue, 64)
if err != nil {
return nil, errors.Wrapf(err, "parse-float (%s)", styleValue)
}
return func(v reflect.Value) {
v.SetFloat(f)
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(styleValue, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "parse-int (%s)", styleValue)
}
return func(v reflect.Value) {
v.SetInt(n)
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseUint(styleValue, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "parse-uint (%s)", styleValue)
}
return func(v reflect.Value) {
v.SetUint(n)
}, nil
default:
return nil, errors.Errorf("unsupported style kind (%s)", kind)
}
}
func appendedCopy(sl []int, a int) []int {
c := make([]int, len(sl)+1)
for i, v := range sl {
c[i] = v
}
c[len(c)-1] = a
return c
}