webmcp
diff framework/env/convert/to_human.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/convert/to_human.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,21 @@ 1.4 +function convert.to_human(value, typ) 1.5 + if value == nil then return "" end -- TODO: is this correct? 1.6 + if typ and not atom.has_type(value, typ) then 1.7 + error("The value passed to convert.to_human(...) has not the specified type.") 1.8 + end 1.9 + local type_symbol 1.10 + local value_type = type(value) 1.11 + if value_type ~= "table" and value_type ~= "userdata" then 1.12 + type_symbol = value_type 1.13 + else 1.14 + type_symbol = convert._type_symbol_mappings[getmetatable(value)] 1.15 + end 1.16 + if not type_symbol then 1.17 + error("Unrecognized type reference occurred in convert.to_human(...).") 1.18 + end 1.19 + local converter = convert["_from_" .. type_symbol .. "_to_human"] 1.20 + if not converter then 1.21 + error("Type reference in convert.from_human(...) could be recognized, but the converter function is not existent.") 1.22 + end 1.23 + return converter(value) 1.24 +end