Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 codec of copied elements from dictionary or array of schemas #724

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lua/zencode_array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ When("create the copy of element '' in array ''", function(pos, arr)
ZEN.assert(num, "Argument is not a position number: "..pos)
ZEN.assert(src[num], "No element found in: "..arr.."["..pos.."]")
ACK.copy = src[num]
new_codec('copy', {encoding = src_codec.encoding})
local n_codec = { encoding = src_codec.encoding }
-- table of schemas can only contain elements
if src_codec.schema then
n_codec.schema = src_codec.schema
n_codec.zentype = "e"
end
new_codec('copy', n_codec)
end)

local function _insert_in(what, dest)
Expand Down
10 changes: 8 additions & 2 deletions src/lua/zencode_dictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,18 @@ local function _extract(tab, ele, root)
end
local function create_copy_f(root, in1, in2)
empty'copy'
local r = have(root)
local r, r_codec = have(root)
ACK.copy = _extract(r, in1, root)
if in2 then
ACK.copy = _extract(ACK.copy, in2, in1)
end
new_codec('copy', nil, root)
local n_codec = { encoding = r_codec.encoding }
-- table of schemas can only contain elements
if r_codec.schema then
n_codec.schema = r_codec.schema
n_codec.zentype = "e"
end
new_codec('copy', n_codec)
ZEN.CODEC['copy'].name = in2 or in1
end
When("create the copy of '' from dictionary ''", function(name, dict) create_copy_f(dict, name) end)
Expand Down
26 changes: 26 additions & 0 deletions test/zencode/array.bats
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,29 @@ EOF
save_output "copy_element.json"
assert_output '{"dictionary_from_dictionary":{"paperino":"duck"},"string_from_array_1":"pluto","string_from_array_2":"paperino","string_from_array_3":"topolino","string_from_dictionary_1":"dog","string_from_dictionary_2":"mouse"}'
}

@test "copy element from schemas" {
cat << EOF | save_asset copy_from_schema_array.data
{
"addresses_signatures": [
{
"address": "0x2B8070975AF995Ef7eb949AE28ee7706B9039504",
"signature": "0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b",
},
{
"address": "0x3028806AC293B5aC9b863B685c73813626311DaD",
"signature": "0x40d305373c648bb6b2bbadebe02ada256a9d0b3d3c37367c0a2795e367b22f7372e40dfc3497927764d1585783d058e4367bb4d24d2107777d7aa4ddcb6593c71b"
}
]
}
EOF

cat << EOF | zexe copy_from_schema_array.zen copy_from_schema_array.data
Scenario 'ethereum': copy element
Given I have a 'ethereum address signature pair array' named 'addresses_signatures'
When I create the copy of element '1' in array 'addresses_signatures'
Then print the 'copy'
EOF
save_output 'copy_from_schema_array.json'
assert_output '{"copy":{"address":"0x2B8070975AF995Ef7eb949AE28ee7706B9039504","signature":"0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b"}}'
}
26 changes: 26 additions & 0 deletions test/zencode/dictionary.bats
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,29 @@ EOF
save_output 'move_in_array_of_schemas.json'
assert_output '{"signatures":["0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b"]}'
}

@test "copy element from schemas" {
cat << EOF | save_asset copy_from_schema_dictionary.data
{
"addresses_signatures": {
"add_sig_1":{
"address": "0x2B8070975AF995Ef7eb949AE28ee7706B9039504",
"signature": "0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b",
},
"add_sig_2":{
"address": "0x3028806AC293B5aC9b863B685c73813626311DaD",
"signature": "0x40d305373c648bb6b2bbadebe02ada256a9d0b3d3c37367c0a2795e367b22f7372e40dfc3497927764d1585783d058e4367bb4d24d2107777d7aa4ddcb6593c71b"
}
}
}
EOF

cat << EOF | zexe copy_from_schema_dictionary.zen copy_from_schema_dictionary.data
Scenario 'ethereum': copy element
Given I have a 'ethereum address signature pair dictionary' named 'addresses_signatures'
When I create the copy of 'add_sig_1' from dictionary 'addresses_signatures'
Then print the 'copy'
EOF
save_output 'copy_from_schema_dictionary.json'
assert_output '{"copy":{"address":"0x2B8070975AF995Ef7eb949AE28ee7706B9039504","signature":"0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b"}}'
}
Loading