-
Notifications
You must be signed in to change notification settings - Fork 0
/
configer.lua
209 lines (162 loc) · 4.02 KB
/
configer.lua
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
local function verifySubtablesAreUnique(where, tbl, merger, level, key, seen)
seen = seen or {}
key = (key == nil) and "<root>" or key
if type(tbl) ~= "table" or (merger and merger(tbl, nil, true)) then
return
elseif seen[tbl] ~= nil then
error(string.format("repeated table in %s config at keys '%s' and '%s'", where, seen[tbl], key), level + 1)
end
seen[tbl] = key
for k, v in pairs(tbl) do
verifySubtablesAreUnique(where, v, merger, level + 1, k, seen)
end
end
local function deepcopy(tbl, merger, seen)
if merger then
local ok, copied = merger(tbl, nil)
if ok then
return copied
end
end
if type(tbl) ~= "table" then
return tbl
end
seen = seen or {}
local new = {}
seen[tbl] = new
for k, v in pairs(tbl) do
new[k] = seen[v] or deepcopy(v, merger, seen)
end
return new
end
local WEAK_META = { __mode = "k" }
local sets = setmetatable({}, WEAK_META)
local updates = setmetatable({}, WEAK_META)
local defaults = setmetatable({}, WEAK_META)
local NIL = {}
local function make(category, value)
local key = {}
category[key] = value
return key
end
local SET = function(value)
if value == nil then
return NIL
else
return make(sets, value)
end
end
local UPDATE = function(updater, ...)
return make(updates, {
updater = updater,
n = select("#", ...),
...
})
end
local defaultMeta; defaultMeta = {
__index = function(self, key)
local oldPath = defaults[self]
local newPath = deepcopy(oldPath)
table.insert(newPath, key)
local new = setmetatable({}, defaultMeta)
defaults[new] = newPath
return new
end
}
local DEFAULT = setmetatable({}, defaultMeta)
defaults[DEFAULT] = {}
local function retrieve(value, path, level)
local cur = value
for i, key in ipairs(path) do
local t = type(cur)
if t ~= "table" then
local pathString = "DEFAULT." .. table.concat(path, ".", 1, i)
error(string.format("invalid DEFAULT access: %s (trying to index a %s)", pathString, t), level + 1)
end
cur = cur[key]
end
return cur
end
local function _resolve(default, new, merger, topDefault, level)
-- NIL
if new == NIL then
return nil
end
-- SET
local value = sets[new]
if value ~= nil then
return deepcopy(value, merger)
end
-- DEFAULT
local path = defaults[new]
if path then
return deepcopy(retrieve(topDefault, path, level + 1), merger)
end
-- UPDATE
local payload = updates[new]
if payload then
return payload.updater(deepcopy(default, merger), table.unpack(payload, 1, payload.n))
end
-- Merge
if merger then
local ok, merged = merger(new, default)
if ok then
return merged
end
end
if type(new) == "table" and type(default) == "table" then
local merged = {}
-- Copy over all the values we're not merging
for k, v in pairs(default) do
if new[k] == nil then
merged[k] = deepcopy(v, merger)
end
end
-- Merge the remaining values
for k, v in pairs(new) do
merged[k] = _resolve(default[k], v, merger, topDefault, level + 1)
end
return merged
-- Merge incoming table with non-table (resolve incoming keywords)
elseif type(new) == "table" then
local merged = {}
for k, v in pairs(new) do
merged[k] = _resolve(nil, v, merger, topDefault, level + 1)
end
return merged
-- Override
elseif new ~= nil then
return deepcopy(new, merger)
-- Keep default
else
return deepcopy(default, merger)
end
end
local function resolve(default, new, options)
local merger = options and options.merger or nil
verifySubtablesAreUnique("default", default, merger, 2)
verifySubtablesAreUnique("incoming", new, merger, 2)
return _resolve(default, new, merger, default, 2)
end
local function injectOne(name, value, env)
if env[name] ~= nil and env[name] ~= value then
error("name conflict when injecting into environment: " .. name, 3)
else
env[name] = value
end
end
local function inject(env)
injectOne("NIL", NIL, env)
injectOne("SET", SET, env)
injectOne("UPDATE", UPDATE, env)
injectOne("DEFAULT", DEFAULT, env)
return env
end
return {
resolve = resolve,
inject = inject,
NIL = NIL,
SET = SET,
UPDATE = UPDATE,
DEFAULT = DEFAULT,
}