Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
11455:a5050e21ab08 | 11456:4e376a43fe40 |
---|---|
20 end | 20 end |
21 end | 21 end |
22 | 22 |
23 local value_goes = {} | 23 local value_goes = {} |
24 | 24 |
25 local function unpack_propschema(propschema, propname, current_ns) | |
26 | |
27 local proptype = "string" | |
28 local value_where = "in_text_tag" | |
29 local name = propname | |
30 local namespace = current_ns | |
31 local prefix | |
32 local single_attribute | |
33 local enums | |
34 | |
35 if type(propschema) == "table" then | |
36 proptype = propschema.type | |
37 elseif type(propschema) == "string" then | |
38 proptype = propschema | |
39 end | |
40 | |
41 if type(propschema) == "table" then | |
42 local xml = propschema.xml | |
43 if xml then | |
44 if xml.name then | |
45 name = xml.name | |
46 end | |
47 if xml.namespace then | |
48 namespace = xml.namespace | |
49 end | |
50 if xml.prefix then | |
51 prefix = xml.prefix | |
52 end | |
53 | |
54 if xml.attribute then | |
55 value_where = "in_attribute" | |
56 elseif xml.text then | |
57 value_where = "in_text" | |
58 elseif xml.x_name_is_value then | |
59 value_where = "in_tag_name" | |
60 elseif xml.x_single_attribute then | |
61 single_attribute = xml.x_single_attribute | |
62 value_where = "in_single_attribute" | |
63 end | |
64 end | |
65 if propschema["const"] then | |
66 enums = {propschema["const"]} | |
67 elseif propschema["enum"] then | |
68 enums = propschema["enum"] | |
69 end | |
70 end | |
71 | |
72 if proptype == "object" or proptype == "array" then | |
73 value_where = "in_children" | |
74 end | |
75 | |
76 return proptype, value_where, name, namespace, prefix, single_attribute, enums | |
77 end | |
78 | |
25 local function parse_object(schema, s) | 79 local function parse_object(schema, s) |
26 local out = {} | 80 local out = {} |
27 if schema.properties then | 81 if schema.properties then |
28 for prop, propschema in pairs(schema.properties) do | 82 for prop, propschema in pairs(schema.properties) do |
29 | 83 |
30 local name = prop | 84 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) |
31 local namespace = s.attr.xmlns; | |
32 local prefix = nil | |
33 local value_where = "in_text_tag" | |
34 local single_attribute | |
35 local enums | |
36 | |
37 local proptype | |
38 if type(propschema) == "table" then | |
39 proptype = propschema.type | |
40 elseif type(propschema) == "string" then | |
41 proptype = propschema | |
42 end | |
43 | |
44 if proptype == "object" or proptype == "array" then | |
45 value_where = "in_children" | |
46 end | |
47 | |
48 if type(propschema) == "table" and propschema.xml then | |
49 if propschema.xml.name then | |
50 name = propschema.xml.name | |
51 end | |
52 if propschema.xml.namespace then | |
53 namespace = propschema.xml.namespace | |
54 end | |
55 if propschema.xml.prefix then | |
56 prefix = propschema.xml.prefix | |
57 end | |
58 if propschema.xml.attribute then | |
59 value_where = "in_attribute" | |
60 elseif propschema.xml.text then | |
61 | |
62 value_where = "in_text" | |
63 elseif propschema.xml.x_name_is_value then | |
64 | |
65 value_where = "in_tag_name" | |
66 elseif propschema.xml.x_single_attribute then | |
67 | |
68 single_attribute = propschema.xml.x_single_attribute | |
69 value_where = "in_single_attribute" | |
70 end | |
71 if propschema["const"] then | |
72 enums = {propschema["const"]} | |
73 elseif propschema["enum"] then | |
74 enums = propschema["enum"] | |
75 end | |
76 end | |
77 | 85 |
78 local value | 86 local value |
79 if value_where == "in_tag_name" then | 87 if value_where == "in_tag_name" then |
80 local c | 88 local c |
81 if proptype == "boolean" then | 89 if proptype == "boolean" then |
150 | 158 |
151 for prop, propschema in pairs(schema.properties) do | 159 for prop, propschema in pairs(schema.properties) do |
152 local v = t[prop] | 160 local v = t[prop] |
153 | 161 |
154 if v ~= nil then | 162 if v ~= nil then |
155 local proptype | 163 |
156 if type(propschema) == "table" then | 164 local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(propschema, prop, current_ns) |
157 proptype = propschema.type | |
158 elseif type(propschema) == "string" then | |
159 proptype = propschema | |
160 end | |
161 | |
162 local name = prop | |
163 local namespace = current_ns | |
164 local prefix = nil | |
165 local value_where = "in_text_tag" | |
166 local single_attribute | |
167 | |
168 if type(propschema) == "table" and propschema.xml then | |
169 | |
170 if propschema.xml.name then | |
171 name = propschema.xml.name | |
172 end | |
173 if propschema.xml.namespace then | |
174 namespace = propschema.xml.namespace | |
175 end | |
176 | |
177 if propschema.xml.prefix then | |
178 prefix = propschema.xml.prefix | |
179 end | |
180 | |
181 if propschema.xml.attribute then | |
182 value_where = "in_attribute" | |
183 elseif propschema.xml.text then | |
184 value_where = "in_text" | |
185 elseif propschema.xml.x_name_is_value then | |
186 value_where = "in_tag_name" | |
187 elseif propschema.xml.x_single_attribute then | |
188 single_attribute = propschema.xml.x_single_attribute | |
189 value_where = "in_single_attribute" | |
190 end | |
191 end | |
192 | 165 |
193 if value_where == "in_attribute" then | 166 if value_where == "in_attribute" then |
194 local attr = name | 167 local attr = name |
195 if prefix then | 168 if prefix then |
196 attr = prefix .. ":" .. name | 169 attr = prefix .. ":" .. name |