Skip to content

Commit

Permalink
feat: make flatten deterministic by sorting keys
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda authored and jaromil committed Aug 24, 2023
1 parent 3d9c2c3 commit 60c445e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lua/zencode_array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ When("create the flat array of contents in ''", function(dic)
ZEN.assert(luatype(data) == 'table', "Invalid array: "..dic)
empty'flat array'
ACK.flat_array = {}
deepmap(function(v, k, res) table.insert(res, v) end, data, ACK.flat_array)
deepsortmap(function(v, k, res) table.insert(res, v) end, data, ACK.flat_array)
new_codec('flat array', { encoding="string" })
end)

local function _keys_flat_array(data, res)
for k, item in pairs(data) do
for k, item in sort_pairs(data) do
if type(k) == 'string' then
k = O.from_string(k)
elseif type(k) == 'number' then
Expand Down
21 changes: 21 additions & 0 deletions src/lua/zenroom_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ function deepmap(fun,t,...)
return setmetatable(res, getmetatable(t))
end

function deepsortmap(fun,t,...)
local luatype = luatype
if luatype(fun) ~= 'function' then
error("Internal error: deepmap 1st argument is not a function", 3)
return nil end
-- if luatype(t) == 'number' then
-- return t end
if luatype(t) ~= 'table' then
return fun(t) end
-- error("Internal error: deepmap 2nd argument is not a table", 3)
-- return nil end
local res = {}
for k,v in sort_pairs(t) do
if luatype(v) == 'table' then
res[k] = deepsortmap(fun,v,...) -- recursion
else
res[k] = fun(v,k,...)
end
end
return setmetatable(res, getmetatable(t))
end

-- function to be used when converting codecs with complex trees
-- mask is a dictionary of functions to be applied in place
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/array.bats
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ and print the 'contents flat array'
EOF

output=`cat $TMP/out`
assert_output '{"contents_flat_array":["1644878787666","1644878787611","1644878787367","1644878787754","1644878787679","1644878787692","1644878787628","18485","135"],"keys_flat_array":["allTimestamps",1,2,3,4,5,6,"average","variance","standard_deviation"]}'
assert_output '{"contents_flat_array":["1644878787666","1644878787611","1644878787367","1644878787754","1644878787679","1644878787692","1644878787628","135","18485"],"keys_flat_array":["allTimestamps",1,2,3,4,5,6,"average","standard_deviation","variance"]}'
}

@test "Consensusroom flatten" {
Expand Down Expand Up @@ -559,7 +559,7 @@ Then print the 'flattened array 2'
Then print the string 'succes'
EOF
save_output "consensusroom-flatten.json"
assert_output '{"flattened_array_1":["https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp"],"flattened_array_2":["ip","port_https","get-6-timestampsAPI","uid","timestampAPI","announceAPI","baseUrl","port_http","public_key","version","tracker"],"output":["succes"]}'
assert_output '{"flattened_array_1":["https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp","https://api/dyneorgroom.net/api/dyneorg/consensusroom-get-timestamp"],"flattened_array_2":["announceAPI","baseUrl","get-6-timestampsAPI","ip","port_http","port_https","public_key","timestampAPI","tracker","uid","version"],"output":["succes"]}'
}

@test "dict2array" {
Expand Down

0 comments on commit 60c445e

Please sign in to comment.