Annotate

util/datamapper.lua @ 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
child 11436:5df9ffc25bb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require("util.stanza");
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local function toboolean(s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 if s == "true" or s == "1" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 return true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 elseif s == "false" or s == "0" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 return false
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local function parse_object(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local out = {}
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 if schema.properties then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 for prop, propschema in pairs(schema.properties) do
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local name = prop
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local namespace = s.attr.xmlns;
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local prefix = nil
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local is_attribute = false
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local is_text = false
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local proptype
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if type(propschema) == "table" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 proptype = propschema.type
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 elseif type(propschema) == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 proptype = propschema
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 if type(propschema) == "table" and propschema.xml then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 if propschema.xml.name then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 name = propschema.xml.name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 if propschema.xml.namespace then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 namespace = propschema.xml.namespace
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if propschema.xml.prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 prefix = propschema.xml.prefix
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if propschema.xml.attribute then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 is_attribute = true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 elseif propschema.xml.text then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 is_text = true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if is_attribute then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local attr = name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 attr = prefix .. ":" .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 elseif namespace ~= s.attr.xmlns then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 attr = namespace .. "\1" .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 out[prop] = s.attr[attr]
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 out[prop] = tonumber(s.attr[attr])
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 elseif proptype == "boolean" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 out[prop] = toboolean(s.attr[attr])
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 elseif is_text then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 out[prop] = s:get_text()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 out[prop] = tonumber(s:get_text())
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 else
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if proptype == "string" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 out[prop] = s:get_child_text(name, namespace)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 elseif proptype == "integer" or proptype == "number" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 out[prop] = tonumber(s:get_child_text(name, namespace))
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 elseif proptype == "object" and type(propschema) == "table" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 local c = s:get_child(name, namespace)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 if c then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 out[prop] = parse_object(propschema, c);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return out
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local function parse(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if schema.type == "object" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 return parse_object(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 return {parse = parse}