Software /
code /
prosody
Comparison
teal-src/util/datamapper.tl @ 11458:0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 18 Mar 2021 13:07:10 +0100 |
parent | 11457:6a51749af7f4 |
child | 11459:86904555bffc |
comparison
equal
deleted
inserted
replaced
11457:6a51749af7f4 | 11458:0e00fa518688 |
---|---|
190 value = c:get_text(); | 190 value = c:get_text(); |
191 else | 191 else |
192 error "NYI" | 192 error "NYI" |
193 end | 193 end |
194 | 194 |
195 value = totype(proptype, value) | |
196 | |
195 if value ~= nil then | 197 if value ~= nil then |
196 table.insert(out, value); | 198 table.insert(out, value); |
197 end | 199 end |
198 end | 200 end |
199 return out; | 201 return out; |
208 error "top-level scalars unsupported" | 210 error "top-level scalars unsupported" |
209 end | 211 end |
210 end | 212 end |
211 | 213 |
212 local function unparse ( schema : js.schema_t, t : table, current_name : string, current_ns : string ) : st.stanza_t | 214 local function unparse ( schema : js.schema_t, t : table, current_name : string, current_ns : string ) : st.stanza_t |
213 if schema.type == "object" then | |
214 | 215 |
215 if schema.xml then | 216 if schema.xml then |
216 if schema.xml.name then | 217 if schema.xml.name then |
217 current_name = schema.xml.name | 218 current_name = schema.xml.name |
218 end | 219 end |
221 end | 222 end |
222 -- TODO prefix? | 223 -- TODO prefix? |
223 end | 224 end |
224 | 225 |
225 local out = st.stanza(current_name, { xmlns = current_ns }) | 226 local out = st.stanza(current_name, { xmlns = current_ns }) |
227 | |
228 if schema.type == "object" then | |
226 | 229 |
227 for prop, propschema in pairs(schema.properties) do | 230 for prop, propschema in pairs(schema.properties) do |
228 local v = t[prop] | 231 local v = t[prop] |
229 | 232 |
230 if v ~= nil then | 233 if v ~= nil then |
292 elseif proptype == "object" and propschema is js.schema_t and v is table then | 295 elseif proptype == "object" and propschema is js.schema_t and v is table then |
293 local c = unparse(propschema, v, name, namespace); | 296 local c = unparse(propschema, v, name, namespace); |
294 if c then | 297 if c then |
295 out:add_direct_child(c); | 298 out:add_direct_child(c); |
296 end | 299 end |
297 -- else TODO | 300 elseif proptype == "array" and propschema is js.schema_t and v is table then |
301 local c = unparse(propschema, v, name, namespace); | |
302 if c then | |
303 if value_where == "in_wrapper" then | |
304 local w = st.stanza(propschema.xml.name or name, { xmlns = propschema.xml.namespace or namespace }) | |
305 w:add_direct_child(c); | |
306 out:add_direct_child(w); | |
307 else | |
308 out:add_direct_child(c); | |
309 end | |
310 end | |
311 else | |
312 error "NYI" | |
298 end | 313 end |
299 end | 314 end |
300 end | 315 end |
301 end | 316 end |
302 return out; | 317 return out; |
303 | 318 |
319 elseif schema.type == "array" then | |
320 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns) | |
321 -- TODO , prefix, single_attribute | |
322 if proptype == "string" then | |
323 for _, item in ipairs(t as { string }) do | |
324 if value_where == "in_text_tag" then | |
325 out:text_tag(name, item, { xmlns = namespace }); | |
326 else | |
327 error "NYI" | |
328 end | |
329 end | |
330 else | |
331 error "NYI" | |
332 end | |
333 return out; | |
304 end | 334 end |
305 end | 335 end |
306 | 336 |
307 return { | 337 return { |
308 parse = parse, | 338 parse = parse, |