- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringstore.lua
More file actions
Latest commit
64 lines (55 loc) · 1.92 KB
/
Copy pathstringstore.lua
File metadata and controls
64 lines (55 loc) · 1.92 KB
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
stringstore= { stores= {} }
functionstringstore.convert_to_string(val)
localval_type=type(val)
ifval_type=="function" then
error("Serializing functions is not supported")
end
returntostring(val)
end
functionstringstore.convert_from_string(target_type, key, storeinfo)
iftarget_type=="nil" thenreturnnilend
iftarget_type=="string" thenreturnstoreinfo.get(key) end
iftarget_type=="number" thenreturntonumber(storeinfo.get(key)) end
iftarget_type=="table" then
returnstringstore.open_store(storeinfo, key, storeinfo.get_sub_prefix(key))
end
iftarget_type=="boolean" thenreturnstoreinfo.get(key) =="true" end
error("Unsupported target type: " ..target_type)
end
functionstringstore.open_store(storeinfo, base_key, key_prefix)
ifnotstoreinfo.set_typethenerror("Missing storeinfo 'set_type' field") end
ifnotstoreinfo.setthenerror("Missing storeinfo 'set' field") end
ifnotstoreinfo.get_typethenerror("Missing storeinfo 'get_type' field") end
ifnotstoreinfo.getthenerror("Missing storeinfo 'get' field") end
ifnotbase_keythenbase_key="" end
returnsetmetatable({},
{
__newindex=function(self, key, val)
storeinfo.restrict(tostring(key))
key=storeinfo.get_typed_key(key, type(key))
ifkey_prefixthen
key=key_prefix..key
end
localval_type=type(val)
storeinfo.set_type(key, val_type)
if (val_type=="table") then
fork, vinpairs(val) do
stringstore.convert_from_string("table", key, storeinfo)[k] =v
end
else
storeinfo.set(key, tostring(val))
end
end,
__index=function(self, key)
storeinfo.restrict(tostring(key))
key=storeinfo.get_typed_key(key, type(key))
ifkey_prefixthen
key=key_prefix..key
end
localval_type=storeinfo.get_type(key)
if (val_type==nil) thenreturnnilend
returnstringstore.convert_from_string(val_type, key, storeinfo)
end
})
end
returnstringstore