Difference between revisions of "User:Dr. Strangelove/ReadModule"
m |
|||
Line 1: | Line 1: | ||
− | + | local timeA = os.clock() | |
− | + | local p = {} | |
− | + | ||
+ | local data = mw.loadData('Module:Test/data') | ||
+ | |||
+ | function find_key_in_table(find_key, in_table) | ||
+ | for key, val in pairs(in_table) do | ||
+ | if key == find_key then return val | ||
+ | elseif type(val) == "table" then | ||
+ | local var = find_key_in_table(find_key, val) | ||
+ | if var then return var end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | -- use "ParentName" to identify the parent | ||
+ | -- side effect (data table as parameter) | ||
+ | function find_using_inheritance(find_key, in_table) | ||
+ | local found = find_key_in_table(find_key, in_table) | ||
+ | if found then return found | ||
+ | elseif in_table.ParentName then | ||
+ | found = find_using_inheritance(find_key, find_key_in_table(in_table.ParentName, data)) | ||
+ | if found then return found end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function p.get(frame, data) | ||
+ | local parent = find_key_in_table(frame.args[1], data) | ||
+ | local found = find_using_inheritance(frame.args[2], parent) | ||
+ | return found | ||
+ | end | ||
+ | |||
+ | local timeB = os.clock() | ||
+ | |||
+ | mw.log(timeB-timeA) | ||
+ | |||
+ | return p |
Revision as of 10:18, 11 March 2021
local timeA = os.clock() local p = {}
local data = mw.loadData('Module:Test/data')
function find_key_in_table(find_key, in_table) for key, val in pairs(in_table) do if key == find_key then return val elseif type(val) == "table" then local var = find_key_in_table(find_key, val) if var then return var end end end end
-- use "ParentName" to identify the parent -- side effect (data table as parameter) function find_using_inheritance(find_key, in_table) local found = find_key_in_table(find_key, in_table) if found then return found elseif in_table.ParentName then found = find_using_inheritance(find_key, find_key_in_table(in_table.ParentName, data)) if found then return found end end end
function p.get(frame, data) local parent = find_key_in_table(frame.args[1], data) local found = find_using_inheritance(frame.args[2], parent) return found end
local timeB = os.clock()
mw.log(timeB-timeA)
return p