This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
archiverutils.go
79 lines (68 loc) · 2.45 KB
/
archiverutils.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
package nskeyedarchiver
import (
"bytes"
"encoding/json"
"fmt"
"log"
plist "howett.net/plist"
)
//toUidList type asserts a []interface{} to a []plist.UID by iterating through the list.
func toUidList(list []interface{}) []plist.UID {
l := len(list)
result := make([]plist.UID, l)
for i := 0; i < l; i++ {
result[i] = list[i].(plist.UID)
}
return result
}
//plistFromBytes decodes a binary or XML based PLIST using the amazing github.com/DHowett/go-plist library and returns an interface{} or propagates the error raised by the library.
func plistFromBytes(plistBytes []byte) (interface{}, error) {
var test interface{}
decoder := plist.NewDecoder(bytes.NewReader(plistBytes))
err := decoder.Decode(&test)
if err != nil {
return test, err
}
return test, nil
}
//ToPlist converts a given struct to a Plist using the
//github.com/DHowett/go-plist library. Make sure your struct is exported.
//It returns a string containing the plist.
func ToPlist(data interface{}) string {
buf := &bytes.Buffer{}
encoder := plist.NewEncoder(buf)
encoder.Encode(data)
return buf.String()
}
//Print an object as JSON for debugging purposes, careful log.Fatals on error
func printAsJSON(obj interface{}) {
b, err := json.MarshalIndent(obj, "", " ")
if err != nil {
log.Fatalf("Error while marshalling Json:%s", err)
}
fmt.Print(string(b))
}
//verifyCorrectArchiver makes sure the nsKeyedArchived plist has all the right keys and values and returns an error otherwise
func verifyCorrectArchiver(nsKeyedArchiverData map[string]interface{}) error {
if val, ok := nsKeyedArchiverData[archiverKey]; !ok {
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", archiverKey)
} else {
if stringValue := val.(string); stringValue != nsKeyedArchiver {
return fmt.Errorf("Invalid value: %s for key '%s', expected: '%s'", stringValue, archiverKey, nsKeyedArchiver)
}
}
if _, ok := nsKeyedArchiverData[topKey]; !ok {
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", topKey)
}
if _, ok := nsKeyedArchiverData[objectsKey]; !ok {
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", objectsKey)
}
if val, ok := nsKeyedArchiverData[versionKey]; !ok {
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", versionKey)
} else {
if stringValue := val.(uint64); stringValue != versionValue {
return fmt.Errorf("Invalid value: %d for key '%s', expected: '%d'", stringValue, versionKey, versionValue)
}
}
return nil
}