Comparison

util/datamapper.lua @ 11471:ab03de8e503e

util.datamapper: Handle nested arrays or objects in arrays
author Kim Alvefur <zash@zash.se>
date Mon, 22 Mar 2021 10:05:41 +0100
parent 11470:5ebad952ebf7
child 11475:9bd36e871f05
comparison
equal deleted inserted replaced
11470:5ebad952ebf7 11471:ab03de8e503e
154 154
155 return out 155 return out
156 end 156 end
157 157
158 function parse_array(schema, s) 158 function parse_array(schema, s)
159 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns) 159 local itemschema = schema.items;
160 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(itemschema, nil, s.attr.xmlns)
160 local attr_name 161 local attr_name
161 if value_where == "in_single_attribute" then 162 if value_where == "in_single_attribute" then
162 value_where = "in_attribute"; 163 value_where = "in_attribute";
163 attr_name = single_attribute; 164 attr_name = single_attribute;
164 end 165 end
165 local out = {} 166 local out = {}
166 for c in s:childtags(child_name, namespace) do 167
167 local value = extract_value(c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums) 168 if proptype == "object" then
168 169 if type(itemschema) == "table" then
169 table.insert(out, totype(proptype, value)); 170 for c in s:childtags(child_name, namespace) do
171 table.insert(out, parse_object(itemschema, c));
172 end
173 else
174 error("array items must be schema object")
175 end
176 elseif proptype == "array" then
177 if type(itemschema) == "table" then
178 for c in s:childtags(child_name, namespace) do
179 table.insert(out, parse_array(itemschema, c));
180 end
181 end
182 else
183 for c in s:childtags(child_name, namespace) do
184 local value = extract_value(c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
185
186 table.insert(out, totype(proptype, value));
187 end
170 end 188 end
171 return out 189 return out
172 end 190 end
173 191
174 local function parse(schema, s) 192 local function parse(schema, s)