# HG changeset patch # User Kim Alvefur # Date 1615509195 -3600 # Node ID f0037234b2e9cb9dfa8d1ae243aace355a59eee6 # Parent c799bac7ca59f50535b104af10bfeaeceb6b9452 util.datamapper: Enumerated elements E.g. error conditions or chat states. diff -r c799bac7ca59 -r f0037234b2e9 spec/util_datamapper_spec.lua --- a/spec/util_datamapper_spec.lua Wed Mar 10 00:19:45 2021 +0100 +++ b/spec/util_datamapper_spec.lua Fri Mar 12 01:33:15 2021 +0100 @@ -29,6 +29,13 @@ }; 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 @@ Hello Becasue + @@ -77,7 +85,7 @@ 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 -r c799bac7ca59 -r f0037234b2e9 teal-src/util/datamapper.tl --- a/teal-src/util/datamapper.tl Wed Mar 10 00:19:45 2021 +0100 +++ b/teal-src/util/datamapper.tl Fri Mar 12 01:33:15 2021 +0100 @@ -42,6 +42,7 @@ 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 @@ -- 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 -r c799bac7ca59 -r f0037234b2e9 util/datamapper.lua --- a/util/datamapper.lua Wed Mar 10 00:19:45 2021 +0100 +++ b/util/datamapper.lua Fri Mar 12 01:33:15 2021 +0100 @@ -20,6 +20,7 @@ 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 @@ 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