Software /
code /
prosody-modules
Comparison
mod_rest/jsonmap.lib.lua @ 3895:25a3ad36ef3e
mod_rest: Rename loop variable for improved clarity
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Feb 2020 13:51:59 +0100 |
parent | 3894:14a32224900c |
child | 3896:987b203bb091 |
comparison
equal
deleted
inserted
replaced
3894:14a32224900c | 3895:25a3ad36ef3e |
---|---|
409 by = error.attr.by, | 409 by = error.attr.by, |
410 }; | 410 }; |
411 return t; | 411 return t; |
412 end | 412 end |
413 | 413 |
414 for k, typ in pairs(field_mappings) do | 414 for k, mapping in pairs(field_mappings) do |
415 if typ == "text_tag" then | 415 if mapping == "text_tag" then |
416 t[k] = s:get_child_text(k); | 416 t[k] = s:get_child_text(k); |
417 elseif typ[1] == "text_tag" then | 417 elseif mapping[1] == "text_tag" then |
418 t[k] = s:get_child_text(typ[3], typ[2]); | 418 t[k] = s:get_child_text(mapping[3], mapping[2]); |
419 elseif typ[1] == "name" then | 419 elseif mapping[1] == "name" then |
420 local child = s:get_child(nil, typ[2]); | 420 local child = s:get_child(nil, mapping[2]); |
421 if child then | 421 if child then |
422 t[k] = child.name; | 422 t[k] = child.name; |
423 end | 423 end |
424 elseif typ[1] == "attr" then | 424 elseif mapping[1] == "attr" then |
425 local child = s:get_child(typ[3], typ[2]) | 425 local child = s:get_child(mapping[3], mapping[2]) |
426 if child then | 426 if child then |
427 t[k] = child.attr[typ[4]]; | 427 t[k] = child.attr[mapping[4]]; |
428 end | 428 end |
429 elseif typ[1] == "bool_tag" then | 429 elseif mapping[1] == "bool_tag" then |
430 if s:get_child(typ[3], typ[2]) then | 430 if s:get_child(mapping[3], mapping[2]) then |
431 t[k] = true; | 431 t[k] = true; |
432 end | 432 end |
433 elseif typ[1] == "func" then | 433 elseif mapping[1] == "func" then |
434 local child = s:get_child(typ[3], typ[2] or k); | 434 local child = s:get_child(mapping[3], mapping[2] or k); |
435 -- TODO handle err | 435 -- TODO handle err |
436 if child then | 436 if child then |
437 t[k] = typ[4](child); | 437 t[k] = mapping[4](child); |
438 end | 438 end |
439 end | 439 end |
440 end | 440 end |
441 | 441 |
442 return t; | 442 return t; |
485 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | 485 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); |
486 return s; | 486 return s; |
487 end | 487 end |
488 | 488 |
489 for k, v in pairs(t) do | 489 for k, v in pairs(t) do |
490 local typ = field_mappings[k]; | 490 local mapping = field_mappings[k]; |
491 if typ then | 491 if mapping then |
492 if typ == "text_tag" then | 492 if mapping == "text_tag" then |
493 s:text_tag(k, v); | 493 s:text_tag(k, v); |
494 elseif typ == "attr" then -- luacheck: ignore 542 | 494 elseif mapping == "attr" then -- luacheck: ignore 542 |
495 -- handled already | 495 -- handled already |
496 elseif typ[1] == "text_tag" then | 496 elseif mapping[1] == "text_tag" then |
497 s:text_tag(typ[3] or k, v, typ[2] and { xmlns = typ[2] }); | 497 s:text_tag(mapping[3] or k, v, mapping[2] and { xmlns = mapping[2] }); |
498 elseif typ[1] == "name" then | 498 elseif mapping[1] == "name" then |
499 s:tag(v, { xmlns = typ[2] }):up(); | 499 s:tag(v, { xmlns = mapping[2] }):up(); |
500 elseif typ[1] == "attr" then | 500 elseif mapping[1] == "attr" then |
501 s:tag(typ[3] or k, { xmlns = typ[2], [ typ[4] or k ] = v }):up(); | 501 s:tag(mapping[3] or k, { xmlns = mapping[2], [ mapping[4] or k ] = v }):up(); |
502 elseif typ[1] == "bool_tag" then | 502 elseif mapping[1] == "bool_tag" then |
503 s:tag(typ[3] or k, { xmlns = typ[2] }):up(); | 503 s:tag(mapping[3] or k, { xmlns = mapping[2] }):up(); |
504 elseif typ[1] == "func" then | 504 elseif mapping[1] == "func" then |
505 s:add_child(typ[5](v)):up(); | 505 s:add_child(mapping[5](v)):up(); |
506 end | 506 end |
507 else | 507 else |
508 return nil, "unknown-field"; | 508 return nil, "unknown-field"; |
509 end | 509 end |
510 end | 510 end |