-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynctable.lua
181 lines (157 loc) · 3.16 KB
/
synctable.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
local synctable = {}
---@param src table
---@param dst table
local function copy(src, dst)
for k, v in pairs(src) do
dst[k] = v
end
end
---@param t table
---@param path table
---@return table
local function deep_index(t, path)
for _, k in ipairs(path) do
t = t[k]
end
return t
end
---@param v string|number|table|boolean|nil
local function assert_value_type(v)
local t = type(v)
assert(t == "string" or t == "number" or t == "table" or t == "boolean" or t == "nil")
end
---@param t table
local function validate(t)
for k, v in pairs(t) do
local tk = type(k)
assert(tk == "string" or tk == "number")
assert_value_type(v)
if type(v) == "table" then
validate(v)
end
end
end
---@param t table
---@return table
local function get_path(t)
local path = {}
while t.__parent do
table.insert(path, 1, t.__name)
t = t.__parent
end
return path
end
local mt = {}
---@param s table
---@param k string|number
---@param v string|number|table|boolean|nil
function mt.__newindex(s, k, v)
local _t = s.__t
local path = get_path(s)
if type(v) ~= "table" then
assert_value_type(v)
_t[k] = v
s.__cb(path, k, v, false)
return
end
if getmetatable(v) == mt then
_t[k] = v.__t
s.__cb(path, k, get_path(v), true)
return
end
local _v = _t[k]
if _v ~= v then
_v = {}
_t[k] = _v
end
s.__cb(path, k, {}, false)
copy(v, s[k])
end
---@param s table
---@param k string|number
---@return string|number|table|boolean|nil
function mt.__index(s, k)
local _t = s.__t
local v = _t[k]
local _v = rawget(s, v)
if type(v) ~= "table" then
return v
elseif _v then
return _v
end
_v = setmetatable({
__t = v,
__name = k,
__parent = s,
__cb = s.__cb,
}, mt)
rawset(s, v, _v)
return _v
end
---@param t table
---@param callback function
---@return table
function synctable.new(t, callback)
validate(t)
local res = setmetatable({ -- set mt before copy
__t = t,
__cb = callback,
}, mt)
copy(t, res) -- will call callback
return res
end
---@param object table
---@param path table
---@param k any
---@param v any
---@param is_path boolean
function synctable.set(object, path, k, v, is_path)
local t = deep_index(object, path)
if is_path then
v = deep_index(object, v)
end
t[k] = v
end
---@param key any
---@return string
local function formatKey(key)
local f = type(key) == "string" and ".%s" or "[%s]"
return f:format(key)
end
---@param path table
---@return string
local function formatPath(path)
local p = {}
for _, key in ipairs(path) do
table.insert(p, formatKey(key))
end
return table.concat(p)
end
---@param prefix string
---@param value any
---@param is_path boolean
---@return string
local function formatValue(prefix, value, is_path)
if is_path then
return prefix .. formatPath(value)
end
if type(value) == "table" then
return "{}"
elseif type(value) == "string" then
return ("%q"):format(value)
end
return tostring(value)
end
---@param prefix string
---@param path table
---@param k any
---@param v any
---@param is_path boolean
---@return string
function synctable.format(prefix, path, k, v, is_path)
return ("%s = %s"):format(
prefix .. formatPath(path) .. formatKey(k),
formatValue(prefix, v, is_path)
)
end
return synctable