Changeset

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
parents 11465:19a88b61ab4e
children 11467:88792dd2bee9
files spec/util_datamapper_spec.lua teal-src/util/datamapper.tl util/datamapper.lua
diffstat 3 files changed, 85 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_datamapper_spec.lua	Sat Mar 20 19:02:18 2021 +0100
+++ b/spec/util_datamapper_spec.lua	Sat Mar 20 20:45:06 2021 +0100
@@ -9,6 +9,7 @@
 describe("util.datampper", function()
 
 	local s, x, d
+	local disco, disco_info, disco_schema
 	setup(function()
 
 		local function attr() return {type = "string"; xml = {attribute = true}} end
@@ -91,12 +92,74 @@
 				};
 			};
 		};
+
+		disco_schema = {
+			type = "object";
+			xml = {
+				name = "iq";
+				namespace = "jabber:client"
+			};
+			properties = {
+				to = attr();
+				from = attr();
+				type = attr();
+				id = attr();
+				disco = {
+					type = "object";
+					xml = {
+						name = "query";
+						namespace	= "http://jabber.org/protocol/disco#info"
+					};
+					properties = {
+						features = {
+							type = "array";
+							items = {
+								type = "string";
+								xml = {
+									name = "feature";
+									x_single_attribute = "var";
+								};
+							};
+						};
+					};
+				};
+			};
+		};
+
+		disco_info = xml.parse[[
+		<iq type="result" id="disco1" from="example.com">
+			<query xmlns="http://jabber.org/protocol/disco#info">
+				<feature var="urn:example:feature:1">wrong</feature>
+				<feature var="urn:example:feature:2"/>
+				<feature var="urn:example:feature:3"/>
+				<unrelated var="urn:example:feature:not"/>
+			</query>
+		</iq>
+		]];
+
+		disco = {
+			type="result";
+			id="disco1";
+			from="example.com";
+			disco = {
+				features = {
+					"urn:example:feature:1";
+					"urn:example:feature:2";
+					"urn:example:feature:3";
+				};
+			};
+		};
 	end);
 
 	describe("parse", function()
 		it("works", function()
 			assert.same(d, map.parse(s, x));
 		end);
+
+		it("handles arrays", function ()
+			assert.same(disco, map.parse(disco_schema, disco_info));
+		end);
+
 	end);
 
 	describe("unparse", function()
--- a/teal-src/util/datamapper.tl	Sat Mar 20 19:02:18 2021 +0100
+++ b/teal-src/util/datamapper.tl	Sat Mar 20 20:45:06 2021 +0100
@@ -60,7 +60,7 @@
 local function unpack_propschema( propschema : schema_t, propname : string, current_ns : string )
 		: json_type_name, value_goes, string, string, string, string, { any }
 	local proptype : json_type_name = "string"
-	local value_where : value_goes = "in_text_tag"
+	local value_where : value_goes = propname and "in_text_tag" or "in_text"
 	local name = propname
 	local namespace = current_ns
 	local prefix : string
@@ -191,21 +191,17 @@
 end
 
 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any }
-	local proptype, value_where, child_name, namespace = unpack_propschema(schema.items, nil, s.attr.xmlns)
+	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, 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;
-		if value_where == "in_text_tag" then
-			value = c:get_text();
-		else
-			error "NYI"
-		end
+		local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
 
-		value = totype(proptype, value)
-
-		if value ~= nil then
-			table.insert(out, value);
-		end
+		table.insert(out, totype(proptype, value));
 	end
 	return out;
 end
@@ -329,10 +325,10 @@
 		if proptype == "string" then
 			for _, item in ipairs(t as { string }) do
 				if value_where == "in_text_tag" then
-					out:text_tag(name, item, { xmlns = namespace });
-				else
-					error "NYI"
-				end
+				out:text_tag(name, item, { xmlns = namespace });
+			else
+				error "NYI"
+			end
 			end
 		else
 			error "NYI"
--- a/util/datamapper.lua	Sat Mar 20 19:02:18 2021 +0100
+++ b/util/datamapper.lua	Sat Mar 20 20:45:06 2021 +0100
@@ -27,7 +27,7 @@
 local function unpack_propschema(propschema, propname, current_ns)
 
 	local proptype = "string"
-	local value_where = "in_text_tag"
+	local value_where = propname and "in_text_tag" or "in_text"
 	local name = propname
 	local namespace = current_ns
 	local prefix
@@ -158,21 +158,17 @@
 end
 
 function parse_array(schema, s)
-	local proptype, value_where, child_name, namespace = unpack_propschema(schema.items, nil, s.attr.xmlns)
+	local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, 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;
-		if value_where == "in_text_tag" then
-			value = c:get_text();
-		else
-			error("NYI")
-		end
+		local value = extract_value(c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
 
-		value = totype(proptype, value)
-
-		if value ~= nil then
-			table.insert(out, value);
-		end
+		table.insert(out, totype(proptype, value));
 	end
 	return out
 end