Skip to content

Commit

Permalink
feat(table): ✨ statement to move or copy in a table with a certain en…
Browse files Browse the repository at this point in the history
…coding

This statements support also schema as encoding
  • Loading branch information
matteo-cristino authored and jaromil committed Oct 7, 2024
1 parent d7fd17e commit 18ca415
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/lua/zencode_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,54 @@
--on Monday, 28th August 2023
--]]

local function move_or_copy_in(src_value, src_name, dest, new)
local d = have(dest)
local function encode_from_to(src_name, src_enc, dest_enc)
local src_value, src_codec = have(src_name)
f_src_enc = get_encoding_function(src_enc)
if not f_src_enc then error("Encoding format not found: "..src_enc, 2) end
local encoded_src
-- accpet also schemas as encoding
if ZEN.schemas[uscore(src_enc)] then
if uscore(src_enc) ~= src_codec.schema then
error("Source schema: "..src_codec.schema.." does not match encoding "..src_enc)
end
if f_src_enc == default_export_f then
f_src_enc = function (obj)
if luatype(obj) == "table" then
return deepmap(CONF.output.encoding.fun, obj)
else
return CONF.output.encoding.fun(obj)
end
end
end
if src_codec.zentype == "e" then
encoded_src = I.spy(f_src_enc(I.spy(src_value)))
else
encoded_src = {}
for k,v in src_value do
encoded_src[k] = f_src_enc(src_value)
end
end
else
encoded_src = deepmap(f_src_enc, src_value)
end
f_dest_enc = input_encoding(dest_enc)
if not f_dest_enc then error("Destination encoding format not found: "..dest_enc, 2) end
return deepmap(f_dest_enc.fun, I.spy(encoded_src))
end

local function move_or_copy_in(src_value, src_name, dest, new, enc)
local d, cdest = have(dest)
if luatype(d) ~= 'table' then error("Object is not a table: "..dest, 2) end
local new_name = new or src_name
local cdest = CODEC[dest]
if enc then
src_value = encode_from_to(src_name, enc, cdest.encoding)
end
if cdest.zentype == 'e' and cdest.schema then
local sdest = ZEN.schemas[cdest.schema]
if luatype(sdest) ~= 'table' then -- old schema types are not open
error("Schema is not open to accept extra objects: "..dest)
error("Schema is not open to accept extra objects: "..dest, 2)
elseif not sdest.schematype or sdest.schematype ~= 'open' then
error("Schema is not open to accept extra objects: "..dest)
error("Schema is not open to accept extra objects: "..dest, 2)
end
if d[new_name] then
error("Cannot overwrite: "..new_name.." in "..dest,2)
Expand Down Expand Up @@ -64,7 +101,13 @@ When("move '' in ''", function(src, dest)
end)

When("move '' to '' in ''", function(src, new, dest)
move_or_copy_in(have(src), src, dest, new) ---old, new, inside, true)
move_or_copy_in(have(src), src, dest, new)
ACK[src] = nil
CODEC[src] = nil
end)

When("move '' as '' in ''", function(src, enc, dest)
move_or_copy_in(have(src), src, dest, nil, enc)
ACK[src] = nil
CODEC[src] = nil
end)
Expand All @@ -87,6 +130,10 @@ When("copy '' to '' in ''", function(src, new, dest)
move_or_copy_in(have(src), src, dest, new)
end)

When("copy '' as '' in ''", function(src, enc, dest)
move_or_copy_in(have(src), src, dest, nil, enc)
end)

When("copy '' from '' in ''", function(name, src, dest)
move_or_copy_in(have({src, name}), name, dest)
end)
Expand Down
48 changes: 48 additions & 0 deletions test/zencode/dictionary.bats
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,51 @@ EOF
save_output 'copy_from_schema_dictionary.json'
assert_output '{"copy":{"address":"0x2B8070975AF995Ef7eb949AE28ee7706B9039504","signature":"0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b"}}'
}

# TODO: move to table.bats (not doing in this moment since will create a lot of conflicts with another PR)
@test "move and copy statements" {
cat <<EOF | save_asset move_and_copy_statements.data.json
{
"my_dict": {
"str": "hello"
},
"keyring": {
"eddsa": "Cwj9CcqHNoBnXBo8iDfnhFkQeDun4Y4LStd2m3TEAYAg",
"ecdh": "yxL8P0pLHxsl3m8Nt6m9Zm0wSMzrWlD0JTAWlaheQg4="
},
"to_be_signed": "some data to be signed",
"base64_string": "aGVsbG8gbXkgZnJpZW5k"
}
EOF
cat <<EOF | zexe move_and_copy_statements.zen move_and_copy_statements.data.json
Scenario 'eddsa': sign
Scenario 'ecdh': sign
Given I have a 'keyring'
and I have a 'base64' named 'base64_string'
and I have a 'string' named 'to_be_signed'
and I have a 'string dictionary' named 'my_dict'
# sign
When I create the eddsa signature of 'to_be_signed'
and I create the ecdh signature of 'to_be_signed'
# copy
When I copy 'my_dict' to 'result_copy'
and I copy 'to_be_signed' to 'signed' in 'result_copy'
and I copy 'eddsa signature' as 'base58' in 'result_copy'
and I copy 'ecdh signature' as 'ecdh_signature' in 'result_copy'
and I copy 'base64_string' as 'string' in 'result_copy'
# move statement
When I rename 'my_dict' to 'result_move'
and I move 'to_be_signed' to 'signed' in 'result_move'
and I move 'eddsa signature' as 'base58' in 'result_move'
and I move 'ecdh signature' as 'ecdh_signature' in 'result_move'
and I move 'base64_string' as 'string' in 'result_move'
Then print the 'result_copy'
Then print the 'result_move'
EOF
save_output move_and_copy_statements.out.json
assert_output '{"result_copy":{"base64_string":"hello my friend","ecdh_signature":{"r":"d2tYw0FFyVU7UjX+IRpiN8SLkLR4S8bYZmCwI2rzurI=","s":"vUljXtnKkBqle/Ik7y3GfMa1o3wEIi4lRC+b/KmVbaI="},"eddsa_signature":"2iSCa9YxtYTQtmcUHHKsYZXVsqnSWSa7E2skWZqN4EdY8wBX9UuBgCYx7Np3hJmDVQ2NiHSebLHmz5BGH3cuZ4um","signed":"some data to be signed","str":"hello"},"result_move":{"base64_string":"hello my friend","ecdh_signature":{"r":"d2tYw0FFyVU7UjX+IRpiN8SLkLR4S8bYZmCwI2rzurI=","s":"vUljXtnKkBqle/Ik7y3GfMa1o3wEIi4lRC+b/KmVbaI="},"eddsa_signature":"2iSCa9YxtYTQtmcUHHKsYZXVsqnSWSa7E2skWZqN4EdY8wBX9UuBgCYx7Np3hJmDVQ2NiHSebLHmz5BGH3cuZ4um","signed":"some data to be signed","str":"hello"}}'
}

0 comments on commit 18ca415

Please sign in to comment.