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: | Line 247: | ||
for field in pairs(data) do | for field in pairs(data) do | ||
readOnlyFields[field] = true | readOnlyFields[field] = true | ||
end | |||
local function restrictedFieldError(key, restriction) | |||
error(string.format( | |||
"image object field '%s' is %s", | |||
tostring(key), | |||
restriction | |||
), 3) | |||
end | end | ||
Line 252: | Line 260: | ||
__index = function (t, key) | __index = function (t, key) | ||
if privateFields[key] then | if privateFields[key] then | ||
restrictedFieldError(key, 'private') | |||
else | else | ||
return data[key] | return data[key] | ||
Line 262: | Line 267: | ||
__newindex = function (t, key, value) | __newindex = function (t, key, value) | ||
if privateFields[key] then | if privateFields[key] then | ||
restrictedFieldError(key, 'private') | |||
elseif readOnlyFields[key] then | elseif readOnlyFields[key] then | ||
restrictedFieldError(key, 'read-only') | |||
else | else | ||
data[key] = value | data[key] = value |