Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Data: Difference between revisions

From Attu Project
Content deleted Content added
Make this work when called with wikitext's fake tables
 
allow module to work with JSON data pages
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
function mt.__index(t, k)
function mt.__index(t, k)
return function(frame)
return function(frame)
local data = mw.loadData(k)
local success, data = pcall(mw.loadData, k)
local i = 1
if not success then
success, data = pcall(mw.loadJsonData, k)
for _,v in ipairs(frame.args) do
if not success then
error("'" .. k .. "' is not a valid data page")
end
end
for i, v in ipairs(frame.args) do
local ty = type(data)
local ty = type(data)
if ty ~= 'table' then
if ty ~= 'table' then
Line 18: Line 23:
end
end
end
end
data = data[v]
local nextdata = data[v]
if nextdata == nil and tonumber(v) then
i = i + 1
data = data[tonumber(v)]
else
data = nextdata
end
end
end
return data
return data

Latest revision as of 12:41, 18 February 2026

Documentation for this module may be created at Module:Data/doc

local mt = {}

function mt.__index(t, k)
	return function(frame)
		local success, data = pcall(mw.loadData, k)
		if not success then
			success, data = pcall(mw.loadJsonData, k)
			if not success then
				error("'" .. k .. "' is not a valid data page")
			end
		end
		for i, v in ipairs(frame.args) do
			local ty = type(data)
			if ty ~= 'table' then
				local args = {}
				for j = 1, i - 1 do
					args[j] = frame.args[j]
				end
				if frame.args.softfail then
					return '<span class="error">[[Category:Pages with failed Module:Data lookups]]Error: Tried to read index "' .. mw.text.nowiki(v) .. '" of mw.loadData("' .. mw.text.nowiki(k) .. '").' .. mw.text.nowiki(table.concat(args, '.')) .. ', which is a ' .. ty .. '</span>'
				else
					error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(args, '.') .. ', which is a ' .. ty)
				end
			end
			local nextdata = data[v]
			if nextdata == nil and tonumber(v) then
				data = data[tonumber(v)]
			else
				data = nextdata
			end
		end
		return data
	end
end

return setmetatable({}, mt)