Comparison

util/datamapper.lua @ 11475:9bd36e871f05

util.datamapper: Factor out conversion from any value to XML string Since this was the last severely duplicated code left.
author Kim Alvefur <zash@zash.se>
date Mon, 22 Mar 2021 22:24:39 +0100
parent 11471:ab03de8e503e
child 11476:83e127eb91f9
comparison
equal deleted inserted replaced
11474:8fba807e5256 11475:9bd36e871f05
197 else 197 else
198 error("top-level scalars unsupported") 198 error("top-level scalars unsupported")
199 end 199 end
200 end 200 end
201 201
202 local function toxmlstring(proptype, v)
203 if proptype == "string" and type(v) == "string" then
204 return v
205 elseif proptype == "number" and type(v) == "number" then
206 return string.format("%g", v)
207 elseif proptype == "integer" and type(v) == "number" then
208 return string.format("%d", v)
209 elseif proptype == "boolean" then
210 return v and "1" or "0"
211 end
212 end
213
202 local unparse 214 local unparse
203 215
204 local function unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, single_attribute) 216 local function unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, single_attribute)
205 if value_where == "in_attribute" then 217 if value_where == "in_attribute" then
206 local attr = name 218 local attr = name
208 attr = prefix .. ":" .. name 220 attr = prefix .. ":" .. name
209 elseif namespace ~= current_ns then 221 elseif namespace ~= current_ns then
210 attr = namespace .. "\1" .. name 222 attr = namespace .. "\1" .. name
211 end 223 end
212 224
213 if proptype == "string" and type(v) == "string" then 225 out.attr[attr] = toxmlstring(proptype, v)
214 out.attr[attr] = v
215 elseif proptype == "number" and type(v) == "number" then
216 out.attr[attr] = string.format("%g", v)
217 elseif proptype == "integer" and type(v) == "number" then
218 out.attr[attr] = string.format("%d", v)
219 elseif proptype == "boolean" then
220 out.attr[attr] = v and "1" or "0"
221 end
222 elseif value_where == "in_text" then 226 elseif value_where == "in_text" then
223 if type(v) == "string" then 227 out:text(toxmlstring(proptype, v))
224 out:text(v)
225 end
226 elseif value_where == "in_single_attribute" then 228 elseif value_where == "in_single_attribute" then
227 assert(single_attribute) 229 assert(single_attribute)
228 local propattr = {} 230 local propattr = {}
229 231
230 if namespace ~= current_ns then 232 if namespace ~= current_ns then
231 propattr.xmlns = namespace 233 propattr.xmlns = namespace
232 end 234 end
233 235
234 if proptype == "string" and type(v) == "string" then 236 propattr[single_attribute] = toxmlstring(proptype, v)
235 propattr[single_attribute] = v
236 elseif proptype == "number" and type(v) == "number" then
237 propattr[single_attribute] = string.format("%g", v)
238 elseif proptype == "integer" and type(v) == "number" then
239 propattr[single_attribute] = string.format("%d", v)
240 elseif proptype == "boolean" and type(v) == "boolean" then
241 propattr[single_attribute] = v and "1" or "0"
242 end
243 out:tag(name, propattr):up(); 237 out:tag(name, propattr):up();
244 238
245 else 239 else
246 local propattr 240 local propattr
247 if namespace ~= current_ns then 241 if namespace ~= current_ns then
251 if proptype == "string" and type(v) == "string" then 245 if proptype == "string" and type(v) == "string" then
252 out:tag(v, propattr):up(); 246 out:tag(v, propattr):up();
253 elseif proptype == "boolean" and v == true then 247 elseif proptype == "boolean" and v == true then
254 out:tag(name, propattr):up(); 248 out:tag(name, propattr):up();
255 end 249 end
256 elseif proptype == "string" and type(v) == "string" then
257 out:text_tag(name, v, propattr)
258 elseif proptype == "number" and type(v) == "number" then
259 out:text_tag(name, string.format("%g", v), propattr)
260 elseif proptype == "integer" and type(v) == "number" then
261 out:text_tag(name, string.format("%d", v), propattr)
262 elseif proptype == "boolean" and type(v) == "boolean" then
263 out:text_tag(name, v and "1" or "0", propattr)
264 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then 250 elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then
265 local c = unparse(propschema, v, name, namespace); 251 local c = unparse(propschema, v, name, namespace);
266 if c then 252 if c then
267 out:add_direct_child(c); 253 out:add_direct_child(c);
268 end 254 end
273 out:add_direct_child(c); 259 out:add_direct_child(c);
274 end 260 end
275 else 261 else
276 unparse(propschema, v, name, namespace, out); 262 unparse(propschema, v, name, namespace, out);
277 end 263 end
264 else
265 out:text_tag(name, toxmlstring(proptype, v), propattr)
278 end 266 end
279 end 267 end
280 end 268 end
281 269
282 function unparse(schema, t, current_name, current_ns, ctx) 270 function unparse(schema, t, current_name, current_ns, ctx)