Skip to content

Commit

Permalink
Parser: improved handling of unexpected values
Browse files Browse the repository at this point in the history
XML-RPC libs tend to do their own thing sometimes in terms of
serialization of data, therefore make the methods a little more robust
by handling some more of these cases.
  • Loading branch information
timn committed Sep 15, 2010
1 parent ecb90ab commit ea95463
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/init.lua
Expand Up @@ -75,13 +75,13 @@ end
local function x2boolean (tab)
if tab.tag == "boolean" then
local v = next_nonspace (tab, 1)
return tonumber (v) == 1 or false
return v == true or v == "true" or tonumber (v) == 1 or false
end
end

---------------------------------------------------------------------
local function x2string (tab)
return tab.tag == "string" and tab[1]
return tab.tag == "string" and (tab[1] or "")
end

---------------------------------------------------------------------
Expand Down Expand Up @@ -202,6 +202,10 @@ x2value = function (tab)
local get = xmlrpc_types[t]
if not get then error ("Invalid <"..t.."> element") end
return get (next_nonspace (tab))
elseif type(n) == "nil" then
-- the next best thing is to assume it's an empty string
return ""

end
end

Expand Down Expand Up @@ -365,7 +369,14 @@ function toxml.array (val, typ)
local et = typ.elemtype
local f = format_func (et)
for i,v in ipairs (val) do
tinsert (ret, format (formats.value, f (v, et)))
if et and et ~= "array" then
tinsert (ret, format (formats.value, f (v, et)))
else
local ct,cv = type_val(v)
local cf = format_func(ct)
tinsert (ret, format (formats.value, cf(cv, ct)))
end

end
return format (formats.array, concat (ret, '\n'))
end
Expand Down Expand Up @@ -398,7 +409,7 @@ end
---------------------------------------------------------------------
-- Get type and value of object.
---------------------------------------------------------------------
local function type_val (obj)
function type_val (obj)
local t = type (obj)
local v = obj
if t == "table" then
Expand Down

0 comments on commit ea95463

Please sign in to comment.