Comparison

teal-src/util/datamapper.tl @ 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
186 186
187 return out 187 return out
188 end 188 end
189 189
190 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any } 190 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any }
191 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns) 191 local itemschema : schema_t = schema.items;
192 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(itemschema, nil, s.attr.xmlns)
192 local attr_name : string 193 local attr_name : string
193 if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed 194 if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed
194 value_where = "in_attribute"; 195 value_where = "in_attribute";
195 attr_name = single_attribute; 196 attr_name = single_attribute;
196 end 197 end
197 local out : { any } = {} 198 local out : { any } = {}
198 for c in s:childtags(child_name, namespace) do 199
199 local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums) 200 if proptype == "object" then
200 201 if itemschema is json_schema_object then
201 table.insert(out, totype(proptype, value)); 202 for c in s:childtags(child_name, namespace) do
203 table.insert(out, parse_object(itemschema, c));
204 end
205 else
206 error "array items must be schema object"
207 end
208 elseif proptype == "array" then
209 if itemschema is json_schema_object then
210 for c in s:childtags(child_name, namespace) do
211 table.insert(out, parse_array(itemschema, c));
212 end
213 end
214 else
215 for c in s:childtags(child_name, namespace) do
216 local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
217
218 table.insert(out, totype(proptype, value));
219 end
202 end 220 end
203 return out; 221 return out;
204 end 222 end
205 223
206 local function parse (schema : json_schema_object, s : st.stanza_t) : table 224 local function parse (schema : json_schema_object, s : st.stanza_t) : table