| 1 |
package plist |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"runtime" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
type incompatibleDecodeTypeError struct { |
| 12 |
dest reflect.Type |
| 13 |
src string |
| 14 |
} |
| 15 |
|
| 16 |
func (u *incompatibleDecodeTypeError) Error() string { |
| 17 |
return fmt.Sprintf("plist: type mismatch: tried to decode plist type `%v' into value of type `%v'", u.src, u.dest) |
| 18 |
} |
| 19 |
|
| 20 |
var ( |
| 21 |
plistUnmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() |
| 22 |
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() |
| 23 |
uidType = reflect.TypeOf(UID(0)) |
| 24 |
) |
| 25 |
|
| 26 |
func isEmptyInterface(v reflect.Value) bool { |
| 27 |
return v.Kind() == reflect.Interface && v.NumMethod() == 0 |
| 28 |
} |
| 29 |
|
| 30 |
func (p *Decoder) unmarshalPlistInterface(pval cfValue, unmarshalable Unmarshaler) { |
| 31 |
err := unmarshalable.UnmarshalPlist(func(i interface{}) (err error) { |
| 32 |
defer func() { |
| 33 |
if r := recover(); r != nil { |
| 34 |
if _, ok := r.(runtime.Error); ok { |
| 35 |
panic(r) |
| 36 |
} |
| 37 |
err = r.(error) |
| 38 |
} |
| 39 |
}() |
| 40 |
p.unmarshal(pval, reflect.ValueOf(i)) |
| 41 |
return |
| 42 |
}) |
| 43 |
|
| 44 |
if err != nil { |
| 45 |
panic(err) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (p *Decoder) unmarshalTextInterface(pval cfString, unmarshalable encoding.TextUnmarshaler) { |
| 50 |
err := unmarshalable.UnmarshalText([]byte(pval)) |
| 51 |
if err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func (p *Decoder) unmarshalTime(pval cfDate, val reflect.Value) { |
| 57 |
val.Set(reflect.ValueOf(time.Time(pval))) |
| 58 |
} |
| 59 |
|
| 60 |
func (p *Decoder) unmarshalLaxString(s string, val reflect.Value) { |
| 61 |
switch val.Kind() { |
| 62 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 63 |
i := mustParseInt(s, 10, 64) |
| 64 |
val.SetInt(i) |
| 65 |
return |
| 66 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 67 |
i := mustParseUint(s, 10, 64) |
| 68 |
val.SetUint(i) |
| 69 |
return |
| 70 |
case reflect.Float32, reflect.Float64: |
| 71 |
f := mustParseFloat(s, 64) |
| 72 |
val.SetFloat(f) |
| 73 |
return |
| 74 |
case reflect.Bool: |
| 75 |
b := mustParseBool(s) |
| 76 |
val.SetBool(b) |
| 77 |
return |
| 78 |
case reflect.Struct: |
| 79 |
if val.Type() == timeType { |
| 80 |
t, err := time.Parse(textPlistTimeLayout, s) |
| 81 |
if err != nil { |
| 82 |
panic(err) |
| 83 |
} |
| 84 |
val.Set(reflect.ValueOf(t.In(time.UTC))) |
| 85 |
return |
| 86 |
} |
| 87 |
fallthrough |
| 88 |
default: |
| 89 |
panic(&incompatibleDecodeTypeError{val.Type(), "string"}) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (p *Decoder) unmarshal(pval cfValue, val reflect.Value) { |
| 94 |
if pval == nil { |
| 95 |
return |
| 96 |
} |
| 97 |
|
| 98 |
if val.Kind() == reflect.Ptr { |
| 99 |
if val.IsNil() { |
| 100 |
val.Set(reflect.New(val.Type().Elem())) |
| 101 |
} |
| 102 |
val = val.Elem() |
| 103 |
} |
| 104 |
|
| 105 |
if isEmptyInterface(val) { |
| 106 |
v := p.valueInterface(pval) |
| 107 |
val.Set(reflect.ValueOf(v)) |
| 108 |
return |
| 109 |
} |
| 110 |
|
| 111 |
incompatibleTypeError := &incompatibleDecodeTypeError{val.Type(), pval.typeName()} |
| 112 |
|
| 113 |
|
| 114 |
if date, ok := pval.(cfDate); ok { |
| 115 |
if val.Type() == timeType { |
| 116 |
p.unmarshalTime(date, val) |
| 117 |
return |
| 118 |
} |
| 119 |
panic(incompatibleTypeError) |
| 120 |
} |
| 121 |
|
| 122 |
if receiver, can := implementsInterface(val, plistUnmarshalerType); can { |
| 123 |
p.unmarshalPlistInterface(pval, receiver.(Unmarshaler)) |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
if val.Type() != timeType { |
| 128 |
if receiver, can := implementsInterface(val, textUnmarshalerType); can { |
| 129 |
if str, ok := pval.(cfString); ok { |
| 130 |
p.unmarshalTextInterface(str, receiver.(encoding.TextUnmarshaler)) |
| 131 |
} else { |
| 132 |
panic(incompatibleTypeError) |
| 133 |
} |
| 134 |
return |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
typ := val.Type() |
| 139 |
|
| 140 |
switch pval := pval.(type) { |
| 141 |
case cfString: |
| 142 |
if val.Kind() == reflect.String { |
| 143 |
val.SetString(string(pval)) |
| 144 |
return |
| 145 |
} |
| 146 |
if p.lax { |
| 147 |
p.unmarshalLaxString(string(pval), val) |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
panic(incompatibleTypeError) |
| 152 |
case *cfNumber: |
| 153 |
switch val.Kind() { |
| 154 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 155 |
val.SetInt(int64(pval.value)) |
| 156 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 157 |
val.SetUint(pval.value) |
| 158 |
default: |
| 159 |
panic(incompatibleTypeError) |
| 160 |
} |
| 161 |
case *cfReal: |
| 162 |
if val.Kind() == reflect.Float32 || val.Kind() == reflect.Float64 { |
| 163 |
|
| 164 |
val.SetFloat(pval.value) |
| 165 |
} else { |
| 166 |
panic(incompatibleTypeError) |
| 167 |
} |
| 168 |
case cfBoolean: |
| 169 |
if val.Kind() == reflect.Bool { |
| 170 |
val.SetBool(bool(pval)) |
| 171 |
} else { |
| 172 |
panic(incompatibleTypeError) |
| 173 |
} |
| 174 |
case cfData: |
| 175 |
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { |
| 176 |
panic(incompatibleTypeError) |
| 177 |
} |
| 178 |
|
| 179 |
if typ.Elem().Kind() != reflect.Uint8 { |
| 180 |
panic(incompatibleTypeError) |
| 181 |
} |
| 182 |
|
| 183 |
b := []byte(pval) |
| 184 |
switch val.Kind() { |
| 185 |
case reflect.Slice: |
| 186 |
val.SetBytes(b) |
| 187 |
case reflect.Array: |
| 188 |
if val.Len() < len(b) { |
| 189 |
panic(fmt.Errorf("plist: attempted to unmarshal %d bytes into a byte array of size %d", len(b), val.Len())) |
| 190 |
} |
| 191 |
sval := reflect.ValueOf(b) |
| 192 |
reflect.Copy(val, sval) |
| 193 |
} |
| 194 |
case cfUID: |
| 195 |
if val.Type() == uidType { |
| 196 |
val.SetUint(uint64(pval)) |
| 197 |
} else { |
| 198 |
switch val.Kind() { |
| 199 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 200 |
val.SetInt(int64(pval)) |
| 201 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 202 |
val.SetUint(uint64(pval)) |
| 203 |
default: |
| 204 |
panic(incompatibleTypeError) |
| 205 |
} |
| 206 |
} |
| 207 |
case *cfArray: |
| 208 |
p.unmarshalArray(pval, val) |
| 209 |
case *cfDictionary: |
| 210 |
p.unmarshalDictionary(pval, val) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func (p *Decoder) unmarshalArray(a *cfArray, val reflect.Value) { |
| 215 |
var n int |
| 216 |
if val.Kind() == reflect.Slice { |
| 217 |
|
| 218 |
|
| 219 |
cnt := len(a.values) + val.Len() |
| 220 |
if cnt >= val.Cap() { |
| 221 |
ncap := 2 * cnt |
| 222 |
if ncap < 4 { |
| 223 |
ncap = 4 |
| 224 |
} |
| 225 |
new := reflect.MakeSlice(val.Type(), val.Len(), ncap) |
| 226 |
reflect.Copy(new, val) |
| 227 |
val.Set(new) |
| 228 |
} |
| 229 |
n = val.Len() |
| 230 |
val.SetLen(cnt) |
| 231 |
} else if val.Kind() == reflect.Array { |
| 232 |
if len(a.values) > val.Cap() { |
| 233 |
panic(fmt.Errorf("plist: attempted to unmarshal %d values into an array of size %d", len(a.values), val.Cap())) |
| 234 |
} |
| 235 |
} else { |
| 236 |
panic(&incompatibleDecodeTypeError{val.Type(), a.typeName()}) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
for _, sval := range a.values { |
| 241 |
p.unmarshal(sval, val.Index(n)) |
| 242 |
n++ |
| 243 |
} |
| 244 |
return |
| 245 |
} |
| 246 |
|
| 247 |
func (p *Decoder) unmarshalDictionary(dict *cfDictionary, val reflect.Value) { |
| 248 |
typ := val.Type() |
| 249 |
switch val.Kind() { |
| 250 |
case reflect.Struct: |
| 251 |
tinfo, err := getTypeInfo(typ) |
| 252 |
if err != nil { |
| 253 |
panic(err) |
| 254 |
} |
| 255 |
|
| 256 |
entries := make(map[string]cfValue, len(dict.keys)) |
| 257 |
for i, k := range dict.keys { |
| 258 |
sval := dict.values[i] |
| 259 |
entries[k] = sval |
| 260 |
} |
| 261 |
|
| 262 |
for _, finfo := range tinfo.fields { |
| 263 |
p.unmarshal(entries[finfo.name], finfo.value(val)) |
| 264 |
} |
| 265 |
case reflect.Map: |
| 266 |
if val.IsNil() { |
| 267 |
val.Set(reflect.MakeMap(typ)) |
| 268 |
} |
| 269 |
|
| 270 |
for i, k := range dict.keys { |
| 271 |
sval := dict.values[i] |
| 272 |
|
| 273 |
keyv := reflect.ValueOf(k).Convert(typ.Key()) |
| 274 |
mapElem := reflect.New(typ.Elem()).Elem() |
| 275 |
|
| 276 |
p.unmarshal(sval, mapElem) |
| 277 |
val.SetMapIndex(keyv, mapElem) |
| 278 |
} |
| 279 |
default: |
| 280 |
panic(&incompatibleDecodeTypeError{typ, dict.typeName()}) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
func (p *Decoder) valueInterface(pval cfValue) interface{} { |
| 286 |
switch pval := pval.(type) { |
| 287 |
case cfString: |
| 288 |
return string(pval) |
| 289 |
case *cfNumber: |
| 290 |
if pval.signed { |
| 291 |
return int64(pval.value) |
| 292 |
} |
| 293 |
return pval.value |
| 294 |
case *cfReal: |
| 295 |
if pval.wide { |
| 296 |
return pval.value |
| 297 |
} else { |
| 298 |
return float32(pval.value) |
| 299 |
} |
| 300 |
case cfBoolean: |
| 301 |
return bool(pval) |
| 302 |
case *cfArray: |
| 303 |
return p.arrayInterface(pval) |
| 304 |
case *cfDictionary: |
| 305 |
return p.dictionaryInterface(pval) |
| 306 |
case cfData: |
| 307 |
return []byte(pval) |
| 308 |
case cfDate: |
| 309 |
return time.Time(pval) |
| 310 |
case cfUID: |
| 311 |
return UID(pval) |
| 312 |
} |
| 313 |
return nil |
| 314 |
} |
| 315 |
|
| 316 |
func (p *Decoder) arrayInterface(a *cfArray) []interface{} { |
| 317 |
out := make([]interface{}, len(a.values)) |
| 318 |
for i, subv := range a.values { |
| 319 |
out[i] = p.valueInterface(subv) |
| 320 |
} |
| 321 |
return out |
| 322 |
} |
| 323 |
|
| 324 |
func (p *Decoder) dictionaryInterface(dict *cfDictionary) map[string]interface{} { |
| 325 |
out := make(map[string]interface{}) |
| 326 |
for i, k := range dict.keys { |
| 327 |
subv := dict.values[i] |
| 328 |
out[k] = p.valueInterface(subv) |
| 329 |
} |
| 330 |
return out |
| 331 |
} |