Changeset

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
parents 11470:5ebad952ebf7
children 11472:c32753ceb0f0
files teal-src/util/datamapper.tl util/datamapper.lua
diffstat 2 files changed, 44 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/teal-src/util/datamapper.tl	Mon Mar 22 10:03:32 2021 +0100
+++ b/teal-src/util/datamapper.tl	Mon Mar 22 10:05:41 2021 +0100
@@ -188,17 +188,35 @@
 end
 
 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any }
-	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns)
+	local itemschema : schema_t = schema.items;
+	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(itemschema, nil, s.attr.xmlns)
 	local attr_name : string
 	if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed
 		value_where = "in_attribute";
 		attr_name = single_attribute;
 	end
 	local out : { any } = {}
-	for c in s:childtags(child_name, namespace) do
-		local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
 
-		table.insert(out, totype(proptype, value));
+	if proptype == "object" then
+		if itemschema is json_schema_object then
+			for c in s:childtags(child_name, namespace) do
+				table.insert(out, parse_object(itemschema, c));
+			end
+		else
+			error "array items must be schema object"
+		end
+	elseif proptype == "array" then
+		if itemschema is json_schema_object then
+			for c in s:childtags(child_name, namespace) do
+				table.insert(out, parse_array(itemschema, c));
+			end
+		end
+	else
+		for c in s:childtags(child_name, namespace) do
+			local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
+
+			table.insert(out, totype(proptype, value));
+		end
 	end
 	return out;
 end
--- a/util/datamapper.lua	Mon Mar 22 10:03:32 2021 +0100
+++ b/util/datamapper.lua	Mon Mar 22 10:05:41 2021 +0100
@@ -156,17 +156,35 @@
 end
 
 function parse_array(schema, s)
-	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns)
+	local itemschema = schema.items;
+	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(itemschema, nil, s.attr.xmlns)
 	local attr_name
 	if value_where == "in_single_attribute" then
 		value_where = "in_attribute";
 		attr_name = single_attribute;
 	end
 	local out = {}
-	for c in s:childtags(child_name, namespace) do
-		local value = extract_value(c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
 
-		table.insert(out, totype(proptype, value));
+	if proptype == "object" then
+		if type(itemschema) == "table" then
+			for c in s:childtags(child_name, namespace) do
+				table.insert(out, parse_object(itemschema, c));
+			end
+		else
+			error("array items must be schema object")
+		end
+	elseif proptype == "array" then
+		if type(itemschema) == "table" then
+			for c in s:childtags(child_name, namespace) do
+				table.insert(out, parse_array(itemschema, c));
+			end
+		end
+	else
+		for c in s:childtags(child_name, namespace) do
+			local value = extract_value(c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
+
+			table.insert(out, totype(proptype, value));
+		end
 	end
 	return out
 end