diff options
author | Kim Alvefur <zash@zash.se> | 2021-03-06 23:14:23 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2021-03-06 23:14:23 +0100 |
commit | d20ea9b87e6e4071aca1a6895041d9e82ebb8691 (patch) | |
tree | 18d117f1e6a0ec3f63ed407ec22b6d4aabb16198 | |
parent | d8e9e044f2be19a01af4c3c9a0ac99b8d00fdcba (diff) | |
download | prosody-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.
-rw-r--r-- | spec/util_datamapper_spec.lua | 16 | ||||
-rw-r--r-- | teal-src/util/datamapper.tl | 17 | ||||
-rw-r--r-- | teal-src/util/jsonschema.tl | 1 | ||||
-rw-r--r-- | util/datamapper.lua | 17 |
4 files changed, 42 insertions, 9 deletions
diff --git a/spec/util_datamapper_spec.lua b/spec/util_datamapper_spec.lua index 2ef828f2..2a149b1c 100644 --- a/spec/util_datamapper_spec.lua +++ b/spec/util_datamapper_spec.lua @@ -27,15 +27,20 @@ describe("util.datampper", function() xml = {namespace = "urn:xmpp:delay"; name = "delay"}; properties = {stamp = attr(); from = attr(); reason = {type = "string"; xml = {text = true}}}; }; + state = { + type = "string"; + xml = {x_name_is_value = true; namespace = "http://jabber.org/protocol/chatstates"}; + }; }; }; x = xml.parse [[ - <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> - </message> - ]]; + <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> + <active xmlns='http://jabber.org/protocol/chatstates'/> + </message> + ]]; d = { to = "a@test"; @@ -45,6 +50,7 @@ describe("util.datampper", function() lang = "en"; body = "Hello"; delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"}; + state = "active"; }; end); diff --git a/teal-src/util/datamapper.tl b/teal-src/util/datamapper.tl index 925bf1cd..4db153da 100644 --- a/teal-src/util/datamapper.tl +++ b/teal-src/util/datamapper.tl @@ -19,6 +19,7 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table local prefix : string = nil local is_attribute = false local is_text = false + local name_is_value = false; local proptype : js.schema_t.type_e if propschema is js.schema_t then @@ -41,10 +42,17 @@ local function parse_object (schema : js.schema_t, s : st.stanza_t) : table 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 @@ -125,6 +133,7 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string, local prefix : string = nil local is_attribute = false local is_text = false + local name_is_value = false; if propschema is js.schema_t and propschema.xml then @@ -143,6 +152,8 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string, 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 @@ -172,7 +183,9 @@ local function unparse ( schema : js.schema_t, t : table, current_name : string, if namespace ~= current_ns then propattr = { xmlns = namespace } end - if proptype == "string" and v is string then + if name_is_value and v is string then + out:tag(v, propattr):up(); + elseif proptype == "string" and v is string then out:text_tag(name, v, propattr) elseif proptype == "number" and v is number then out:text_tag(name, string.format("%g", v), propattr) diff --git a/teal-src/util/jsonschema.tl b/teal-src/util/jsonschema.tl index 1c11f63b..96832a7f 100644 --- a/teal-src/util/jsonschema.tl +++ b/teal-src/util/jsonschema.tl @@ -64,6 +64,7 @@ local record schema_t -- nonstantard, maybe in the future text : boolean + x_name_is_value : boolean end xml : xml_t 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) |