-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample_unmarshaler_test.go
74 lines (62 loc) · 1.56 KB
/
example_unmarshaler_test.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
package plist_test
import (
"fmt"
"github.com/micromdm/plist"
)
const data = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>typekey</key>
<string>A</string>
<key>typeAkey</key>
<string>VALUE-A</string>
</dict>
</plist>`
type TypeDecider struct {
ActualType interface{} `plist:"-"`
}
type TypeA struct {
TypeAKey string `plist:"typeAkey"`
}
type TypeB struct {
TypeBKey string `plist:"typeBkey"`
}
func (t *TypeDecider) UnmarshalPlist(f func(interface{}) error) error {
// stub struct for decoding a single key to tell which
// specific type we should umarshal into
typeKey := &struct {
TypeKey string `plist:"typekey"`
}{}
if err := f(typeKey); err != nil {
return err
}
// switch using the decoded value to determine the correct type
switch typeKey.TypeKey {
case "A":
t.ActualType = new(TypeA)
case "B":
t.ActualType = new(TypeB)
case "":
return fmt.Errorf("empty typekey (or wrong input data)")
default:
return fmt.Errorf("unknown typekey: %s", typeKey.TypeKey)
}
// decode into the actual type
return f(t.ActualType)
}
// ExampleUnmarshaler demonstrates using structs that use the Unmarshaler interface.
func ExampleUnmarshaler() {
decider := new(TypeDecider)
if err := plist.Unmarshal([]byte(data), decider); err != nil {
fmt.Println(err)
return
}
typeA, ok := decider.ActualType.(*TypeA)
if !ok {
fmt.Println("actual type is not TypeA")
return
}
fmt.Println(typeA.TypeAKey)
// Output: VALUE-A
}