Thu, 23 Mar 2023 11:51:31 +0000
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;