-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
archiver.go
149 lines (124 loc) · 3.5 KB
/
archiver.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
package nskeyedarchiver
import (
"fmt"
"reflect"
"howett.net/plist"
)
/*
type NSKeyedObject struct {
isPrimitive bool
primitive interface{}
}
*/
func ArchiveXML(object interface{}) (string, error) {
plist, err := archiveObject(object)
if err != nil {
return "", err
}
return toPlist(plist)
}
func ArchiveBin(object interface{}) ([]byte, error) {
plist, err := archiveObject(object)
if err != nil {
return []byte{}, err
}
return toBinaryPlist(plist)
}
func archiveObject(object interface{}) (interface{}, error) {
SetupEncoders()
archiverSkeleton := createSkeleton(true)
objects := make([]interface{}, 1)
objects[0] = null
objects, _ = archive(object, objects)
archiverSkeleton[objectsKey] = objects
return archiverSkeleton, nil
}
func createSkeleton(withRoot bool) map[string]interface{} {
var topDict map[string]interface{}
if withRoot {
topDict = map[string]interface{}{"root": plist.UID(1)}
}
return map[string]interface{}{
versionKey: versionValue,
archiverKey: NsKeyedArchiver,
topKey: topDict,
}
}
func archive(object interface{}, objects []interface{}) ([]interface{}, plist.UID) {
if object, ok := isPrimitiveObject(object); ok {
index := len(objects)
objects = append(objects, object)
return objects, plist.UID(index)
}
if v, ok := object.([]interface{}); ok {
return serializeArray(v, objects)
}
if v, ok := object.(map[string]interface{}); ok {
return serializeMap(v, objects, buildClassDict("NSDictionary", "NSObject"))
}
typeOf := reflect.TypeOf(object)
name := typeOf.Name()
//seems like Name() can be empty for pointer types
if name == "" {
name = typeOf.String()
}
if encoderFunc, ok := encodableClasses[name]; ok {
return encoderFunc(object, objects)
}
panic(fmt.Errorf("NSKeyedArchiver Unsupported object: '%s' of type:%s", object, typeOf))
}
func serializeArray(array []interface{}, objects []interface{}) ([]interface{}, plist.UID) {
arrayDict := map[string]interface{}{}
arrayObjectIndex := len(objects)
objects = append(objects, arrayDict)
classDefinitionIndex := len(objects)
objects = append(objects, buildClassDict("NSArray", "NSObject"))
arrayDict["$class"] = plist.UID(classDefinitionIndex)
itemRefs := make([]plist.UID, len(array))
for index, item := range array {
var uid plist.UID
objects, uid = archive(item, objects)
itemRefs[index] = uid
}
arrayDict["NS.objects"] = itemRefs
return objects, plist.UID(arrayObjectIndex)
}
func serializeMap(mapObject map[string]interface{}, objects []interface{}, classDict map[string]interface{}) ([]interface{}, plist.UID) {
dictDict := map[string]interface{}{}
dictionaryRef := len(objects)
objects = append(objects, dictDict)
index := len(objects)
objects = append(objects, classDict)
dictDict["$class"] = plist.UID(index)
keyRefs := make([]plist.UID, len(mapObject))
i := 0
keys := make([]string, len(mapObject))
for k := range mapObject {
keys[i] = k
i++
}
index = 0
for _, key := range keys {
var uid plist.UID
objects, uid = archive(key, objects)
keyRefs[index] = uid
index++
}
dictDict["NS.keys"] = keyRefs
index = 0
valueRefs := make([]plist.UID, len(mapObject))
for _, key := range keys {
var uid plist.UID
objects, uid = archive(mapObject[key], objects)
valueRefs[index] = uid
index++
}
dictDict["NS.objects"] = valueRefs
return objects, plist.UID(dictionaryRef)
}
func isArray(object interface{}) bool {
return reflect.TypeOf(object).Kind() == reflect.Array
}
func isMap(object interface{}) bool {
return reflect.TypeOf(object).Kind() == reflect.Map
}