-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LayeredDataBase.ahk
338 lines (267 loc) · 8.55 KB
/
LayeredDataBase.ahk
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
This is a base class representing a set of data that has multiple layers.
Data can be retrieved from any layer, but the main benefit is that the layers can be combined
into one set of data, where each layer overwrites the values of the layer underneath it.
Finally, processors can be applied after the layers are merged.
Example:
- Layer 1: Initial defaults
- Layer 2: Defaults from datasource
- Layer 3: Auto-detected defaults
- Layer 4: User configuration values
- Processor 1: Token expander
- Processor 2: File locator
*/
class LayeredDataBase {
layers := Map()
layerPriority := []
processors := []
original := ""
__New(processors, params*) {
if (processors != "") {
this.processors := processors
}
if (params.Has(1)) {
this.SetLayers(params*)
}
this.LoadValues()
}
LoadValues() {
; Optional hook for subclasses to load data into one or more layers
}
SetLayers(params*) {
nextParam := 1
while (params.Has(nextParam)) {
layerName := params[nextParam]
layerData := params[nextParam + 1]
this.SetLayer(layerName, layerData)
nextParam := nextParam + 2
}
}
SetLayer(layerName, data, layerPriority := "") {
alreadyExists := this.layers.Has(layerName)
this.layers[layerName] := data
if (!alreadyExists) {
if (layerPriority == "") {
this.layerPriority.Push(layerName)
} else {
this.layerPriority.InsertAt(layerPriority, layerName)
}
}
}
GetLayer(layerName) {
return this.layers.Has(layerName) ? this.layers[layerName] : Map()
}
DeleteLayer(layerName) {
if (this.layers.Has(layerName)) {
this.layers.Delete(layerName)
for index, value in this.layerPriority {
if (layerName == value) {
this.layerPriority.RemoveAt(index)
break
}
}
}
}
/**
key: The key to retrieve
processValue: Whether or not to apply the processors to the returned value
layer: The layer to retrieve data from. The default if empty is all layers (with
the topmost layer that has a value winning out)
*/
GetValue(key, processValue := true, layer := "") {
value := ""
if (layer != "") {
if (this.layers.Has(layer) && this.layers[layer].Has(key)) {
value := this.layers[layer][key]
}
} else {
for index, layerName in this.layerPriority {
if (this.layers[layerName].Has(key)) {
value := this.layers[layerName][key]
}
}
}
if (processValue) {
value := this.ApplyProcessors(value)
}
return value
}
HasValue(key, layer := "", allowEmpty := true) {
hasValue := false
if (layer != "") {
hasValue := (this.layers.Has(layer) && this.layers[layer].Has(key))
} else {
for index, layerName in this.layerPriority {
if (this.layers[layerName].Has(key)) {
if (allowEmpty || this.layers[layerName][key] != "") {
hasValue := true
break
}
}
}
}
return hasValue
}
ApplyProcessors(value) {
for index, processor in this.processors {
value := this.ApplyProcessor(processor, value)
}
return value
}
ApplyProcessor(processor, value) {
return processor.Process(value)
}
/**
key: The key to set the value for
value: The new value to set
layer: The layer to set the value to. The default if empty is the topmost layer.
*/
SetValue(key, value, layer := "") {
if (layer == "") {
layer := this.GetTopLayer()
}
if (this.layers.Has(layer)) {
this.layers[layer][key] := value
}
}
DeleteValue(key, layer := "") {
if (layer == "") {
layer := this.GetTopLayer()
}
if (this.layers.Has(layer) && this.layers[layer].Has(key)) {
this.layers[layer].Delete(key)
}
}
GetTopLayer() {
layer := ""
for index, layerName in this.layerPriority {
layer := layerName
}
return layer
}
GetMergedData(process := true) {
data := Map()
for index, layer in this.layerPriority {
data := this.MergeData(data, this.layers[layer], true)
}
if (process) {
for key, value in data {
data[key] := this.ApplyProcessors(value)
}
}
return data
}
MergeData(data, otherData, overwrite := true) {
for key, value in otherData {
if (!data.Has(key) || overwrite) {
data[key] := value
}
}
return data
}
StoreOriginal(update := false) {
if (this.original == "" || update) {
this.original := this.CloneLayers()
}
return this.original
}
RestoreFromOriginal() {
if (this.original != "") {
this.layers := this.CloneLayers(this.original)
}
}
CloneLayers(layers := "") {
if (layers == "") {
layers := this.layers
} else if (Type(layers) == "String") {
layers := Map(layers, this.layers[layers])
}
cloned := Map()
for key, layer in layers {
cloned[key] := this.CloneData(layer)
}
return cloned
}
CloneData(data) {
return data.Clone()
}
/**
Diffs changes between the requested layer or all layers merged (default if blank)
*/
DiffChanges(layer := "") {
currentData := (layer == "") ? this.GetMergedData(false) : this.layers[layer]
originalData := Map()
added := Map()
modified := Map()
removed := Map()
if (this.original != "") {
if (layer == "") {
for key, data in this.original {
originalData := this.MergeData(originalData, data, true)
}
} else if this.original.Has(layer) {
originalData := this.original[layer]
}
}
if (originalData.Count == 0) {
added := this.CloneData(currentData)
} else {
for key, value in currentData {
if (originalData.Has(key)) {
if (value != originalData[key]) {
modified[key] := value
}
} else {
added[key] := value
}
}
for key, value in originalData {
if (!currentData.Has(key)) {
removed[key] := value
}
}
}
return DiffResult(added, modified, removed)
}
; TODO: Replace with a call to Debugger's Inspect method
DebugData(layer := "") {
layers := (layer == "") ? this.layers : Map(layer, this.layers[layer])
output := ""
for layer, layerData in layers {
if (output) {
output .= "`n"
}
output .= layer . "{`n"
for key, value in layerData {
output .= "`t" . key . ": " . this.DebugValue(value) . "`n"
}
output .= "}`n"
}
if (output) {
MsgBox(output)
}
}
DebugValue(val, indent := "`t") {
output := val
if (Type(val) == "Array") {
output := indent . "Array {`n"
for index, value in val {
output .= indent . "`t" . index . ": " . this.DebugValue(value, indent . "`t") . "`n"
}
output .= indent . "}`n"
} else if (Type(val) == "Map") {
output := indent . "Map {`n"
for key, value in val {
output .= indent . "`t" . key . ": " . this.DebugValue(value, indent . "`t") . "`n"
}
output .= indent . "}`n"
} else if (IsObject(val)) {
output := indent . "Object {`n"
for key, value in val.OwnProps() {
output .= indent . "`t" . key . ": " . this.DebugValue(value, indent . "`t") . "`n"
}
output .= indent . "}`n"
}
return output
}
}