Mercurial > prosody-hg
changeset 14004:9991b6bd98ef
util.stanza: New :get_meta()/:set_meta() API to attach custom properties to stanza objects
These properties do not carry through clone(), and this is an intentional
decision (for now, at least). My feeling is that it is the cloner that knows
what metadata should be carried across to the clone, and the answer is usually
not "all of it".
Metadata is not persisted or serialized. It is designed to last only as long
as the in-memory stanza object, to communicate properties to code that is
processing that stanza object.
Although the API does not currently distinguish between stanzas and child
elements, it is recommended to refrain from setting metadata properties
on child elements as this ability may be removed in the future.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 06 Dec 2025 17:27:59 +0000 |
| parents | 2c95239d0b92 |
| children | b6d857435de1 |
| files | util/stanza.lua |
| diffstat | 1 files changed, 21 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/util/stanza.lua Sat Dec 06 16:00:22 2025 +0000 +++ b/util/stanza.lua Sat Dec 06 17:27:59 2025 +0000 @@ -300,6 +300,27 @@ until not self end +function stanza_mt:set_meta(k, v) + local meta = self.meta; + if not meta then + if v == nil then + return; -- no need to unset something that isn't set + end + meta = {}; + self.meta = meta; + end + meta[k] = v; + return self; +end + +function stanza_mt:get_meta(k) + local meta = self.meta; + if not meta then + return nil; + end + return meta[k]; +end + local function _clone(stanza, only_top) local attr = {}; for k,v in pairs(stanza.attr) do attr[k] = v; end
