-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.lua
More file actions
111 lines (89 loc) · 3.04 KB
/
Task.lua
File metadata and controls
111 lines (89 loc) · 3.04 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
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
function initialize()
local Arr = {}
local storage = {}
local metadata = {}
function Arr.arrayCreate(config)
if not config or not config.name then
error("Array must have a name")
end
local name = config.name
local values = config.value or {}
local expectedType = config.type
local maxSize = config.itemsize
local deleteExceeds = config.deleteExceeds or false
local showIndex = config.showIndex or false
-- validate initial values
if expectedType then
for i, v in ipairs(values) do
if type(v) ~= expectedType then
error("Type mismatch in array '" .. name .. "' at index " .. i)
end
end
end
if maxSize and #values > maxSize then
if deleteExceeds then
while #values > maxSize do
table.remove(values)
end
else
error("Array '" .. name .. "' exceeds max size")
end
end
storage[name] = values
metadata[name] = {
type = expectedType,
itemsize = maxSize,
deleteExceeds = deleteExceeds,
showIndex = showIndex
}
-- proxy with enforcement
Arr[name] = setmetatable({}, {
__index = function(_, key)
local arr = storage[name]
-- RANDOM PICK
if key == "rand" then
if #arr == 0 then return nil end
local i = math.random(1, #arr)
local value = arr[i]
if metadata[name].showIndex then
return value .. " - index of " .. i
end
return value
end
local value = arr[key]
if metadata[name].showIndex and value ~= nil then
return value .. " - index of " .. key
end
return value
end,
__newindex = function(_, key, value)
local arr = storage[name]
local meta = metadata[name]
-- type enforcement
if meta.type and type(value) ~= meta.type then
error("Type mismatch: expected " .. meta.type .. ", got " .. type(value))
end
-- size enforcement
if meta.itemsize and key > meta.itemsize then
if meta.deleteExceeds then
return -- ignore insertion
else
error("Array '" .. name .. "' exceeded max size")
end
end
arr[key] = value
end
})
end
return Arr
end
local loader = require("Task")
Arr = initialize()
Arr.arrayCreate({
name = "school",
type = "string",
deleteExceeds = false,
showIndex = true,
value = {"students", "principal", "teacher", "school owner"}
})
print(Arr.school["rand"])