changeset 3888:04ea96a0488d

mod_rest: Allow passing form data in a more compact format
author Kim Alvefur <zash@zash.se>
date Fri, 07 Feb 2020 22:30:38 +0100
parents 3d0e8e32453c
children 59765d1bb6dc
files mod_rest/jsonmap.lib.lua
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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