aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-03-06 23:14:23 +0100
committerKim Alvefur <zash@zash.se>2021-03-06 23:14:23 +0100
commitd20ea9b87e6e4071aca1a6895041d9e82ebb8691 (patch)
tree18d117f1e6a0ec3f63ed407ec22b6d4aabb16198 /util
parentd8e9e044f2be19a01af4c3c9a0ac99b8d00fdcba (diff)
downloadprosody-d20ea9b87e6e4071aca1a6895041d9e82ebb8691.tar.gz
prosody-d20ea9b87e6e4071aca1a6895041d9e82ebb8691.zip
util.datamapper: Invent extension for using tag name as value
Useful for certain enum-like uses where the element name is the relevant information, e.g. chat states.
Diffstat (limited to 'util')
-rw-r--r--util/datamapper.lua17
1 files changed, 15 insertions, 2 deletions
diff --git a/util/datamapper.lua b/util/datamapper.lua
index e0a8493a..82532f0c 100644
--- a/util/datamapper.lua
+++ b/util/datamapper.lua
@@ -18,6 +18,7 @@ local function parse_object(schema, s)
local prefix = nil
local is_attribute = false
local is_text = false
+ local name_is_value = false;
local proptype
if type(propschema) == "table" then
@@ -40,10 +41,17 @@ local function parse_object(schema, s)
is_attribute = true
elseif propschema.xml.text then
is_text = true
+ elseif propschema.xml.x_name_is_value then
+ name_is_value = true
end
end
- if is_attribute then
+ if name_is_value then
+ local c = s:get_child(nil, namespace);
+ if c then
+ out[prop] = c.name;
+ end
+ elseif is_attribute then
local attr = name
if prefix then
attr = prefix .. ":" .. name
@@ -124,6 +132,7 @@ local function unparse(schema, t, current_name, current_ns)
local prefix = nil
local is_attribute = false
local is_text = false
+ local name_is_value = false;
if type(propschema) == "table" and propschema.xml then
@@ -142,6 +151,8 @@ local function unparse(schema, t, current_name, current_ns)
is_attribute = true
elseif propschema.xml.text then
is_text = true
+ elseif propschema.xml.x_name_is_value then
+ name_is_value = true
end
end
@@ -171,7 +182,9 @@ local function unparse(schema, t, current_name, current_ns)
if namespace ~= current_ns then
propattr = {xmlns = namespace}
end
- if proptype == "string" and type(v) == "string" then
+ if name_is_value and type(v) == "string" then
+ out:tag(v, propattr):up();
+ elseif proptype == "string" and type(v) == "string" then
out:text_tag(name, v, propattr)
elseif proptype == "number" and type(v) == "number" then
out:text_tag(name, string.format("%g", v), propattr)