module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+9 -8
View File
@@ -1,8 +1,9 @@
module 'aux.util.persistence'
include 'T'
include 'aux'
local T = require 'T'
function M.read(schema, str)
if schema == 'string' then
return str
@@ -11,9 +12,9 @@ function M.read(schema, str)
elseif schema == 'number' then
return tonumber(str)
elseif type(schema) == 'table' and schema[1] == 'list' then
return temp-read_list(schema, str)
return T.temp-read_list(schema, str)
elseif type(schema) == 'table' and schema[1] == 'tuple' then
return temp-read_tuple(schema, str)
return T.temp-read_tuple(schema, str)
else
error('Invalid schema.', 2)
end
@@ -36,7 +37,7 @@ function M.write(schema, obj)
end
function read_list(schema, str)
if str == '' then return T end
if str == '' then return T.acquire() end
local separator = schema[2]
local element_type = schema[3]
return map(split(str, separator), function(part)
@@ -47,7 +48,7 @@ end
function write_list(schema, list)
local separator = schema[2]
local element_type = schema[3]
local parts = map(temp-copy(list), function(element)
local parts = map(T.temp-copy(list), function(element)
return write(element_type, element)
end)
return join(parts, separator)
@@ -55,8 +56,8 @@ end
function read_tuple(schema, str)
local separator = schema[2]
local tuple = T
local parts = temp-split(str, separator)
local tuple = T.acquire()
local parts = T.temp-split(str, separator)
for i = 3, getn(schema) do
local key, type = next(schema[i])
tuple[key] = read(type, parts[i - 2])
@@ -66,7 +67,7 @@ end
function write_tuple(schema, tuple)
local separator = schema[2]
local parts = temp-T
local parts = T.temp-T.acquire()
for i = 3, getn(schema) do
local key, type = next(schema[i])
tinsert(parts, write(type, tuple[key]))