mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-20 17:41:37 +00:00
plugin: Add API interface for strongly typing responses
This commit is contained in:
parent
c62a5d15ad
commit
0dbbf44ab2
2 changed files with 94 additions and 10 deletions
24
plugin/src/strict.lua
Normal file
24
plugin/src/strict.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
local function strictInner(name, target)
|
||||
assert(type(name) == "string", "Argument #1 to `strict` must be a string or the table to modify")
|
||||
assert(type(target) == "table", "Argument #2 to `strict` must be nil or the table to modify")
|
||||
|
||||
setmetatable(target, {
|
||||
__index = function(_, key)
|
||||
error(("%q is not a valid member of strict table %q"):format(tostring(key), name), 2)
|
||||
end,
|
||||
|
||||
__newindex = function()
|
||||
error(("Strict table %q is read-only"):format(name), 2)
|
||||
end,
|
||||
})
|
||||
|
||||
return target
|
||||
end
|
||||
|
||||
return function(nameOrTarget, target)
|
||||
if type(nameOrTarget) == "string" then
|
||||
return strictInner(nameOrTarget, target)
|
||||
else
|
||||
return strictInner("<unnamed table>", target)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue