Comparison

teal-src/util/datamapper.tl @ 11466:c098d07e6717

util.datamapper: Finally implement support for parsing arrays
author Kim Alvefur <zash@zash.se>
date Sat, 20 Mar 2021 20:45:06 +0100
parent 11465:19a88b61ab4e
child 11467:88792dd2bee9
comparison
equal deleted inserted replaced
11465:19a88b61ab4e 11466:c098d07e6717
58 end 58 end
59 59
60 local function unpack_propschema( propschema : schema_t, propname : string, current_ns : string ) 60 local function unpack_propschema( propschema : schema_t, propname : string, current_ns : string )
61 : json_type_name, value_goes, string, string, string, string, { any } 61 : json_type_name, value_goes, string, string, string, string, { any }
62 local proptype : json_type_name = "string" 62 local proptype : json_type_name = "string"
63 local value_where : value_goes = "in_text_tag" 63 local value_where : value_goes = propname and "in_text_tag" or "in_text"
64 local name = propname 64 local name = propname
65 local namespace = current_ns 65 local namespace = current_ns
66 local prefix : string 66 local prefix : string
67 local single_attribute : string 67 local single_attribute : string
68 local enums : { any } 68 local enums : { any }
189 189
190 return out 190 return out
191 end 191 end
192 192
193 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any } 193 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any }
194 local proptype, value_where, child_name, namespace = unpack_propschema(schema.items, nil, s.attr.xmlns) 194 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns)
195 local attr_name : string
196 if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed
197 value_where = "in_attribute";
198 attr_name = single_attribute;
199 end
195 local out : { any } = {} 200 local out : { any } = {}
196 for c in s:childtags(child_name, namespace) do 201 for c in s:childtags(child_name, namespace) do
197 local value : string; 202 local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
198 if value_where == "in_text_tag" then 203
199 value = c:get_text(); 204 table.insert(out, totype(proptype, value));
200 else
201 error "NYI"
202 end
203
204 value = totype(proptype, value)
205
206 if value ~= nil then
207 table.insert(out, value);
208 end
209 end 205 end
210 return out; 206 return out;
211 end 207 end
212 208
213 local function parse (schema : json_schema_object, s : st.stanza_t) : table 209 local function parse (schema : json_schema_object, s : st.stanza_t) : table
327 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns) 323 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns)
328 -- TODO , prefix, single_attribute 324 -- TODO , prefix, single_attribute
329 if proptype == "string" then 325 if proptype == "string" then
330 for _, item in ipairs(t as { string }) do 326 for _, item in ipairs(t as { string }) do
331 if value_where == "in_text_tag" then 327 if value_where == "in_text_tag" then
332 out:text_tag(name, item, { xmlns = namespace }); 328 out:text_tag(name, item, { xmlns = namespace });
333 else 329 else
334 error "NYI" 330 error "NYI"
335 end 331 end
336 end 332 end
337 else 333 else
338 error "NYI" 334 error "NYI"
339 end 335 end
340 return out; 336 return out;