Module:File link: Difference between revisions

reduce redundant error code
(seperate treatment of private fields and read-only fields, and add a __pairs metamethod)
(reduce redundant error code)
Line 247:
for field in pairs(data) do
readOnlyFields[field] = true
end
local function restrictedFieldError(key, restriction)
error(string.format(
"image object field '%s' is private%s",
tostring(key),
restriction
), 23)
end
Line 252 ⟶ 260:
__index = function (t, key)
if privateFields[key] then
restrictedFieldError(key, 'private')
error(string.format(
"image object field '%s' is private",
tostring(key)
), 2)
else
return data[key]
Line 262 ⟶ 267:
__newindex = function (t, key, value)
if privateFields[key] then
restrictedFieldError(key, 'private')
error(string.format(
"image object field '%s' is private",
tostring(key)
), 2)
elseif readOnlyFields[key] then
restrictedFieldError(key, 'read-only')
error(string.format(
"image object field '%s' is read-only",
tostring(key)
), 2)
else
data[key] = value