-
Notifications
You must be signed in to change notification settings - Fork 72
/
Facade_Nested.ahk
215 lines (203 loc) · 5.11 KB
/
Facade_Nested.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
#Include <IsFuncObj>
#Include <Validate>
#Include <Op>
#Include <Array>
_Nested_Blame(Sig, Func)
{
local
try
{
Result := Func.Call()
}
catch E
{
if (E["Message"] == "Key Error")
{
throw Exception("Key Error", -2
,Sig . " Invalid path.")
}
else if ( E["Message"] == "Type Error"
and E["Extra"] == "Invalid path.")
{
throw Exception("Type Error", -2
,Sig . " Invalid path.")
}
else
{
throw E
}
}
return Result
}
Nested_Count(Path, Dict)
{
local
Sig := "Nested_Count(Path, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_ObjArg(Sig, "Dict", Dict)
Result := _Nested_Blame(Sig
,Func("Nested_Get")
.Bind(Path
,Dict)).Count()
if (Result == "")
{
throw Exception("Type Error", -1
,Sig . " Invalid path.")
}
return Result
}
Nested_HasKey(Path, Dict)
{
local
Sig := "Nested_HasKey(Path, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_ObjArg(Sig, "Dict", Dict)
Result := true
Index := 1
CurrentObject := Dict
while (Result and Index <= Path.Length())
{
; Return false instead of an empty string when HasKey(Key) does not
; exist.
Result := CurrentObject.HasKey(Path[Index]) ? true : false
if (Result)
{
CurrentObject := Op_Get(CurrentObject, Path[Index])
++Index
}
}
return Result
}
Nested_Get(Path, Dict)
{
local
Sig := "Nested_Get(Path, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_ObjArg(Sig, "Dict", Dict)
return _Nested_Blame(Sig
,Func("Array_FoldL")
.Bind(Func("Op_Get")
,Dict
,Path))
}
_Nested_SetAux(Value, Dict, Key)
{
local
if (IsFuncObj(Dict.Set))
{
Dict.Set(Key, Value)
}
else
{
Dict[Key] := Value
}
return Dict
}
_Nested_ReconstructAux(Path, Index, PreviousClone, PreviousKey, Func)
{
local
if (Index == 1)
{
Clone := PreviousClone.Clone()
}
else
{
Clone := Op_Get(PreviousClone, PreviousKey).Clone()
}
if (Clone == "")
{
throw Exception("Type Error",
,"Invalid path.")
}
Key := Path[Index]
if (Index == Path.Length())
{
Result := Func.Call(Clone, Key)
}
else
{
Result := _Nested_SetAux(_Nested_ReconstructAux(Path
,Index + 1
,Clone
,Key
,Func)
,Clone
,Key)
}
return Result
}
_Nested_Reconstruct(Path, Func, Dict)
{
local
if (Path.Length() == 0)
{
; Mutation-equivalent functions should always return a copy.
Result := Dict.Clone()
if (Result == "")
{
throw Exception("Type Error",
,"Invalid path.")
}
}
else
{
Result := _Nested_ReconstructAux(Path
,1
,Dict
,""
,Func)
}
return Result
}
Nested_Set(Path, Value, Dict)
{
local
Sig := "Nested_Set(Path, Value, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_ObjArg(Sig, "Dict", Dict)
return _Nested_Blame(Sig
,Func("_Nested_Reconstruct")
.Bind(Path
,Func("_Nested_SetAux").Bind(Value)
,Dict))
}
_Nested_UpdateAux(Func, Dict, Key)
{
local
return _Nested_SetAux(Func.Call(Op_Get(Dict, Key)), Dict, Key)
}
Nested_Update(Path, Func, Dict)
{
local
Sig := "Nested_Update(Path, Func, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_FuncArg(Sig, "Func", Func)
_Validate_ObjArg(Sig, "Dict", Dict)
return _Nested_Blame(Sig
,Func("_Nested_Reconstruct")
.Bind(Path
,Func("_Nested_UpdateAux").Bind(Func)
,Dict))
}
_Nested_DeleteAux(Dict, Key)
{
local
if (not Dict.HasKey(Key))
{
throw Exception("Key Error")
}
Dict.Delete(Key)
return Dict
}
Nested_Delete(Path, Dict)
{
local
Sig := "Nested_Delete(Path, Dict)"
_Validate_ArrayArg(Sig, "Path", Path)
_Validate_ObjArg(Sig, "Dict", Dict)
return _Nested_Blame(Sig
,Func("_Nested_Reconstruct")
.Bind(Path
,Func("_Nested_DeleteAux")
,Dict))
}