Software /
code /
prosody
Annotate
util/datamapper.lua @ 11456:4e376a43fe40
util.datamapper: Factor out common schema unpacking
This code extracts the bits from the schema that determines how the data
is to be mapped to/from XML.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 14 Mar 2021 16:50:49 +0100 |
parent | 11455:a5050e21ab08 |
child | 11457:6a51749af7f4 |
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 |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
8 elseif s then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
9 return true |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
10 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
11 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
12 |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
13 local function totype(t, s) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
14 if t == "string" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
15 return s |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
16 elseif t == "boolean" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
17 return toboolean(s) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
18 elseif t == "number" or t == "integer" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
19 return tonumber(s) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
23 local value_goes = {} |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
24 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
25 local function unpack_propschema(propschema, propname, current_ns) |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
26 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
27 local proptype = "string" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
28 local value_where = "in_text_tag" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
29 local name = propname |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
30 local namespace = current_ns |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
31 local prefix |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
32 local single_attribute |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
33 local enums |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
34 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
35 if type(propschema) == "table" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
36 proptype = propschema.type |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
37 elseif type(propschema) == "string" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
38 proptype = propschema |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
39 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
40 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
41 if type(propschema) == "table" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
42 local xml = propschema.xml |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
43 if xml then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
44 if xml.name then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
45 name = xml.name |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
46 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
47 if xml.namespace then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
48 namespace = xml.namespace |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
49 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
50 if xml.prefix then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
51 prefix = xml.prefix |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
52 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
53 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
54 if xml.attribute then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
55 value_where = "in_attribute" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
56 elseif xml.text then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
57 value_where = "in_text" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
58 elseif xml.x_name_is_value then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
59 value_where = "in_tag_name" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
60 elseif xml.x_single_attribute then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
61 single_attribute = xml.x_single_attribute |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
62 value_where = "in_single_attribute" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
63 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
64 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
65 if propschema["const"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
66 enums = {propschema["const"]} |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
67 elseif propschema["enum"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
68 enums = propschema["enum"] |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
69 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
70 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
71 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
72 if proptype == "object" or proptype == "array" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
73 value_where = "in_children" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
74 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
75 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
76 return proptype, value_where, name, namespace, prefix, single_attribute, enums |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
77 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
78 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 local function parse_object(schema, s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 local out = {} |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 if schema.properties then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 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
|
83 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
84 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
86 local value |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
87 if value_where == "in_tag_name" then |
11453
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
88 local c |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
89 if proptype == "boolean" then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
90 c = s:get_child(name, namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
91 elseif enums and proptype == "string" then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
92 |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
93 for i = 1, #enums do |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
94 c = s:get_child(enums[i], namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
95 if c then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
96 break |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
97 end |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
98 end |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
99 else |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
100 c = s:get_child(nil, namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
101 end |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
102 value = c.name; |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
103 elseif value_where == "in_attribute" then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 local attr = name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 if prefix then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 attr = prefix .. ":" .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 elseif namespace ~= s.attr.xmlns then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 attr = namespace .. "\1" .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 end |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
110 value = s.attr[attr] |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
112 elseif value_where == "in_text" then |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
113 value = s:get_text() |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
115 elseif value_where == "in_single_attribute" then |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
116 local c = s:get_child(name, namespace) |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
117 value = c and c.attr[single_attribute] |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
118 elseif value_where == "in_text_tag" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
119 value = s:get_child_text(name, namespace) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
120 elseif value_where == "in_children" and type(propschema) == "table" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
121 if proptype == "object" then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 local c = s:get_child(name, namespace) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 if c then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 out[prop] = parse_object(propschema, c); |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 end |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
129 if value_where ~= "in_children" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
130 out[prop] = totype(proptype, value) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
131 end |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 return out |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 local function parse(schema, s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 if schema.type == "object" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 return parse_object(schema, s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
144 local function unparse(schema, t, current_name, current_ns) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
145 if schema.type == "object" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
146 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
147 if schema.xml then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
148 if schema.xml.name then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
149 current_name = schema.xml.name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
150 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
151 if schema.xml.namespace then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
152 current_ns = schema.xml.namespace |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
153 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
154 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
155 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
156 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
157 local out = st.stanza(current_name, {xmlns = current_ns}) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
158 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
159 for prop, propschema in pairs(schema.properties) do |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
160 local v = t[prop] |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
161 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
162 if v ~= nil then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
163 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
164 local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(propschema, prop, current_ns) |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
165 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
166 if value_where == "in_attribute" then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
167 local attr = name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
168 if prefix then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
169 attr = prefix .. ":" .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
170 elseif namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
171 attr = namespace .. "\1" .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
172 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
173 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
174 if proptype == "string" and type(v) == "string" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
175 out.attr[attr] = v |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
176 elseif proptype == "number" and type(v) == "number" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
177 out.attr[attr] = string.format("%g", v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
178 elseif proptype == "integer" and type(v) == "number" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
179 out.attr[attr] = string.format("%d", v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
180 elseif proptype == "boolean" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
181 out.attr[attr] = v and "1" or "0" |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
182 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
183 elseif value_where == "in_text" then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
184 if type(v) == "string" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
185 out:text(v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
186 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
187 elseif value_where == "in_single_attribute" then |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
188 local propattr = {} |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
189 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
190 if namespace ~= current_ns then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
191 propattr.xmlns = namespace |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
192 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
193 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
194 if proptype == "string" and type(v) == "string" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
195 propattr[single_attribute] = v |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
196 elseif proptype == "number" and type(v) == "number" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
197 propattr[single_attribute] = string.format("%g", v) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
198 elseif proptype == "integer" and type(v) == "number" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
199 propattr[single_attribute] = string.format("%d", v) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
200 elseif proptype == "boolean" and type(v) == "boolean" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
201 propattr[single_attribute] = v and "1" or "0" |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
202 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
203 out:tag(name, propattr):up(); |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
204 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
205 else |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
206 local propattr |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
207 if namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
208 propattr = {xmlns = namespace} |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
209 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
210 if value_where == "in_tag_name" then |
11438
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
211 if proptype == "string" and type(v) == "string" then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
212 out:tag(v, propattr):up(); |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
213 elseif proptype == "boolean" and v == true then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
214 out:tag(name, propattr):up(); |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
215 end |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
216 elseif proptype == "string" and type(v) == "string" then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
217 out:text_tag(name, v, propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
218 elseif proptype == "number" and type(v) == "number" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
219 out:text_tag(name, string.format("%g", v), propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
220 elseif proptype == "integer" and type(v) == "number" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
221 out:text_tag(name, string.format("%d", v), propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
222 elseif proptype == "boolean" and type(v) == "boolean" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
223 out:text_tag(name, v and "1" or "0", propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
224 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
225 local c = unparse(propschema, v, name, namespace); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
226 if c then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
227 out:add_direct_child(c); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
228 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
229 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
230 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
231 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
232 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
233 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
234 return out |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
235 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
236 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
237 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
238 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
239 return {parse = parse; unparse = unparse} |