summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/array.lua
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-03-25 10:27:27 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-03-25 10:27:27 +0100
commitdf6e17e48995f25e72509986f30700d778b179b6 (patch)
treef432c24b8e4ad81009188650dabfd99194883265 /misc/freeswitch/scripts/common/array.lua
parent11f186a118285fbc87a536af26730780a9ad01f5 (diff)
parentcce94a74aa5c9691f9b37cd9be5a6831f8063812 (diff)
Merge branch 'develop'5.1.2
Diffstat (limited to 'misc/freeswitch/scripts/common/array.lua')
-rw-r--r--misc/freeswitch/scripts/common/array.lua97
1 files changed, 97 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/array.lua b/misc/freeswitch/scripts/common/array.lua
new file mode 100644
index 0000000..b1b7a71
--- /dev/null
+++ b/misc/freeswitch/scripts/common/array.lua
@@ -0,0 +1,97 @@
+-- Gemeinschaft 5 module: array functions
+-- (c) AMOOMA GmbH 2013
+--
+
+module(...,package.seeall)
+
+function try(array, arguments)
+ if type(arguments) ~= 'string' or type(array) ~= 'table' then
+ return nil;
+ end
+
+ local result = array;
+
+ arguments:gsub('([^%.]+)', function(entry)
+ local success, result = pcall(function() result = (result[tonumber(entry) or entry]); end);
+ end);
+
+ return result;
+end
+
+
+function set(array, arguments, value)
+ local nop, arguments_count = arguments:gsub('%.', '');
+ local structure = array;
+ arguments:gsub('([^%.]+)', function(entry)
+ if arguments_count <= 0 then
+ structure[entry] = value;
+ elseif type(structure[entry]) == 'table' then
+ structure = structure[entry];
+ else
+ structure[entry] = {};
+ structure = structure[entry];
+ end
+ arguments_count = arguments_count - 1;
+ end);
+end
+
+
+function expand_variable(variable_path, variable_sets)
+ for index=1, #variable_sets do
+ local result = try(variable_sets[index], variable_path);
+ if result then
+ return result;
+ end
+ end
+end
+
+-- replace variables in a string by array values
+function expand_variables(line, ...)
+ local variable_sets = {...};
+ return (line:gsub('{([%a%d%._]+)}', function(captured)
+ return expand_variable(captured, variable_sets);
+ end))
+end
+
+
+-- concatenate array values
+function to_s(array, separator, prefix, suffix)
+ require 'common.str';
+
+ local buffer = '';
+ for key, value in pairs(array) do
+ buffer = common.str.append(buffer, value, separator, prefix, suffix);
+ end
+
+ return buffer;
+end
+
+-- concatenate array keys
+function keys_to_s(array, separator, prefix, suffix)
+ require 'common.str';
+
+ local buffer = '';
+ for key, value in pairs(array) do
+ buffer = common.str.append(buffer, key, separator, prefix, suffix);
+ end
+
+ return buffer;
+end
+
+-- convert to JSON
+function to_json(array)
+ require 'common.str';
+ local buffer = '{';
+ for key, value in pairs(array) do
+ if type(value) == 'table' then
+ buffer = buffer .. '"' .. key .. '":' .. to_json(value) .. ',';
+ else
+ buffer = buffer .. '"' .. key .. '":' .. common.str.to_json(value) .. ',';
+ end
+ end
+ if buffer:sub(-1) == ',' then
+ buffer = buffer:sub(1, -2);
+ end
+ buffer = buffer .. '}';
+ return buffer;
+end