aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_datamapper_spec.lua7
-rw-r--r--teal-src/util/datamapper.tl34
-rw-r--r--teal-src/util/jsonschema.tl1
-rw-r--r--util/datamapper.lua34
4 files changed, 76 insertions, 0 deletions
diff --git a/spec/util_datamapper_spec.lua b/spec/util_datamapper_spec.lua
index 7f6ef75e..1f03c850 100644
--- a/spec/util_datamapper_spec.lua
+++ b/spec/util_datamapper_spec.lua
@@ -35,6 +35,10 @@ describe("util.datampper", function()
type = "boolean";
xml = {x_name_is_value = true; name = "fallback"; namespace = "urn:xmpp:fallback:0"};
};
+ origin_id = {
+ type = "string";
+ xml = {name = "origin-id"; namespace = "urn:xmpp:sid:0"; x_single_attribute = "id"};
+ };
};
};
@@ -44,6 +48,7 @@ describe("util.datampper", function()
<delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Becasue</delay>
<active xmlns='http://jabber.org/protocol/chatstates'/>
<fallback xmlns='urn:xmpp:fallback:0'/>
+ <origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/>
</message>
]];
@@ -57,6 +62,7 @@ describe("util.datampper", function()
delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"};
state = "active";
fallback = true;
+ origin_id = "qgkmMdPB";
};
end);
@@ -75,6 +81,7 @@ describe("util.datampper", function()
assert.equal(x:get_child_text("body"), u:get_child_text("body"));
assert.equal(x:get_child_text("delay", "urn:xmpp:delay"), u:get_child_text("delay", "urn:xmpp:delay"));
assert.same(x:get_child("delay", "urn:xmpp:delay").attr, u:get_child("delay", "urn:xmpp:delay").attr);
+ assert.same(x:get_child("origin-id", "urn:xmpp:sid:0").attr, u:get_child("origin-id", "urn:xmpp:sid:0").attr);
end);
end);
end)
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
diff --git a/teal-src/util/jsonschema.tl b/teal-src/util/jsonschema.tl
index 96832a7f..13537e0a 100644
--- a/teal-src/util/jsonschema.tl
+++ b/teal-src/util/jsonschema.tl
@@ -65,6 +65,7 @@ local record schema_t
-- nonstantard, maybe in the future
text : boolean
x_name_is_value : boolean
+ x_single_attribute : string
end
xml : xml_t
diff --git a/util/datamapper.lua b/util/datamapper.lua
index a87b21ae..f4860d6d 100644
--- a/util/datamapper.lua
+++ b/util/datamapper.lua
@@ -19,6 +19,7 @@ local function parse_object(schema, s)
local is_attribute = false
local is_text = false
local name_is_value = false;
+ local single_attribute
local proptype
if type(propschema) == "table" then
@@ -43,6 +44,8 @@ local function parse_object(schema, s)
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
@@ -77,6 +80,16 @@ local function parse_object(schema, s)
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
@@ -135,6 +148,7 @@ local function unparse(schema, t, current_name, current_ns)
local is_attribute = false
local is_text = false
local name_is_value = false;
+ local single_attribute
if type(propschema) == "table" and propschema.xml then
@@ -155,6 +169,8 @@ local function unparse(schema, t, current_name, current_ns)
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
@@ -179,6 +195,24 @@ local function unparse(schema, t, current_name, current_ns)
if type(v) == "string" then
out:text(v)
end
+ elseif single_attribute then
+ local propattr = {}
+
+ if namespace ~= current_ns then
+ propattr.xmlns = namespace
+ end
+
+ if proptype == "string" and type(v) == "string" then
+ propattr[single_attribute] = v
+ elseif proptype == "number" and type(v) == "number" then
+ propattr[single_attribute] = string.format("%g", v)
+ elseif proptype == "integer" and type(v) == "number" then
+ propattr[single_attribute] = string.format("%d", v)
+ elseif proptype == "boolean" and type(v) == "boolean" then
+ propattr[single_attribute] = v and "1" or "0"
+ end
+ out:tag(name, propattr):up();
+
else
local propattr
if namespace ~= current_ns then