Comparison

util/datamapper.lua @ 11465:19a88b61ab4e

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.
author Kim Alvefur <zash@zash.se>
date Sat, 20 Mar 2021 19:02:18 +0100
parent 11464:6e25409fecbd
child 11466:c098d07e6717
comparison
equal deleted inserted replaced
11464:6e25409fecbd 11465:19a88b61ab4e
80 end 80 end
81 81
82 local parse_object 82 local parse_object
83 local parse_array 83 local parse_array
84 84
85 local function extract_value(s, value_where, proptype, name, namespace, prefix, single_attribute, enums)
86 if value_where == "in_tag_name" then
87 local c
88 if proptype == "boolean" then
89 c = s:get_child(name, namespace);
90 elseif enums and proptype == "string" then
91
92 for i = 1, #enums do
93 c = s:get_child(enums[i], namespace);
94 if c then
95 break
96 end
97 end
98 else
99 c = s:get_child(nil, namespace);
100 end
101 if c then
102 return c.name
103 end
104 elseif value_where == "in_attribute" then
105 local attr = name
106 if prefix then
107 attr = prefix .. ":" .. name
108 elseif namespace ~= s.attr.xmlns then
109 attr = namespace .. "\1" .. name
110 end
111 return s.attr[attr]
112
113 elseif value_where == "in_text" then
114 return s:get_text()
115
116 elseif value_where == "in_single_attribute" then
117 local c = s:get_child(name, namespace)
118 return c and c.attr[single_attribute]
119 elseif value_where == "in_text_tag" then
120 return s:get_child_text(name, namespace)
121 end
122 end
123
85 function parse_object(schema, s) 124 function parse_object(schema, s)
86 local out = {} 125 local out = {}
87 if type(schema) == "table" and schema.properties then 126 if type(schema) == "table" and schema.properties then
88 for prop, propschema in pairs(schema.properties) do 127 for prop, propschema in pairs(schema.properties) do
89 128
90 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) 129 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns)
91 130
92 local value 131 if value_where == "in_children" and type(propschema) == "table" then
93 if value_where == "in_tag_name" then
94 local c
95 if proptype == "boolean" then
96 c = s:get_child(name, namespace);
97 elseif enums and proptype == "string" then
98
99 for i = 1, #enums do
100 c = s:get_child(enums[i], namespace);
101 if c then
102 break
103 end
104 end
105 else
106 c = s:get_child(nil, namespace);
107 end
108 if type(c) == "table" then
109 value = c.name;
110 end
111 elseif value_where == "in_attribute" then
112 local attr = name
113 if prefix then
114 attr = prefix .. ":" .. name
115 elseif namespace ~= s.attr.xmlns then
116 attr = namespace .. "\1" .. name
117 end
118 value = s.attr[attr]
119
120 elseif value_where == "in_text" then
121 value = s:get_text()
122
123 elseif value_where == "in_single_attribute" then
124 local c = s:get_child(name, namespace)
125 value = c and c.attr[single_attribute]
126 elseif value_where == "in_text_tag" then
127 value = s:get_child_text(name, namespace)
128 elseif value_where == "in_children" and type(propschema) == "table" then
129 if proptype == "object" then 132 if proptype == "object" then
130 local c = s:get_child(name, namespace) 133 local c = s:get_child(name, namespace)
131 if c then 134 if c then
132 out[prop] = parse_object(propschema, c); 135 out[prop] = parse_object(propschema, c);
133 end 136 end
142 out[prop] = parse_array(propschema, wrapper); 145 out[prop] = parse_array(propschema, wrapper);
143 else 146 else
144 error("unreachable") 147 error("unreachable")
145 end 148 end
146 else 149 else
147 error("unreachable") 150 local value = extract_value(s, value_where, proptype, name, namespace, prefix, single_attribute, enums)
148 end 151
149 if value_where ~= "in_children" and value_where ~= "in_wrapper" then
150 out[prop] = totype(proptype, value) 152 out[prop] = totype(proptype, value)
151 end 153 end
152 end 154 end
153 end 155 end
154 156