client: Move some generic utility functions to a helpers module

Thu, 23 Mar 2023 11:51:31 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 11:51:31 +0000
changeset 171
433a1f36d0d3
parent 170
db73c4c317ce
child 172
2c17151ed21b

client: Move some generic utility functions to a helpers module

scansion/helpers.lua file | annotate | diff | comparison | revisions
scansion/objects/client.lua file | annotate | diff | comparison | revisions
squishy file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scansion/helpers.lua	Thu Mar 23 11:51:31 2023 +0000
@@ -0,0 +1,30 @@
+local function filter_expression(script, s)
+	local expr = s:match("^%$%{(.+)%}$");
+	if not expr then return s end
+	local name, value_name = expr:match("^(.+)'s (.+)$");
+	assert(name, "Unable to parse expression: "..expr);
+	local key = value_name:lower():gsub(" ", "_");
+	assert(script.objects[name], "Unknown object called "..name);
+	local value = script.objects[name][key];
+	assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name);
+	return value;
+end
+
+local function fill_vars(script, stanza)
+	for k, v in pairs(stanza.attr) do
+		stanza.attr[k] = filter_expression(script, v);
+	end
+	for i, child in ipairs(stanza) do
+		if type(child) == "string" then
+			stanza[i] = filter_expression(script, child);
+		elseif type(child) == "table" then
+			fill_vars(script, child);
+		end
+	end
+	return stanza;
+end
+
+return {
+	filter_expression = filter_expression;
+	fill_vars = fill_vars;
+};
--- a/scansion/objects/client.lua	Thu Mar 23 11:43:25 2023 +0000
+++ b/scansion/objects/client.lua	Thu Mar 23 11:51:31 2023 +0000
@@ -8,31 +8,7 @@
 
 local stanzacmp = require "scansion.stanzacmp";
 
-local function filter_expression(script, s)
-	local expr = s:match("^%$%{(.+)%}$");
-	if not expr then return s end
-	local name, value_name = expr:match("^(.+)'s (.+)$");
-	assert(name, "Unable to parse expression: "..expr);
-	local key = value_name:lower():gsub(" ", "_");
-	assert(script.objects[name], "Unknown object called "..name);
-	local value = script.objects[name][key];
-	assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name);
-	return value;
-end
-
-local function fill_vars(script, stanza)
-	for k, v in pairs(stanza.attr) do
-		stanza.attr[k] = filter_expression(script, v);
-	end
-	for i, child in ipairs(stanza) do
-		if type(child) == "string" then
-			stanza[i] = filter_expression(script, child);
-		elseif type(child) == "table" then
-			fill_vars(script, child);
-		end
-	end
-	return stanza;
-end
+local helpers = require "scansion.helpers";
 
 return {
 	_validate = function (client)
@@ -81,7 +57,7 @@
 	end;
 
 	sends = function (client, data)
-		local stanza =  fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", "  ")))));
+		local stanza =  helpers.fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", "  ")))));
 		local wait, done = async.waiter();
 		local function handle_drained()
 			client.stream:unhook("drained", handle_drained);
@@ -98,7 +74,7 @@
 		local have_received_stanza = false;
 		data = table.concat(data):gsub("\t", "    "):gsub("^%s+", ""):gsub("%s+$", "");
 		if data ~= "nothing" then
-			expected_stanza = fill_vars(client.script, assert(parse_xml(data)));
+			expected_stanza = helpers.fill_vars(client.script, assert(parse_xml(data)));
 		end
 		local function stanza_handler(received_stanza)
 			have_received_stanza = true;
--- a/squishy	Thu Mar 23 11:43:25 2023 +0000
+++ b/squishy	Thu Mar 23 11:51:31 2023 +0000
@@ -6,6 +6,7 @@
 Module "scansion.async"
 Module "scansion.error"
 Module "scansion.parser"
+Module "scansion.helpers"
 Module "scansion.iterators"
 Module "scansion.objects.client"
 Module "scansion.queue"

mercurial