Software /
code /
prosody
Changeset
11435:a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Based on the XML support in the OpenAPI specification.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 07 Mar 2021 00:57:36 +0100 |
parents | 11434:66d4067bdfb2 |
children | 11436:5df9ffc25bb4 |
files | .luacheckrc spec/util_datamapper_spec.lua teal-src/util/datamapper.tl util/datamapper.lua |
diffstat | 4 files changed, 255 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.luacheckrc Sat Mar 06 21:07:53 2021 +0100 +++ b/.luacheckrc Sun Mar 07 00:57:36 2021 +0100 @@ -29,6 +29,9 @@ files["util/jsonschema.lua"] = { ignore = { "211" }; } +files["util/datamapper.lua"] = { + ignore = { "211" }; +} files["plugins/"] = { module = true; allow_defined_top = true;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spec/util_datamapper_spec.lua Sun Mar 07 00:57:36 2021 +0100 @@ -0,0 +1,56 @@ +local xml +local map + +setup(function() + xml = require "util.xml"; + map = require "util.datamapper"; +end); + +describe("util.datampper", function() + + local s, x, d + setup(function() + + local function attr() return {type = "string"; xml = {attribute = true}} end + s = { + type = "object"; + xml = {name = "message"; namespace = "jabber:client"}; + properties = { + to = attr(); + from = attr(); + type = attr(); + id = attr(); + body = "string"; + lang = {type = "string"; xml = {attribute = true; prefix = "xml"}}; + delay = { + type = "object"; + xml = {namespace = "urn:xmpp:delay"; name = "delay"}; + properties = {stamp = attr(); from = attr(); reason = {type = "string"; xml = {text = true}}}; + }; + }; + }; + + 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> + ]]; + + d = { + to = "a@test"; + from = "b@test"; + type = "chat"; + id = "1"; + lang = "en"; + body = "Hello"; + delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"}; + }; + end); + + describe("parse", function() + it("works", function() + assert.same(d, map.parse(s, x)); + end); + end); +end)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/teal-src/util/datamapper.tl Sun Mar 07 00:57:36 2021 +0100 @@ -0,0 +1,100 @@ +local st = require "util.stanza"; +local js = require "util.jsonschema" + +local function toboolean ( s : string ) : boolean + if s == "true" or s == "1" then + return true + elseif s == "false" or s == "0" then + return false + end +end + +local function parse_object (schema : js.schema_t, s : st.stanza_t) : table + local out : { string : any } = {} + if schema.properties then + for prop, propschema in pairs(schema.properties) do + -- TODO factor out, if it's generic enough + local name = prop + local namespace = s.attr.xmlns; + local prefix : string = nil + local is_attribute = false + local is_text = false + + local proptype : js.schema_t.type_e + if propschema is js.schema_t then + proptype = propschema.type + elseif propschema is js.schema_t.type_e then + proptype = propschema + end + + if propschema is js.schema_t and propschema.xml then + if propschema.xml.name then + name = propschema.xml.name + end + if propschema.xml.namespace then + namespace = propschema.xml.namespace + end + if propschema.xml.prefix then + prefix = propschema.xml.prefix + end + if propschema.xml.attribute then + is_attribute = true + elseif propschema.xml.text then + is_text = true + end + end + + if is_attribute then + local attr = name + if prefix then + attr = prefix .. ':' .. name + elseif namespace ~= s.attr.xmlns then + attr = namespace .. "\1" .. name + end + if proptype == "string" then + out[prop] = s.attr[attr] + elseif proptype == "integer" or proptype == "number" then + -- TODO floor if integer ? + out[prop] = tonumber(s.attr[attr]) + elseif proptype == "boolean" then + out[prop] = toboolean(s.attr[attr]) + -- else TODO + end + + elseif is_text then + if proptype == "string" then + out[prop] = s:get_text() + elseif proptype == "integer" or proptype == "number" then + out[prop] = tonumber(s:get_text()) + end + + else + + if proptype == "string" then + out[prop] = s:get_child_text(name, namespace) + elseif proptype == "integer" or proptype == "number" then + out[prop] = tonumber(s:get_child_text(name, namespace)) + elseif proptype == "object" and propschema is js.schema_t then + local c = s:get_child(name, namespace) + if c then + out[prop] = parse_object(propschema, c); + end + -- else TODO + end + end + end + end + + return out +end + +local function parse (schema : js.schema_t, s : st.stanza_t) : table + if schema.type == "object" then + return parse_object(schema, s) + end +end + +return { + parse = parse, + -- unparse = unparse, -- TODO +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util/datamapper.lua Sun Mar 07 00:57:36 2021 +0100 @@ -0,0 +1,96 @@ +local st = require("util.stanza"); + +local function toboolean(s) + if s == "true" or s == "1" then + return true + elseif s == "false" or s == "0" then + return false + end +end + +local function parse_object(schema, s) + local out = {} + if schema.properties then + for prop, propschema in pairs(schema.properties) do + + local name = prop + local namespace = s.attr.xmlns; + local prefix = nil + local is_attribute = false + local is_text = false + + local proptype + if type(propschema) == "table" then + proptype = propschema.type + elseif type(propschema) == "string" then + proptype = propschema + end + + if type(propschema) == "table" and propschema.xml then + if propschema.xml.name then + name = propschema.xml.name + end + if propschema.xml.namespace then + namespace = propschema.xml.namespace + end + if propschema.xml.prefix then + prefix = propschema.xml.prefix + end + if propschema.xml.attribute then + is_attribute = true + elseif propschema.xml.text then + is_text = true + end + end + + if is_attribute then + local attr = name + if prefix then + attr = prefix .. ":" .. name + elseif namespace ~= s.attr.xmlns then + attr = namespace .. "\1" .. name + end + if proptype == "string" then + out[prop] = s.attr[attr] + elseif proptype == "integer" or proptype == "number" then + + out[prop] = tonumber(s.attr[attr]) + elseif proptype == "boolean" then + out[prop] = toboolean(s.attr[attr]) + + end + + elseif is_text then + if proptype == "string" then + out[prop] = s:get_text() + elseif proptype == "integer" or proptype == "number" then + out[prop] = tonumber(s:get_text()) + end + + else + + if proptype == "string" then + out[prop] = s:get_child_text(name, namespace) + elseif proptype == "integer" or proptype == "number" then + out[prop] = tonumber(s:get_child_text(name, namespace)) + elseif proptype == "object" and type(propschema) == "table" then + local c = s:get_child(name, namespace) + if c then + out[prop] = parse_object(propschema, c); + end + + end + end + end + end + + return out +end + +local function parse(schema, s) + if schema.type == "object" then + return parse_object(schema, s) + end +end + +return {parse = parse}