aboutsummaryrefslogtreecommitdiffstats
path: root/teal-src/util/datamapper.tl
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-03-07 12:48:49 +0100
committerKim Alvefur <zash@zash.se>2021-03-07 12:48:49 +0100
commit07889f274dfb3dbb17988688701602cc43291941 (patch)
tree6cc1dd52bfa322dedd8ab7b713d2db9a865f7771 /teal-src/util/datamapper.tl
parent7cd2803161ee05303371cd681f8ae00a0838d07f (diff)
downloadprosody-07889f274dfb3dbb17988688701602cc43291941.tar.gz
prosody-07889f274dfb3dbb17988688701602cc43291941.zip
util.datamapper: Add support for mapping of elements where only one attribute matters
E.g. <feature var='foo'/> in XEP-0030 and some other simple specifications.
Diffstat (limited to 'teal-src/util/datamapper.tl')
-rw-r--r--teal-src/util/datamapper.tl34
1 files changed, 34 insertions, 0 deletions
diff --git a/teal-src/util/datamapper.tl b/teal-src/util/datamapper.tl
index 9546809a..fde5a4a4 100644
--- a/teal-src/util/datamapper.tl
+++ b/teal-src/util/datamapper.tl
@@ -20,6 +20,7 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table
local is_attribute = false
local is_text = false
local name_is_value = false;
+ local single_attribute : string
local proptype : js.schema_t.type_e
if propschema is js.schema_t then
@@ -44,6 +45,8 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table
is_text = true
elseif propschema.xml.x_name_is_value then
name_is_value = true
+ elseif propschema.xml.x_single_attribute then
+ single_attribute = propschema.xml.x_single_attribute
end
end
@@ -78,6 +81,16 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table
out[prop] = tonumber(s:get_text())
end
+ elseif single_attribute then
+ local c = s:get_child(name, namespace)
+ local a = c and c.attr[single_attribute]
+ if proptype == "string" then
+ out[prop] = a
+ elseif proptype == "integer" or proptype == "number" then
+ out[prop] = tonumber(a)
+ elseif proptype == "boolean" then
+ out[prop] = toboolean(a)
+ end
else
if proptype == "string" then
@@ -136,6 +149,7 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string,
local is_attribute = false
local is_text = false
local name_is_value = false;
+ local single_attribute : string
if propschema is js.schema_t and propschema.xml then
@@ -156,6 +170,8 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string,
is_text = true
elseif propschema.xml.x_name_is_value then
name_is_value = true
+ elseif propschema.xml.x_single_attribute then
+ single_attribute = propschema.xml.x_single_attribute
end
end
@@ -180,6 +196,24 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string,
if v is string then
out:text(v)
end
+ elseif single_attribute then
+ local propattr : { string : string } = {}
+
+ if namespace ~= current_ns then
+ propattr.xmlns = namespace
+ end
+
+ if proptype == "string" and v is string then
+ propattr[single_attribute] = v
+ elseif proptype == "number" and v is number then
+ propattr[single_attribute] = string.format("%g", v)
+ elseif proptype == "integer" and v is number then
+ propattr[single_attribute] = string.format("%d", v)
+ elseif proptype == "boolean" and v is boolean then
+ propattr[single_attribute] = v and "1" or "0"
+ end
+ out:tag(name, propattr):up();
+
else
local propattr : { string : string }
if namespace ~= current_ns then