-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
351 lines (320 loc) · 9.14 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
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
package plist
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"time"
)
// MarshalFunc is a function used to Unmarshal custom plist types.
type MarshalFunc func(interface{}) error
type Unmarshaler interface {
UnmarshalPlist(f func(interface{}) error) error
}
// Unmarshal parses the plist-encoded data and stores the result in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
// Check for binary plist here before setting up the decoder.
if bytes.HasPrefix(data, []byte("bplist0")) {
return NewBinaryDecoder(bytes.NewReader(data)).Decode(v)
}
return NewXMLDecoder(bytes.NewReader(data)).Decode(v)
}
// A Decoder reads and decodes Apple plist objects from an input stream.
// The plists can be in XML or binary format.
type Decoder struct {
reader io.Reader // binary decoders assert this to io.ReadSeeker
isBinary bool // true if this is a binary plist
}
// NewDecoder returns a new XML plist decoder.
// DEPRECATED: Please use NewXMLDecoder instead.
func NewDecoder(r io.Reader) *Decoder {
return NewXMLDecoder(r)
}
// NewXMLDecoder returns a new decoder that reads an XML plist from r.
func NewXMLDecoder(r io.Reader) *Decoder {
return &Decoder{reader: r, isBinary: false}
}
// NewBinaryDecoder returns a new decoder that reads a binary plist from r.
// No error checking is done to make sure that r is actually a binary plist.
func NewBinaryDecoder(r io.ReadSeeker) *Decoder {
return &Decoder{reader: r, isBinary: true}
}
// Decode reads the next plist-encoded value from its input and stores it in
// the value pointed to by v. Decode uses xml.Decoder to do the heavy lifting
// for XML plists, and uses binaryParser for binary plists.
func (d *Decoder) Decode(v interface{}) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return errors.New("plist: non-pointer passed to Unmarshal")
}
var pval *plistValue
if d.isBinary {
// For binary decoder, type assert the reader to an io.ReadSeeker
var err error
r, ok := d.reader.(io.ReadSeeker)
if !ok {
return fmt.Errorf("binary plist decoder requires an io.ReadSeeker")
}
parser, err := newBinaryParser(r)
if err != nil {
return err
}
pval, err = parser.parseDocument()
if err != nil {
return err
}
} else {
var err error
parser := newXMLParser(d.reader)
pval, err = parser.parseDocument(nil)
if err != nil {
return err
}
}
return d.unmarshal(pval, val.Elem())
}
func (d *Decoder) unmarshal(pval *plistValue, v reflect.Value) error {
// check for empty interface v type
if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
val := reflect.ValueOf(d.valueInterface(pval))
if !val.IsValid() {
return fmt.Errorf("plist: invalid reflect.Value %v", v)
}
v.Set(val)
return nil
}
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
unmarshalerType := reflect.TypeOf((*Unmarshaler)(nil)).Elem()
if v.CanInterface() && v.Type().Implements(unmarshalerType) {
u := v.Interface().(Unmarshaler)
return u.UnmarshalPlist(
func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
},
)
}
if v.CanAddr() {
pv := v.Addr()
if pv.CanInterface() && pv.Type().Implements(unmarshalerType) {
u := pv.Interface().(Unmarshaler)
return u.UnmarshalPlist(
func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
},
)
}
}
// change pointer values to the correct type
// ex type foo struct {
// Foo *string `plist:"foo"
// }
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
switch pval.kind {
case String:
return d.unmarshalString(pval, v)
case Dictionary:
return d.unmarshalDictionary(pval, v)
case Array:
return d.unmarshalArray(pval, v)
case Boolean:
return d.unmarshalBoolean(pval, v)
case Real:
return d.unmarshalReal(pval, v)
case Integer:
return d.unmarshalInteger(pval, v)
case Data:
return d.unmarshalData(pval, v)
case Date:
return d.unmarshalDate(pval, v)
default:
return fmt.Errorf("plist: %v is an unsuported plist element kind", pval.kind)
}
}
func (d *Decoder) unmarshalDate(pval *plistValue, v reflect.Value) error {
if v.Type() != reflect.TypeOf((*time.Time)(nil)).Elem() {
return UnmarshalTypeError{fmt.Sprintf("%v", pval.value), v.Type()}
}
v.Set(reflect.ValueOf(pval.value.(time.Time)))
return nil
}
func (d *Decoder) unmarshalData(pval *plistValue, v reflect.Value) error {
if v.Kind() != reflect.Slice || v.Type().Elem().Kind() != reflect.Uint8 {
return UnmarshalTypeError{fmt.Sprintf("%s", pval.value.([]byte)), v.Type()}
}
v.SetBytes(pval.value.([]byte))
return nil
}
func (d *Decoder) unmarshalReal(pval *plistValue, v reflect.Value) error {
if v.Kind() != reflect.Float32 && v.Kind() != reflect.Float64 {
return UnmarshalTypeError{fmt.Sprintf("%v", pval.value.(sizedFloat).value), v.Type()}
}
v.SetFloat(pval.value.(sizedFloat).value)
return nil
}
func (d *Decoder) unmarshalBoolean(pval *plistValue, v reflect.Value) error {
if v.Kind() != reflect.Bool {
return UnmarshalTypeError{fmt.Sprintf("%v", pval.value), v.Type()}
}
v.SetBool(pval.value.(bool))
return nil
}
func (d *Decoder) unmarshalDictionary(pval *plistValue, v reflect.Value) error {
subvalues := pval.value.(*dictionary).m
switch v.Kind() {
case reflect.Struct:
fields := cachedTypeFields(v.Type())
for _, field := range fields {
if _, ok := subvalues[field.name]; !ok {
continue
}
if err := d.unmarshal(subvalues[field.name], field.value(v)); err != nil {
return err
}
}
case reflect.Map:
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
for k, sval := range subvalues {
keyv := reflect.ValueOf(k).Convert(v.Type().Key())
mapElem := v.MapIndex(keyv)
if !mapElem.IsValid() {
mapElem = reflect.New(v.Type().Elem()).Elem()
}
if err := d.unmarshal(sval, mapElem); err != nil {
return err
}
v.SetMapIndex(keyv, mapElem)
}
default:
return UnmarshalTypeError{"dict", v.Type()}
}
return nil
}
func (d *Decoder) unmarshalString(pval *plistValue, v reflect.Value) error {
if v.Kind() != reflect.String {
return UnmarshalTypeError{fmt.Sprintf("%s", pval.value.(string)), v.Type()}
}
v.SetString(pval.value.(string))
return nil
}
func (d *Decoder) unmarshalArray(pval *plistValue, v reflect.Value) error {
subvalues := pval.value.([]*plistValue)
switch v.Kind() {
case reflect.Slice:
// Slice of element values.
// Grow slice.
// Borrowed from https://golang.org/src/encoding/xml/read.go
cnt := len(subvalues)
if cnt >= v.Cap() {
ncap := 2 * cnt
if ncap < 4 {
ncap = 4
}
new := reflect.MakeSlice(v.Type(), v.Len(), ncap)
reflect.Copy(new, v)
v.Set(new)
}
n := v.Len()
v.SetLen(cnt)
for _, sval := range subvalues {
if err := d.unmarshal(sval, v.Index(n)); err != nil {
v.SetLen(cnt)
return err
}
n++
}
default:
return UnmarshalTypeError{"array", v.Type()}
}
return nil
}
func (d *Decoder) unmarshalInteger(pval *plistValue, v reflect.Value) error {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v.SetInt(int64(pval.value.(signedInt).value))
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr:
// Make sure plistValue isn't negative when decoding into uint.
if pval.value.(signedInt).signed {
return UnmarshalTypeError{
fmt.Sprintf("%v", int64(pval.value.(signedInt).value)), v.Type(),
}
}
v.SetUint(pval.value.(signedInt).value)
default:
return UnmarshalTypeError{
fmt.Sprintf("%v", pval.value.(signedInt).value), v.Type(),
}
}
return nil
}
// empty interface values
// borrowed from go-plist
func (d *Decoder) valueInterface(pval *plistValue) interface{} {
switch pval.kind {
case String:
return pval.value.(string)
case Integer:
if pval.value.(signedInt).signed {
return int64(pval.value.(signedInt).value)
}
return pval.value.(signedInt).value
case Real:
bits := pval.value.(sizedFloat).bits
switch bits {
case 32:
return float32(pval.value.(sizedFloat).value)
case 64:
return pval.value.(sizedFloat).value
default:
return nil
}
case Boolean:
return pval.value.(bool)
case Array:
return d.arrayInterface(pval.value.([]*plistValue))
case Dictionary:
return d.dictionaryInterface(pval.value.(*dictionary))
case Data:
return pval.value.([]byte)
case Date:
return pval.value.(time.Time)
default:
return nil
}
}
func (d *Decoder) arrayInterface(subvalues []*plistValue) []interface{} {
out := make([]interface{}, len(subvalues))
for i, subv := range subvalues {
out[i] = d.valueInterface(subv)
}
return out
}
func (d *Decoder) dictionaryInterface(dict *dictionary) map[string]interface{} {
out := make(map[string]interface{})
for k, subv := range dict.m {
out[k] = d.valueInterface(subv)
}
return out
}
// An UnmarshalTypeError describes a plist value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // description of plist value - "true", "string", "date"
Type reflect.Type
}
func (e UnmarshalTypeError) Error() string {
return "plist: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
}