Software / code / prosody
Comparison
util/datamapper.lua @ 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 | 11461:766b0eddd12c |
comparison
equal
deleted
inserted
replaced
| 11457:6a51749af7f4 | 11458:0e00fa518688 |
|---|---|
| 160 value = c:get_text(); | 160 value = c:get_text(); |
| 161 else | 161 else |
| 162 error("NYI") | 162 error("NYI") |
| 163 end | 163 end |
| 164 | 164 |
| 165 value = totype(proptype, value) | |
| 166 | |
| 165 if value ~= nil then | 167 if value ~= nil then |
| 166 table.insert(out, value); | 168 table.insert(out, value); |
| 167 end | 169 end |
| 168 end | 170 end |
| 169 return out | 171 return out |
| 178 error("top-level scalars unsupported") | 180 error("top-level scalars unsupported") |
| 179 end | 181 end |
| 180 end | 182 end |
| 181 | 183 |
| 182 local function unparse(schema, t, current_name, current_ns) | 184 local function unparse(schema, t, current_name, current_ns) |
| 185 | |
| 186 if schema.xml then | |
| 187 if schema.xml.name then | |
| 188 current_name = schema.xml.name | |
| 189 end | |
| 190 if schema.xml.namespace then | |
| 191 current_ns = schema.xml.namespace | |
| 192 end | |
| 193 | |
| 194 end | |
| 195 | |
| 196 local out = st.stanza(current_name, {xmlns = current_ns}) | |
| 197 | |
| 183 if schema.type == "object" then | 198 if schema.type == "object" then |
| 184 | |
| 185 if schema.xml then | |
| 186 if schema.xml.name then | |
| 187 current_name = schema.xml.name | |
| 188 end | |
| 189 if schema.xml.namespace then | |
| 190 current_ns = schema.xml.namespace | |
| 191 end | |
| 192 | |
| 193 end | |
| 194 | |
| 195 local out = st.stanza(current_name, {xmlns = current_ns}) | |
| 196 | 199 |
| 197 for prop, propschema in pairs(schema.properties) do | 200 for prop, propschema in pairs(schema.properties) do |
| 198 local v = t[prop] | 201 local v = t[prop] |
| 199 | 202 |
| 200 if v ~= nil then | 203 if v ~= nil then |
| 262 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then | 265 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then |
| 263 local c = unparse(propschema, v, name, namespace); | 266 local c = unparse(propschema, v, name, namespace); |
| 264 if c then | 267 if c then |
| 265 out:add_direct_child(c); | 268 out:add_direct_child(c); |
| 266 end | 269 end |
| 267 | 270 elseif proptype == "array" and type(propschema) == "table" and type(v) == "table" then |
| 271 local c = unparse(propschema, v, name, namespace); | |
| 272 if c then | |
| 273 if value_where == "in_wrapper" 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); | |
| 279 end | |
| 280 end | |
| 281 else | |
| 282 error("NYI") | |
| 268 end | 283 end |
| 269 end | 284 end |
| 270 end | 285 end |
| 271 end | 286 end |
| 272 return out | 287 return out |
| 273 | 288 |
| 289 elseif schema.type == "array" then | |
| 290 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns) | |
| 291 | |
| 292 if proptype == "string" then | |
| 293 for _, item in ipairs(t) do | |
| 294 if value_where == "in_text_tag" then | |
| 295 out:text_tag(name, item, {xmlns = namespace}); | |
| 296 else | |
| 297 error("NYI") | |
| 298 end | |
| 299 end | |
| 300 else | |
| 301 error("NYI") | |
| 302 end | |
| 303 return out | |
| 274 end | 304 end |
| 275 end | 305 end |
| 276 | 306 |
| 277 return {parse = parse; unparse = unparse} | 307 return {parse = parse; unparse = unparse} |