scrooge-generator/src/main/resources/luagen/enum.mustache (35 lines of code) (raw):

{{#docstring}} --[[ {{docstring}} --]] {{/docstring}} -- Import this file with: -- require '{{package}}.{{EnumName}}' local {{EnumName}} = { {{#values}} {{originalName}} = {{value}}, {{/values}}} local inverseMapping = {} local lookupById = function(enum_id) if not next(inverseMapping) then for label, id in pairs({{EnumName}}) do inverseMapping[tostring(id)] = label end end return inverseMapping[tostring(enum_id)] end setmetatable({{EnumName}}, { __index = function(tbl, arg) if type(arg) == "number" then return lookupById(arg) else local upper = arg:upper() if rawget(tbl, upper) then return rawget(tbl, upper) else error('Key `' .. arg .. '` is not a valid entry in the `{{EnumName}}` enum.') end end end }) return {{EnumName}}