forked from oqapps/Qlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplist.go
158 lines (145 loc) · 3.91 KB
/
plist.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/oq-x/go-plist"
)
type Entry struct {
key string
value interface{}
children []Entry
childrenPaths []string
path string
array bool
isParent bool
index int
}
var plistData plist.OrderedDict
var arrayPlist []interface{}
type Entries = map[string]Entry
func GetEntry(entries Entries, index int) Entry {
var entry Entry
for _, e := range entries {
if e.index == index {
entry = e
break
}
}
return entry
}
func ValueToDict(entry Entry) plist.OrderedDict {
dict := plist.OrderedDict{}
for _, e := range entry.children {
dict.Keys = append(dict.Keys, e.key)
if e.isParent {
dict.Values = append(dict.Values, ValueToDict(e))
} else {
dict.Values = append(dict.Values, e.value)
}
}
return dict
}
func ParseEntries(entries Entries) plist.OrderedDict {
dict := plist.OrderedDict{}
for _, entry := range entries {
dict.Keys = append(dict.Keys, entry.key)
if entry.isParent {
dict.Values = append(dict.Values, ValueToDict(entry))
} else {
dict.Values = append(dict.Values, entry.value)
}
}
return dict
}
func Get(dict plist.OrderedDict, key string) interface{} {
var index int
for i, k := range dict.Keys {
if k == key {
index = i
break
}
}
return dict.Values[index]
}
/*func (entry Entry) SetValue(value interface{}) {
entry.value = value
var element = Get(plistData, entry.path[0])
for i, p := range entry.path {
if i == 0 {
continue
}
element = (element.(map[string]interface{}))[p]
}
}*/
func (entry Entry) SetKey(entries Entries, value string) {
entry.key = value
pathSp := strings.Split(entry.path, "\\-\\")
pathSp[len(pathSp)-1] = entry.key
entry.path = strings.Join(pathSp, "\\-\\")
entries[entry.path] = entry
}
func AppendToPath(path string, index ...string) string {
for _, p := range index {
path += "\\-\\" + p
}
return path
}
func Parse(key string, data interface{}, path string, index int, entries Entries) Entry {
var children []Entry
var childrenPaths []string
switch v := data.(type) {
case []interface{}:
for i, item := range v {
children = append(children, Parse(fmt.Sprintf("%d", i), item, AppendToPath(path, fmt.Sprintf("%d", i)), index, entries))
childrenPaths = append(childrenPaths, AppendToPath(path, fmt.Sprintf("%d", i)))
}
entry := Entry{key: key, children: children, index: index, path: path, isParent: true, childrenPaths: childrenPaths}
entries[path] = entry
return entry
case plist.OrderedDict:
for f, a := range v.Keys {
p := AppendToPath(path, a)
value := v.Values[f]
x, ok := value.(plist.OrderedDict)
var ch []Entry
var chp []string
if ok {
for c, k := range x.Keys {
m := x.Values[c]
ch = append(ch, Parse(k, m, AppendToPath(p, k), index, entries))
chp = append(chp, AppendToPath(p, k))
}
e := Entry{key: a, children: ch, path: p, index: f, isParent: true, childrenPaths: chp}
children = append(children, e)
childrenPaths = append(childrenPaths, p)
entries[p] = e
} else {
m, ok := value.([]interface{})
if ok {
for i, q := range m {
in := strconv.Itoa(i)
ch = append(ch, Parse(in, q, AppendToPath(p, in), index, entries))
chp = append(chp, AppendToPath(p, in))
}
e := Entry{key: a, children: ch, path: p, index: f, array: true, isParent: true, childrenPaths: chp}
children = append(children, e)
childrenPaths = append(childrenPaths, p)
entries[p] = e
} else {
e := Entry{key: a, value: value, path: p, index: f}
children = append(children, e)
childrenPaths = append(childrenPaths, p)
entries[p] = e
}
}
}
entry := Entry{key: key, children: children, childrenPaths: childrenPaths, path: path, index: index, isParent: true}
entries[path] = entry
return entry
default:
entry := Entry{key: key, value: v, path: path, index: index}
entries[path] = entry
return entry
}
}