Commit b217d85d authored by Jaromil's avatar Jaromil
Browse files

zencode dictionary fix copy out to support array

works around some weird data structs where a single array element
contains the nested element to be copied... fix #366
parent 945b2111
......@@ -202,40 +202,45 @@ When("find the '' for dictionaries in '' where '' = ''",function(name, arr, left
}, arr)
end)
local function _extract(tab, ele, root)
local nr = root or 'nil'
ZEN.assert(luatype(tab) == 'table', "Object is not a table: "..nr)
ZEN.assert(ele, "Undefined key or index: "..ele.." in "..nr)
-- if tonumber(ele) then
-- ZEN.assert(isarray(tab), "Invalid index "..ele.." as object is not an array: "..nr)
-- else
-- ZEN.assert(isdictionary(tab), "Invalid key "..ele.." as object is not a dictionary: "..nr)
-- end
if #tab == 1 then
if tab[ele] then return tab[ele] end
if luatype(tab[1]) == 'table' then
if tab[1][ele] then
return tab[1][ele]
else
error("Member not found: "..ele.." in "..tab, 3)
end
else
error("Member not found: "..ele.." in "..tab, 3)
end
else
if tab[ele] then return tab[ele] end
end
error("Member not found: "..ele.." in "..tab, 3)
end
local function create_copy_f(root, in1, in2)
local r = have(root)
empty'copy'
ZEN.assert(luatype(r) == 'table', "Object is not a table:"..root)
ZEN.assert(in1, "Undefined key or index: "..in1.." in "..root)
local res
if tonumber(in1) then
ZEN.assert(isarray(r), "Invalid index "..in1.." as object is not an array: "..root)
else
ZEN.assert(isdictionary(r), "Invalid key "..in1.." as object is not a dictionary:"..root)
end
ACK.copy = r[in1]
ZEN.assert(ACK.copy, "Member not found: "..in1.." in "..root)
-- if in2 then empty(in2) else empty(in1) end
-- TODO: nested recursion into single array containing a dict (see #366)
local tmp = _extract(r, in1)
ZEN.assert(tmp, "Member not found: "..in1.." in "..root)
ACK.copy = _extract(r, in1, root)
if in2 then
if tonumber(in2) then
ZEN.assert(isarray(ACK.copy), "Invalid index "..in2.." as object is not an array: "..in1.." in "..root)
else
ZEN.assert(isdictionary(ACK.copy), "Invalid key "..in2.." as object is not a dictionary:"..in1.." in "..root)
end
ACK.copy = ACK.copy[in2]
ZEN.assert(ACK.copy, "Member not found: "..in2.." in "..in1.." in "..root)
end
new_codec('copy', { luatype = luatype(ACK.copy) }, root)
if ZEN.CODEC.copy.luatype == 'table' then
if isdictionary(ACK.copy) then
ZEN.CODEC.copy.zentype = 'dictionary'
elseif isarray(ACK.copy) then
ZEN.CODEC.copy.zentype = 'array'
else
ZEN.assert(false, "Unknown zentype for lua table element: "..dict.."."..name)
end
else
ZEN.CODEC.copy.zentype = 'element'
ACK.copy = _extract(ACK.copy, in2, in1)
end
new_codec('copy', nil, root)
end
When("create the copy of '' from dictionary ''", function(name, dict) create_copy_f(dict, name) end)
When("create the copy of '' from ''", function(name, dict) create_copy_f(dict, name) end)
......
......@@ -438,3 +438,67 @@ When I rename 'tempObject' to named by 'NewRecipient'
Then print the object named by 'NewRecipient'
EOF
cat <<EOF | save dictionary dict-into-array.json
{
"dataFromEndpoint": {
"data": {
"batches": [
{
"header": {
"signer_public_key": "0209373bda3561c82c246b226ab3dfdfcab5fbdcba3cb3969508ea5b427628bb1f",
"transaction_ids": [
"3ff09b69c5973eaed4f06214bfa45ad0cf3d88f92476f3f297b331efb7ec9ad51f27b94a217e0fe1c59a714f067bcdd9e595f8d73c8e3ab22a085b732fc8bb94"
]
},
"header_signature": "b81f74eec9f49fd8062c3a90dc5bd48249842ea6149b4a2e7cded38f50ca0e4d30cb66f6d74a807b737143f07fa2f7e807ef5dd44e3e03e89ac1f39ac41012f0",
"trace": false,
"transactions": [
{
"header": {
"batcher_public_key": "0209373bda3561c82c246b226ab3dfdfcab5fbdcba3cb3969508ea5b427628bb1f",
"dependencies": [],
"family_name": "restroom",
"family_version": "1.0",
"inputs": [
"c274b5"
],
"nonce": "",
"outputs": [
"c274b5"
],
"payload_sha512": "263e4334456dbe1c5904417e8d2b80d0235fd6766a7e71ca3b95bd0fa134b0fe7c722877ee44a715ac81d9dda0df8496abb3455c0f447c26e801dbdfba5f2bc3",
"signer_public_key": "0209373bda3561c82c246b226ab3dfdfcab5fbdcba3cb3969508ea5b427628bb1f"
},
"header_signature": "3ff09b69c5973eaed4f06214bfa45ad0cf3d88f92476f3f297b331efb7ec9ad51f27b94a217e0fe1c59a714f067bcdd9e595f8d73c8e3ab22a085b732fc8bb94",
"payload": "omV2YWx1ZXiTeyJkYXRhVG9TdG9yZSI6IlRHbG1aU0JwY3lCaWIzSnBibWNzSUd4bGRDQjFjeUJ6Y0dsalpTQnBkQ0IxY0NCM2FYUm9JSE52YldVZ1lteHZZMnRqYUdGcGJpQm1kVzVySVE9PSIsIm15MTI4Qml0c1JhbmRvbSI6IklBTVRDNzNEMlB0b0dPdEVRVTlPUXc9PSJ9Z2FkZHJlc3N4RmMyNzRiNTAyYjk5YmUxZDA1NmFmY2UzYTRmZTI0ZmM2Y2NmZjM0NzdmOGJlYjE5ODk2YjdhYTY2ZTQ1MjVjYTAwMTdkY2U="
}
]
}
],
"header": {
"batch_ids": [
"b81f74eec9f49fd8062c3a90dc5bd48249842ea6149b4a2e7cded38f50ca0e4d30cb66f6d74a807b737143f07fa2f7e807ef5dd44e3e03e89ac1f39ac41012f0"
],
"block_num": "50910",
"consensus": "CjEKBFNlYWwQ7sZSGN2NAyIhAlle86noklkwJcqHaKdsqYG3wT5nY+zuLpAhUSM9neiaEkBsxMiV0seIGVRIl1DVmP83/vbSme6fteGVmAlMeBe07QLmdw9Q3vynYLa2vvJ4+F6IrTUuGEZL1LdoGxIRo0tkGrUCCngKIQLOFYL60z4cyx702e+XrMqd1trEIZWdtR/Lv+sEW1vsdRJAbte6Bw6GixkD89OgkIRDvM1c8EoQ3NuEiBO3Pg72U+Ff37HahDK7qGIcnjVN/5ZkSF2ZXTtTQhlcTAmP+jrhgRoEcGJmdCIDMS4wKgZDb21taXQSQH1T1oahhMUWUTIPndLggRKKt2BZ059XM6x/sevk4BVHJsNylXeyHF/ONRn9fdw4F9rEtNLAgGGDV9KcSBuhvIwadwozCgZDb21taXQQ7sZSGN2NAyIhAs4VgvrTPhzLHvTZ75esyp3W2sQhlZ21H8u/6wRbW+x1EkBsxMiV0seIGVRIl1DVmP83/vbSme6fteGVmAlMeBe07QLmdw9Q3vynYLa2vvJ4+F6IrTUuGEZL1LdoGxIRo0tkGrUCCngKIQKSXcyCS0X60UbP5Hj0dag218kdsxT1lLXaHqnoVdvgNBJAt7JFxR/u2DMCAQus7oRlM/FhWr9ja/nov/3hgFSltcr9dQhRCaiynEPR/XpN2sV/vBohb+7DHPkiMQN9spKPAhoEcGJmdCIDMS4wKgZDb21taXQSQFbuGoovqhUcqacE9IPwT5HFkX/2rSnh/w7hMAx3O4FASGkcn1TXKRD23r5yktoibxRmigZGvKyFDhHezrFJAQwadwozCgZDb21taXQQ7sZSGN2NAyIhApJdzIJLRfrRRs/kePR1qDbXyR2zFPWUtdoeqehV2+A0EkBsxMiV0seIGVRIl1DVmP83/vbSme6fteGVmAlMeBe07QLmdw9Q3vynYLa2vvJ4+F6IrTUuGEZL1LdoGxIRo0tkGrUCCngKIQKe9eL0dTf0jPiq36TOpsmcCYOqLq4Xp22sERmaF8XuIhJAZh5oduHWyx0ovqjYg3pbci/VoB3y2s/mURH3cztVb7IGDJMSwoMG6yi468uvdWLzZt3qa2aKay0F6EDiGe3zbRoEcGJmdCIDMS4wKgZDb21taXQSQFyyWIq4PPpsICqH6twz5x202pQfcRNGWXhppLE+ujWUXkaucXNY+sUEtxaejZYrZa+oUkLXmEt81BpcizM/+k0adwozCgZDb21taXQQ7sZSGN2NAyIhAp714vR1N/SM+KrfpM6myZwJg6ourhenbawRGZoXxe4iEkBsxMiV0seIGVRIl1DVmP83/vbSme6fteGVmAlMeBe07QLmdw9Q3vynYLa2vvJ4+F6IrTUuGEZL1LdoGxIRo0tk",
"previous_block_id": "6cc4c895d2c7881954489750d598ff37fef6d299ee9fb5e19598094c7817b4ed02e6770f50defca760b6b6bef278f85e88ad352e18464bd4b7681b1211a34b64",
"signer_public_key": "02595ef3a9e892593025ca8768a76ca981b7c13e6763ecee2e902151233d9de89a",
"state_root_hash": "2debd89f31b8a8d18b663f16ab19d2b70ae79414a73d29b20eb19b60a4353997"
},
"header_signature": "6bfbf0662951751b62c6a43300d29cf9f80758d2ef0241d049f318d44ce0babd3228822482d57d161899598c5c876488e816e04a6a016c9653c1e26b15f2ca1c"
},
"link": "http://195.201.41.35:8008/blocks/6bfbf0662951751b62c6a43300d29cf9f80758d2ef0241d049f318d44ce0babd3228822482d57d161899598c5c876488e816e04a6a016c9653c1e26b15f2ca1c"
}
}
EOF
cat <<EOF | zexe dict-into-array.zen -a dict-into-array.json | jq .
Given I have a 'string dictionary' named 'data' inside 'dataFromEndpoint'
When I create the copy of 'batches' from dictionary 'data'
When I rename the 'copy' to 'batches'
When I create the copy of 'header_signature' from dictionary 'batches'
then print the 'copy'
and print the 'batches'
EOF
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment