aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_datamapper_spec.lua10
-rw-r--r--teal-src/util/datamapper.tl21
-rw-r--r--util/datamapper.lua21
3 files changed, 49 insertions, 3 deletions
diff --git a/spec/util_datamapper_spec.lua b/spec/util_datamapper_spec.lua
index 1f03c850..7f5b71de 100644
--- a/spec/util_datamapper_spec.lua
+++ b/spec/util_datamapper_spec.lua
@@ -29,6 +29,13 @@ describe("util.datampper", function()
};
state = {
type = "string";
+ enum = {
+ "active",
+ "inactive",
+ "gone",
+ "composing",
+ "paused",
+ };
xml = {x_name_is_value = true; namespace = "http://jabber.org/protocol/chatstates"};
};
fallback = {
@@ -46,6 +53,7 @@ describe("util.datampper", function()
<message xmlns="jabber:client" xml:lang="en" to="a@test" from="b@test" type="chat" id="1">
<body>Hello</body>
<delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Becasue</delay>
+ <UNRELATED xmlns='http://jabber.org/protocol/chatstates'/>
<active xmlns='http://jabber.org/protocol/chatstates'/>
<fallback xmlns='urn:xmpp:fallback:0'/>
<origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/>
@@ -77,7 +85,7 @@ describe("util.datampper", function()
local u = map.unparse(s, d);
assert.equal("message", u.name);
assert.same(x.attr, u.attr);
- assert.equal(#x.tags, #u.tags)
+ assert.equal(#x.tags-1, #u.tags)
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);
diff --git a/teal-src/util/datamapper.tl b/teal-src/util/datamapper.tl
index 7aec331b..fac2acce 100644
--- a/teal-src/util/datamapper.tl
+++ b/teal-src/util/datamapper.tl
@@ -42,6 +42,7 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table
local is_text = false
local name_is_value = false;
local single_attribute : string
+ local enums : { any }
local proptype : js.schema_t.type_e
if propschema is js.schema_t then
@@ -72,10 +73,28 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table
-- XXX Custom extension
single_attribute = propschema.xml.x_single_attribute
end
+ if propschema["const"] then
+ enums = { propschema["const"] }
+ elseif propschema["enum"] then
+ enums = propschema["enum"]
+ end
end
if name_is_value then
- local c = s:get_child(nil, namespace);
+ local c : st.stanza_t
+ if proptype == "boolean" then
+ c = s:get_child(name, namespace);
+ elseif enums and proptype == "string" then
+ -- XXX O(n²) ?
+ -- Probably better to flip the table and loop over :childtags(nil, ns), should be 2xO(n)
+ -- BUT works first, optimize later
+ for i = 1, #enums do
+ c = s:get_child(enums[i] as string, namespace);
+ if c then break end
+ end
+ else
+ c = s:get_child(nil, namespace);
+ end
if c and proptype == "string" then
out[prop] = c.name;
elseif proptype == "boolean" and c then
diff --git a/util/datamapper.lua b/util/datamapper.lua
index 35788499..092497d7 100644
--- a/util/datamapper.lua
+++ b/util/datamapper.lua
@@ -20,6 +20,7 @@ local function parse_object(schema, s)
local is_text = false
local name_is_value = false;
local single_attribute
+ local enums
local proptype
if type(propschema) == "table" then
@@ -50,10 +51,28 @@ local function parse_object(schema, s)
single_attribute = propschema.xml.x_single_attribute
end
+ if propschema["const"] then
+ enums = {propschema["const"]}
+ elseif propschema["enum"] then
+ enums = propschema["enum"]
+ end
end
if name_is_value then
- local c = s:get_child(nil, namespace);
+ local c
+ if proptype == "boolean" then
+ c = s:get_child(name, namespace);
+ elseif enums and proptype == "string" then
+
+ for i = 1, #enums do
+ c = s:get_child(enums[i], namespace);
+ if c then
+ break
+ end
+ end
+ else
+ c = s:get_child(nil, namespace);
+ end
if c and proptype == "string" then
out[prop] = c.name;
elseif proptype == "boolean" and c then