Comparison

util/datamapper.lua @ 11462:d1982b7eb00d

util.datamapper: Fix arrays nesting one level too deep
author Kim Alvefur <zash@zash.se>
date Fri, 19 Mar 2021 01:17:59 +0100
parent 11461:766b0eddd12c
child 11464:6e25409fecbd
comparison
equal deleted inserted replaced
11461:766b0eddd12c 11462:d1982b7eb00d
1 local st = require("util.stanza"); 1 local st = require("util.stanza");
2
3 local schema_t = {}
2 4
3 local function toboolean(s) 5 local function toboolean(s)
4 if s == "true" or s == "1" then 6 if s == "true" or s == "1" then
5 return true 7 return true
6 elseif s == "false" or s == "0" then 8 elseif s == "false" or s == "0" then
179 else 181 else
180 error("top-level scalars unsupported") 182 error("top-level scalars unsupported")
181 end 183 end
182 end 184 end
183 185
184 local function unparse(schema, t, current_name, current_ns) 186 local function unparse(schema, t, current_name, current_ns, ctx)
185 187
186 if schema.xml then 188 if schema.xml then
187 if schema.xml.name then 189 if schema.xml.name then
188 current_name = schema.xml.name 190 current_name = schema.xml.name
189 end 191 end
191 current_ns = schema.xml.namespace 193 current_ns = schema.xml.namespace
192 end 194 end
193 195
194 end 196 end
195 197
196 local out = st.stanza(current_name, {xmlns = current_ns}) 198 local out = ctx or st.stanza(current_name, {xmlns = current_ns})
197 199
198 if schema.type == "object" then 200 if schema.type == "object" then
199 201
200 for prop, propschema in pairs(schema.properties) do 202 for prop, propschema in pairs(schema.properties) do
201 local v = t[prop] 203 local v = t[prop]
266 local c = unparse(propschema, v, name, namespace); 268 local c = unparse(propschema, v, name, namespace);
267 if c then 269 if c then
268 out:add_direct_child(c); 270 out:add_direct_child(c);
269 end 271 end
270 elseif proptype == "array" and type(propschema) == "table" and type(v) == "table" then 272 elseif proptype == "array" and type(propschema) == "table" and type(v) == "table" then
271 local c = unparse(propschema, v, name, namespace); 273 if value_where == "in_wrapper" then
272 if c then 274 local c = unparse(propschema, v, name, namespace);
273 if value_where == "in_wrapper" then 275 if c then
274 local w = st.stanza(propschema.xml.name or name, {xmlns = propschema.xml.namespace or namespace})
275 w:add_direct_child(c);
276 out:add_direct_child(w);
277 else
278 out:add_direct_child(c); 276 out:add_direct_child(c);
279 end 277 end
278 else
279 unparse(propschema, v, name, namespace, out);
280 end 280 end
281 else 281 else
282 error("NYI") 282 error("NYI")
283 end 283 end
284 end 284 end