Browse Source

fix to given data conversion deepmap

master
Jaromil 2 years ago
parent
commit
60ac2e4109
  1. 47
      src/lua/zencode_data.lua
  2. 4
      src/lua/zencode_debug.lua
  3. 16
      src/lua/zenroom_common.lua

47
src/lua/zencode_data.lua

@ -160,9 +160,9 @@ end
---
-- Pick a generic data structure from the <b>IN</b> memory
-- space. Looks for named data on the first and second level and makes
-- it ready for @{validate} or @{ack}.
-- it ready in TMP for @{validate} or @{ack}.
--
-- @function ZEN:pick(name, data)
-- @function ZEN:pick(name, data, encoding)
-- @param name string descriptor of the data object
-- @param data[opt] optional data object (default search inside IN.*)
-- @param encoding[opt] optional encoding spec (default CONF.input.encoding)
@ -179,7 +179,7 @@ function ZEN:pick(what, obj, conv)
got = inside_pick(IN.KEYS, what) or inside_pick(IN,what)
ZEN.assert(got, "Cannot find '"..what.."' anywhere")
TMP = { root = nil,
data = ZEN.decode(got, conv),
data = ZEN.decode(got, conv), -- conv may be nil
valid = false,
schema = what }
assert(ZEN.OK)
@ -318,30 +318,6 @@ function ZEN:ackmy(name, object)
assert(ZEN.OK)
end
--- When block (ACK read-write memory)
-- @section When
---
-- Draft a new text made of a simple string: convert it to @{OCTET}
-- and append it to ACK.draft.
--
-- @function ZEN:draft(string)
-- @param string any string to be appended as draft
function ZEN:draft(s)
if s then
ZEN.assert(type(s) == "string", "Provided draft is not a string")
if not ACK.draft then
ACK.draft = str(s)
else
ACK.draft = ACK.draft .. str(s)
end
else -- no arg: sanity checks
ZEN.assert(ACK.draft, "No draft found in ACK.draft")
end
assert(ZEN.OK)
end
---
-- Compare equality of two data objects (TODO: octet, ECP, etc.)
-- @function ZEN:eq(first, second)
@ -413,22 +389,29 @@ local function buf(o) return string.sub(o,5) end
-- @param decoder encoding to be used for conversion
-- @return octet object decoded
function ZEN.decode(anystr, decoder)
ZEN.assert(anystr, "ZEN:import object is nil")
ZEN.assert(anystr, "ZEN.decode object is nil")
local t = type(anystr)
if iszen(t) then
warn("ZEN.decode input already decoded to "..t)
return t
end
ZEN.assert(t == 'string', "ZEN.decode input not a string: "..t)
xxx("ZEN.decode string: "..limit(anystr))
ZEN.assert(t == 'string' or t == 'table', "ZEN.decode input not a string or table: "..t)
if decoder then
ZEN.assert(decoder.fun, "ZEN.decode invalid decoder (no fun)")
xxx("ZEN.decode selected decoder: "..decoder.name)
return(decoder.fun(anystr))
if t == 'table' then
return( deepmap(decoder.fun, anystr) )
else
return( decoder.fun(anystr) )
end
end
-- fallback to configured default
xxx("ZEN.decode default decoder: "..CONF.input.encoding.name)
return(CONF.input.encoding.fun(anystr))
if t == 'table' then
return( deepmap(CONF.input.encoding.fun, anystr))
else
return( CONF.input.encoding.fun(anystr) )
end
end
---

4
src/lua/zencode_debug.lua

@ -36,11 +36,11 @@ Given("backtrace", function() debug_traceback() end)
When("backtrace", function() debug_traceback() end)
Then("backtrace", function() debug_traceback() end)
local function debug_heap_dump()
function debug_heap_dump()
I.warn({HEAP = ZEN.heap()})
end
local function debug_heap_schema()
function debug_heap_schema()
I.schema({SCHEMA = ZEN.heap()})
-- print only keys without values
end

16
src/lua/zenroom_common.lua

@ -191,6 +191,22 @@ function map(data, fun)
return(out)
end
-- deep recursive map on a tree structure
-- for usage see test/deepmap.lua
function deepmap(fun,t,...)
assert(luatype(fun) == 'function', "Internal error: deepmap 2nd argument is not a function")
assert(luatype(t) == 'table', "Internal error: deepmap 1st argument is not a table")
local res = {}
for k,v in pairs(t) do
if luatype(v) == 'table' then
res[k] = deepmap(fun, v) -- recursion
else
res[k] = fun(v,...)
end
end
return setmetatable(res, getmetatable(t))
end
function isarray(obj)
if not obj then error("isarray() called on a nil object",2) end
if luatype(obj) ~= 'table' then error("isarray() argument is not a table",2) end

Loading…
Cancel
Save