-
-
Save demonnic/c4be297596ab94e28363443c374c2544 to your computer and use it in GitHub Desktop.
The finished code from https://demonnic.com/targeted-tables/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- this function allows us to take a name of a variable as a string for checking later. | |
-- For instance, if you use getValue("demonnic.target") it will return the value at demonnic.target, or nil and an error. | |
local function getValue(location) | |
local ok, err = pcall(loadstring("return " .. tostring(location))) | |
if ok then return err end | |
return nil, err | |
end | |
local meta = {} | |
-- return an entry from the table with the target swapped out for %s token | |
-- @param which the table being looked in | |
-- @param what the key being looked for | |
function meta.__index(which, what) | |
local entry = which.entries[what] | |
if not entry then | |
return nil | |
end | |
local target = getValue(which.target) | |
return string.format(entry, target or "DEFAULT") | |
end | |
--- create an entry in the targeted table | |
-- @param which the table to add the entry to | |
-- @param what the key to use for the entry | |
-- @param becomes the value to set the entry to | |
function meta.__newindex(which, what, becomes) | |
local becomesType = type(becomes) | |
if becomesType == "nil" then | |
which.entries[what] = becomes | |
return | |
end | |
if becomesType ~= "string" then | |
local msg = "You must provide a string and a string only for values to a targeted table" | |
printDebug(msg, true) | |
return nil, msg | |
end | |
-- uncomment the following if you want to also ensure they include a '%s' to substitute the target in for. | |
-- if '%s' is not included with the string the target won't get pushed in, but it doesn't error | |
-- so that's why I left it commented out | |
--[[ | |
if not becomes:find("%%s") then | |
local msg = "You must provide a %s in the string to replace with the target" | |
printDebug(msg, true) | |
return nil, msg | |
end | |
--]] | |
which.entries[what] = becomes | |
end | |
function createTargetedTable(target, entries) | |
-- use an empty table if no entries provided | |
entries = entries or {} | |
-- do some type checking | |
entriesType = type(entries) | |
targetType = type(target) | |
if entriesType ~= "table" then | |
return nil, "createTargetedTable(target, entries): optional parameter entries as nil or table expected, got " .. entriesType | |
end | |
if targetType ~= "string" then | |
return nil, "createTargetedTable(target, entries): target variable name as string expected, got " .. targetType | |
end | |
local new = { | |
entries = entries, | |
target = target | |
} | |
setmetatable(new, meta) | |
return new | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment