Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit e16a71e

Browse files
committed
Add function for rec. converting tables to arrays
1 parent a344822 commit e16a71e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/test_utils.lua

+29
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,33 @@ function test_utils.get_shard_key_hash(key)
187187
return 1 + digest.guava(num, shards_n)
188188
end
189189

190+
--- This order function receives two arguments and returns true if the first
191+
--- argument should come first in the sorted array.
192+
local function sort_function(f, s)
193+
local allowed_types = {["string"] = true, ["number"] = true,
194+
["boolean"] = true}
195+
if allowed_types[type(f)] == nil or allowed_types[type(s)] == nil then
196+
error(string.format("Sort function should not be applied to keys, " ..
197+
"which types are not string, number or boolean. First arg is %s " ..
198+
"and second arg is %s", type(f), type(s)))
199+
end
200+
return tostring(f) < tostring(s)
201+
end
202+
203+
-- Converts map recursively into array. If an array with maps is
204+
-- encountered then all this maps will be converted too.
205+
function test_utils.convert_table_to_array(map)
206+
if type(map) == 'table' then
207+
local key_array = utils.get_keys(map)
208+
table.sort(key_array, sort_function)
209+
local res = {}
210+
for _, k in ipairs(key_array) do
211+
table.insert(res, {k, test_utils.convert_table_to_array(map[k])})
212+
end
213+
return res
214+
else
215+
return map
216+
end
217+
end
218+
190219
return test_utils

0 commit comments

Comments
 (0)