# HG changeset patch # User Kim Alvefur # Date 1581111038 -3600 # Node ID 04ea96a0488d656ace18f751a56375d2a156a8f8 # Parent 3d0e8e32453c74c76e8088cc11b8aa57c1158182 mod_rest: Allow passing form data in a more compact format diff -r 3d0e8e32453c -r 04ea96a0488d mod_rest/jsonmap.lib.lua --- a/mod_rest/jsonmap.lib.lua Fri Feb 07 20:57:47 2020 +0100 +++ b/mod_rest/jsonmap.lib.lua Fri Feb 07 22:30:38 2020 +0100 @@ -6,6 +6,7 @@ -- Reused in many XEPs so declared up here local dataform = { + -- Generic and complete dataforms mapping "func", "jabber:x:data", "x", function (s) local fields = array(); @@ -89,6 +90,21 @@ end; }; +local function formdata(s,t) + local form = st.stanza("x", { xmlns = "jabber:x:data", type = t }); + for k,v in pairs(s) do + form:tag("field", { var = k }); + if type(v) == "string" then + form:text_tag("value", v); + elseif type(v) == "table" then + for _, v_ in ipairs(v) do + form:text_tag("value", v_); + end + end + end + return form; +end + local simple_types = { -- top level stanza attributes -- needed here to mark them as known fields @@ -277,6 +293,8 @@ end if s.form then cmd:add_child(dataform[5](s.form)); + elseif s.data then + cmd:add_child(formdata(s.data)); end return cmd; elseif type(s) == "string" then -- assume node