Skip to content

Commit

Permalink
fix(array): 🐛 last element of a dictionary is the last value sorted b…
Browse files Browse the repository at this point in the history
…y keys (#614)

small fix also to the codec of copy_of_last_element
  • Loading branch information
matteo-cristino authored Mar 15, 2023
1 parent 32dc014 commit dce4d2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lua/zencode_array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,19 @@ When("create the copy of last element in ''", function(obj_name)
end
ACK.copy_of_last_element = obj[#obj]

new_codec('copy_of_last_element', { encoding='string', zentype='element' })
new_codec('copy_of_last_element', { zentype='element' }, obj_name)
elseif obj_codec.zentype == 'dictionary' then
local elem = nil
for k, _ in sort_pairs(obj) do
elem = k
for _, v in sort_pairs(obj) do
elem = v
end
if not elem then
error("Last element doesn't exist for empty dictionary")
end

ACK.copy_of_last_element = elem

new_codec('copy_of_last_element', { encoding='string', zentype='element' })
new_codec('copy_of_last_element', { zentype='element' }, obj_name)
else
error("Cannot find last element in " .. obj_codec.zentype)
end
Expand Down
2 changes: 1 addition & 1 deletion test/zencode/array.bats
Original file line number Diff line number Diff line change
Expand Up @@ -797,5 +797,5 @@ Then print the 'last array'
Then print the 'copy of last element'
EOF
save_output "last_element.json"
assert_output '{"copy_of_last_element":"key","last_array":"foo"}'
assert_output '{"copy_of_last_element":"val","last_array":"foo"}'
}

0 comments on commit dce4d2e

Please sign in to comment.