From 87474145e51cd0460e8c6608c2dcb7a712d9ef3c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 20 Mar 2021 19:02:18 +0100 Subject: util.datamapper: Factor out extraction of the XML part to use So extract_value() takes an XML tag and details about which part we're interested in and returns that. Factoring this out will help with array implementation since this will be common behavior. --- util/datamapper.lua | 82 +++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) (limited to 'util') diff --git a/util/datamapper.lua b/util/datamapper.lua index cb008695..32d01050 100644 --- a/util/datamapper.lua +++ b/util/datamapper.lua @@ -82,6 +82,45 @@ end local parse_object local parse_array +local function extract_value(s, value_where, proptype, name, namespace, prefix, single_attribute, enums) + if value_where == "in_tag_name" then + local c + if proptype == "boolean" then + c = s:get_child(name, namespace); + elseif enums and proptype == "string" then + + for i = 1, #enums do + c = s:get_child(enums[i], namespace); + if c then + break + end + end + else + c = s:get_child(nil, namespace); + end + if c then + return c.name + end + elseif value_where == "in_attribute" then + local attr = name + if prefix then + attr = prefix .. ":" .. name + elseif namespace ~= s.attr.xmlns then + attr = namespace .. "\1" .. name + end + return s.attr[attr] + + elseif value_where == "in_text" then + return s:get_text() + + elseif value_where == "in_single_attribute" then + local c = s:get_child(name, namespace) + return c and c.attr[single_attribute] + elseif value_where == "in_text_tag" then + return s:get_child_text(name, namespace) + end +end + function parse_object(schema, s) local out = {} if type(schema) == "table" and schema.properties then @@ -89,43 +128,7 @@ function parse_object(schema, s) local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) - local value - if value_where == "in_tag_name" then - local c - if proptype == "boolean" then - c = s:get_child(name, namespace); - elseif enums and proptype == "string" then - - for i = 1, #enums do - c = s:get_child(enums[i], namespace); - if c then - break - end - end - else - c = s:get_child(nil, namespace); - end - if type(c) == "table" then - value = c.name; - end - elseif value_where == "in_attribute" then - local attr = name - if prefix then - attr = prefix .. ":" .. name - elseif namespace ~= s.attr.xmlns then - attr = namespace .. "\1" .. name - end - value = s.attr[attr] - - elseif value_where == "in_text" then - value = s:get_text() - - elseif value_where == "in_single_attribute" then - local c = s:get_child(name, namespace) - value = c and c.attr[single_attribute] - elseif value_where == "in_text_tag" then - value = s:get_child_text(name, namespace) - elseif value_where == "in_children" and type(propschema) == "table" then + if value_where == "in_children" and type(propschema) == "table" then if proptype == "object" then local c = s:get_child(name, namespace) if c then @@ -144,9 +147,8 @@ function parse_object(schema, s) error("unreachable") end else - error("unreachable") - end - if value_where ~= "in_children" and value_where ~= "in_wrapper" then + local value = extract_value(s, value_where, proptype, name, namespace, prefix, single_attribute, enums) + out[prop] = totype(proptype, value) end end -- cgit v1.2.3