Software /
code /
prosody
Annotate
util/datamapper.lua @ 11464:6e25409fecbd
util.datamapper: Only get element name if there's an element
Fixes error due to attempting to index a nil value
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 19 Mar 2021 19:07:25 +0100 |
parent | 11462:d1982b7eb00d |
child | 11465:19a88b61ab4e |
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 |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
3 local schema_t = {} |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
4 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local function toboolean(s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 if s == "true" or s == "1" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 return true |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 elseif s == "false" or s == "0" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 return false |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
10 elseif s then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
11 return true |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
12 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
13 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
14 |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
15 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
|
16 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
|
17 return 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 == "boolean" then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
19 return toboolean(s) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
20 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
|
21 return tonumber(s) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
25 local value_goes = {} |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
26 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
27 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
|
28 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
29 local proptype = "string" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
30 local value_where = "in_text_tag" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
31 local name = propname |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
32 local namespace = current_ns |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
33 local prefix |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
34 local single_attribute |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
35 local enums |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
36 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
37 if type(propschema) == "table" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
38 proptype = propschema.type |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
39 elseif type(propschema) == "string" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
40 proptype = propschema |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
41 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
42 |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
43 if proptype == "object" or proptype == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
44 value_where = "in_children" |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
45 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
46 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
47 if type(propschema) == "table" then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
48 local xml = propschema.xml |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
49 if xml then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
50 if xml.name then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
51 name = xml.name |
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 if xml.namespace then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
54 namespace = xml.namespace |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
55 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
56 if xml.prefix then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
57 prefix = xml.prefix |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
58 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
59 if proptype == "array" and xml.wrapped then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
60 value_where = "in_wrapper" |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
61 elseif xml.attribute then |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
62 value_where = "in_attribute" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
63 elseif xml.text then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
64 value_where = "in_text" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
65 elseif xml.x_name_is_value then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
66 value_where = "in_tag_name" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
67 elseif xml.x_single_attribute then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
68 single_attribute = xml.x_single_attribute |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
69 value_where = "in_single_attribute" |
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 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
72 if propschema["const"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
73 enums = {propschema["const"]} |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
74 elseif propschema["enum"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
75 enums = propschema["enum"] |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
76 end |
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 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
79 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
|
80 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
81 |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
82 local parse_object |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
83 local parse_array |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
84 |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
85 function parse_object(schema, s) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 local out = {} |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11458
diff
changeset
|
87 if type(schema) == "table" and schema.properties then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 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
|
89 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
90 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
|
91 |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
92 local value |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
93 if value_where == "in_tag_name" then |
11453
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
94 local c |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
95 if proptype == "boolean" then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
96 c = s:get_child(name, namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
97 elseif enums and proptype == "string" then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
98 |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
99 for i = 1, #enums do |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
100 c = s:get_child(enums[i], namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
101 if c then |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
102 break |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
103 end |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
104 end |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
105 else |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
106 c = s:get_child(nil, namespace); |
f0037234b2e9
util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents:
11451
diff
changeset
|
107 end |
11464
6e25409fecbd
util.datamapper: Only get element name if there's an element
Kim Alvefur <zash@zash.se>
parents:
11462
diff
changeset
|
108 if type(c) == "table" then |
6e25409fecbd
util.datamapper: Only get element name if there's an element
Kim Alvefur <zash@zash.se>
parents:
11462
diff
changeset
|
109 value = c.name; |
6e25409fecbd
util.datamapper: Only get element name if there's an element
Kim Alvefur <zash@zash.se>
parents:
11462
diff
changeset
|
110 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
111 elseif value_where == "in_attribute" then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 local attr = name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 if prefix then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 attr = prefix .. ":" .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 elseif namespace ~= s.attr.xmlns then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 attr = namespace .. "\1" .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 end |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
118 value = s.attr[attr] |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
120 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
|
121 value = s:get_text() |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 if proptype == "object" then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 local c = s:get_child(name, namespace) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 if c then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 out[prop] = parse_object(propschema, c); |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
134 elseif proptype == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
135 out[prop] = parse_array(propschema, s); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
136 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
137 error("unreachable") |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
139 elseif value_where == "in_wrapper" and type(propschema) == "table" and proptype == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
140 local wrapper = s:get_child(name, namespace); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
141 if wrapper then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
142 out[prop] = parse_array(propschema, wrapper); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
143 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
144 error("unreachable") |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
145 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
146 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
147 error("unreachable") |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
149 if value_where ~= "in_children" and value_where ~= "in_wrapper" then |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
150 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
|
151 end |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 return out |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
158 function parse_array(schema, s) |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
159 local proptype, value_where, child_name, namespace = unpack_propschema(schema.items, nil, s.attr.xmlns) |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
160 local out = {} |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
161 for c in s:childtags(child_name, namespace) do |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
162 local value; |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
163 if value_where == "in_text_tag" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
164 value = c:get_text(); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
165 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
166 error("NYI") |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
167 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
168 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
169 value = totype(proptype, value) |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
170 |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
171 if value ~= nil then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
172 table.insert(out, value); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
173 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
174 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
175 return out |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
176 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
177 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 local function parse(schema, s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 if schema.type == "object" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 return parse_object(schema, s) |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
181 elseif schema.type == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
182 return parse_array(schema, s) |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
183 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
184 error("top-level scalars unsupported") |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
188 local function unparse(schema, t, current_name, current_ns, ctx) |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
189 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
190 if schema.xml then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
191 if schema.xml.name then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
192 current_name = schema.xml.name |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
193 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
194 if schema.xml.namespace then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
195 current_ns = schema.xml.namespace |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
196 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
197 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
198 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
199 |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
200 local out = ctx or st.stanza(current_name, {xmlns = current_ns}) |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
201 |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
202 if schema.type == "object" then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
203 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
204 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
|
205 local v = t[prop] |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
206 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
207 if v ~= nil then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
208 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
209 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
|
210 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
211 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
|
212 local attr = name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
213 if prefix then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
214 attr = prefix .. ":" .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
215 elseif namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
216 attr = namespace .. "\1" .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
217 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
218 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
219 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
|
220 out.attr[attr] = v |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 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
|
225 elseif proptype == "boolean" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
226 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
|
227 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
228 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
|
229 if type(v) == "string" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
230 out:text(v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
231 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
232 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
|
233 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
|
234 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
235 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
|
236 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
|
237 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
238 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
248 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
|
249 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
250 else |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
251 local propattr |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
252 if namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
253 propattr = {xmlns = namespace} |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
254 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 end |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 if c then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
272 out:add_direct_child(c); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
273 end |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
274 elseif proptype == "array" and type(propschema) == "table" and type(v) == "table" then |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
275 if value_where == "in_wrapper" then |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
276 local c = unparse(propschema, v, name, namespace); |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
277 if c then |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
278 out:add_direct_child(c); |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
279 end |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
280 else |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
281 unparse(propschema, v, name, namespace, out); |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
282 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
283 else |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
284 error("NYI") |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
285 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
286 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
287 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
288 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
289 return out |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
290 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
291 elseif schema.type == "array" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
292 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns) |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
293 |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
294 if proptype == "string" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
295 for _, item in ipairs(t) do |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
296 if value_where == "in_text_tag" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
297 out:text_tag(name, item, {xmlns = namespace}); |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
298 else |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
299 error("NYI") |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
300 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
301 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
302 else |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
303 error("NYI") |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
304 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
305 return out |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
306 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
307 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
308 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
309 return {parse = parse; unparse = unparse} |