/
attr.go
410 lines (385 loc) · 11.6 KB
/
attr.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
package element
import (
"encoding/xml"
"errors"
"fmt"
"github.com/bhollier/ui/pkg/ui/util"
"image/color"
"math/bits"
"reflect"
"strconv"
"strings"
)
// Type for an attribute parser
type AttrParser func(attr string) (reflect.Value, error)
// Type for an attribute parser map
type attributeTypesMap map[reflect.Type]AttrParser
// Map of attribute types, with the
// key being the attribute's (reflect)
// type
var attributeTypes attributeTypesMap
// Function to initialise the attributes types map
func init() {
// Create the attributes types map,
// with all the primitive types (except
// uintptr, complex32 and complex64) and
// the util.* types
attributeTypes = attributeTypesMap{
// "Parsing" a string (function just spits it back)
reflect.TypeOf((*string)(nil)).Elem(): func(attr string) (reflect.Value, error) {
return reflect.ValueOf(attr), nil
},
// Parsing a boolean type
reflect.TypeOf((*bool)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseBool(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing an int type
reflect.TypeOf((*int)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.Atoi(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing an int8 type
reflect.TypeOf((*int8)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseInt(attr, 10, 8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(int8(val)), nil
},
// Parsing an int16 type
reflect.TypeOf((*int16)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseInt(attr, 10, 16)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(int16(val)), nil
},
// Parsing an int32 type
reflect.TypeOf((*int32)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseInt(attr, 10, 32)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(int32(val)), nil
},
// Parsing an int64 type
reflect.TypeOf((*int32)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseInt(attr, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a uint type
reflect.TypeOf((*uint)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, bits.UintSize)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(uint(val)), nil
},
// Parsing a uint8 type
reflect.TypeOf((*uint8)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, 8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(uint8(val)), nil
},
// Parsing a uint16 type
reflect.TypeOf((*uint16)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, 16)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(uint16(val)), nil
},
// Parsing a uint32 type
reflect.TypeOf((*uint32)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, 32)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(uint32(val)), nil
},
// Parsing a uint64 type
reflect.TypeOf((*uint32)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a byte type
reflect.TypeOf((*byte)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseUint(attr, 10, 8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(byte(val)), nil
},
// Parsing a rune type
reflect.TypeOf((*rune)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseInt(attr, 10, 32)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(rune(val)), nil
},
// Parsing a float32 type
reflect.TypeOf((*float32)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseFloat(attr, 32)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(float32(val)), nil
},
// Parsing a float64 type
reflect.TypeOf((*float64)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := strconv.ParseFloat(attr, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "color.RGBA" type
reflect.TypeOf((*color.RGBA)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseColor(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.Unit" type (as any
// unit type, there's no way to know
// whether the unit has to be absolute)
reflect.TypeOf((*util.Unit)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseUnit(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.RelativeSize" type
reflect.TypeOf((*util.RelativeSize)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseRelativeSize(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.AbsoluteQuantity" type
reflect.TypeOf((*util.AbsoluteQuantity)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseAbsoluteQuantity(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.Gravity" type
reflect.TypeOf((*util.Gravity)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseGravity(attr, util.DefaultGravity)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.Ratio" type
reflect.TypeOf((*util.Ratio)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseRatio(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.Orientation" type
reflect.TypeOf((*util.Orientation)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseOrientation(attr, util.DefaultOrientation)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
// Parsing a "util.ScaleOption" type
reflect.TypeOf((*util.ScaleOption)(nil)).Elem(): func(attr string) (reflect.Value, error) {
val, err := util.ParseScaleOption(attr)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(val), nil
},
}
}
// Function to register an
// attribute type
func RegisterAttrType(t reflect.Type, p AttrParser) {
// Register the attribute type
attributeTypes[t] = p
}
// Function to parse an attribute
// string into the given type
func ParseAttr(t reflect.Type, v string) (reflect.Value, error) {
// Try to get the attribute factory
parser, ok := attributeTypes[t]
// If it was found, call the parser
if ok {
return parser(v)
} else {
return reflect.Value{}, errors.New(
"unknown attribute type '" + t.Name() + "'")
}
}
// Function to parse the given xml
// attributes and set the fields of
// the given element using uixml tags.
// This function searches for tags
// recursively. It does not support
// arrays or maps
func SetAttrs(e Element, attrs []xml.Attr) error {
// Get the element's type info
t := reflect.TypeOf(e).Elem()
v := reflect.ValueOf(e).Elem()
// Firstly, look for a namespace attribute
for _, attr := range attrs {
if attr.Name.Space == "xmlns" {
// Add the namespace to the element
e.AddNamespace(attr.Value)
} else if attr.Name.Local == "xmlns" {
e.AddNamespace(attr.Name.Space)
}
}
// Create a map of fields
type Field struct {
Name string
Value reflect.Value
Optional bool
Set bool
}
fields := make(map[xml.Name]*Field, 0)
var findFieldsWithTag func(t reflect.Type, v reflect.Value) error
findFieldsWithTag = func(t reflect.Type, v reflect.Value) error {
// Iterate over the element's fields
for i := 0; i < t.NumField(); i++ {
// Try to get the field's uixml tag
tag, ok := t.Field(i).Tag.Lookup("uixml")
// If it's found
if ok {
// If the tag wants the field to be hidden,
// continue
if tag == "hidden" {
continue
}
// Create a field with the default values
field := Field{
Name: t.Field(i).Name,
Value: v.Field(i),
Optional: false,
Set: false,
}
// Split the tag by commas
commaSepList := strings.Split(tag, ",")
// Parse the name of the attribute
spaceIndex := strings.Index(commaSepList[0], " ")
var attrName xml.Name
if spaceIndex != -1 {
// Set the namespace
attrName.Space = commaSepList[0][:spaceIndex]
// Set the local
attrName.Local = commaSepList[0][spaceIndex+1:]
// Otherwise just set local as the whole name
} else {
attrName.Local = commaSepList[0]
}
// Iterate over all the tokens but the first
// (which is the field's name)
for j := 1; j < len(commaSepList); j++ {
switch commaSepList[j] {
// If the tag specifies the field is optional
case "optional":
field.Optional = true
// Otherwise the token is unknown and so return an error
default:
return errors.New("unknown token '" + commaSepList[j] +
"' in uixml tag on XML element '" +
FullName(e, ".", false) + "'")
}
}
// Add the field to the map
fields[attrName] = &field
// If it doesn't have a tag but
// has subfields that might have a tag
} else if v.Field(i).Kind() == reflect.Struct {
// Find fields in the struct
err := findFieldsWithTag(t.Field(i).Type, v.Field(i))
if err != nil {
return err
}
}
}
return nil
}
// Find fields
err := findFieldsWithTag(t, v)
if err != nil {
return err
}
// Iterate over the attributes
for _, attr := range attrs {
// If the attribute is for the namespace,
// we've already dealt with it so skip it
if attr.Name.Space == "xmlns" ||
attr.Name.Local == "xmlns" {
continue
}
// Look for the attribute in the fields map
field, ok := fields[attr.Name]
// If it doesn't exist
if !ok {
// If the attribute doesn't
// have a namespace
if attr.Name.Space == "" {
// Look for the attribute in the
// element's namespaces
for _, ns := range e.GetNamespaces() {
field, ok = fields[xml.Name{
Space: ns, Local: attr.Name.Local}]
if ok {
break
}
}
}
// If it still wasn't found
if !ok {
return errors.New("unknown attribute '" + XMLNameToString(attr.Name) +
"' on XML element '" + FullName(e, ".", false) + "'")
}
}
// Try to parse the attribute
val, err := ParseAttr(field.Value.Type(), attr.Value)
if err != nil {
return errors.New(fmt.Sprintf("error parsing attribute '"+
XMLNameToString(attr.Name)+"' on XML element '"+
FullName(e, ".", false)+"': %+v", err))
}
// Otherwise set the value
field.Value.Set(val)
// Set the field as set
field.Set = true
}
// Iterate over the fields
for attrName, field := range fields {
// If the field isn't optional but wasn't set
if !field.Optional && !field.Set {
return errors.New("no '" + XMLNameToString(attrName) + "' attribute on " +
"XML element '" + FullName(e, ".", false) + "'")
}
}
return nil
}