Skip to content

Commit

Permalink
feat(when): ✨ add statement to move or copy to an element with a cert…
Browse files Browse the repository at this point in the history
…ain encoding

remove some debug leftover and move the fucntion to apply a new encoding in zencode_data.lua
  • Loading branch information
matteo-cristino authored and jaromil committed Mar 18, 2024
1 parent 6e71ab7 commit e49c939
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 41 deletions.
41 changes: 41 additions & 0 deletions src/lua/zencode_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,44 @@ end
local pruned_tables = prune_in(pruned_values)
return pruned_tables
end

-- encode the octet in ACK[src_name] following the src_enc
-- and then it transform it back to octet following the dest_enc
-- @param src_name name of the variable in ACK
-- @param src_enc the encoding to use when transforming ACK[src_name] into string
-- @param dest_enc the encoding to use when transfroming back the string into octet
-- @return the octet/table of octets of the above transformation
function apply_encoding(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 = f_src_enc(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, encoded_src)
end
37 changes: 1 addition & 36 deletions src/lua/zencode_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,12 @@
--on Monday, 28th August 2023
--]]

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
if enc then
src_value = encode_from_to(src_name, enc, cdest.encoding)
src_value = apply_encoding(src_name, enc, cdest.encoding)
end
if cdest.zentype == 'e' and cdest.schema then
local sdest = ZEN.schemas[cdest.schema]
Expand Down
32 changes: 27 additions & 5 deletions src/lua/zencode_when.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,33 @@ When("create '' string of ''", function(encoding, src)
encoding = 'string' })
end)

When("copy '' to ''", function(old,new)
have(old)
empty(new)
ACK[new] = deepcopy(ACK[old])
new_codec(new, { }, old)
local function move_or_copy_to(src, dest, enc)
empty(dest)
if not enc then
ACK[dest] = deepcopy(ACK[src])
new_codec(dest, { }, src)
else
ACK[dest] = apply_encoding(src, enc, "string")
new_codec(dest, { encoding = 'string' })
end
end

When("copy '' to ''", move_or_copy_to)

When("copy '' as '' to ''", function(src, enc, dest)
move_or_copy_to(src, dest, enc)
end)

When("move '' to ''", function(src, dest)
move_or_copy_to(src, dest)
ACK[src] = nil
CODEC[src] = nil
end)

When("move '' as '' to ''", function(src, enc, dest)
move_or_copy_to(src, dest, enc)
ACK[src] = nil
CODEC[src] = nil
end)

When("copy contents of '' in ''", function(src,dst)
Expand Down
26 changes: 26 additions & 0 deletions test/zencode/when.bats
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,32 @@ EOF
assert_line --partial "When I move/copy '' from '' in ''"
}


@test "move/copy as to" {
cat <<EOF | save_asset move_from_to_existing_obj.data.json
{
"base64_string": "aGVsbG8gbXkgZnJpZW5k",
"ecdh_signature": {
"r":"d2tYw0FFyVU7UjX+IRpiN8SLkLR4S8bYZmCwI2rzurI=",
"s":"vUljXtnKkBqle/Ik7y3GfMa1o3wEIi4lRC+b/KmVbaI="
}
}
EOF
cat <<EOF | zexe move_copy_as_to.zen move_from_to_existing_obj.data.json
Scenario 'ecdh': sign
Given I have a 'ecdh signature'
and I have a 'base64' named 'base64_string'
When I copy 'base64_string' as 'string' to 'string_from_base64'
When I move 'ecdh signature' as 'ecdh signature' to 'new_ecdh_siganture_with_string_encoding'
Then print the 'string_from_base64'
Then print the 'new_ecdh_siganture_with_string_encoding'
EOF
save_output move_from_to_existing_obj.out.json
assert_output '{"new_ecdh_siganture_with_string_encoding":{"r":"d2tYw0FFyVU7UjX+IRpiN8SLkLR4S8bYZmCwI2rzurI=","s":"vUljXtnKkBqle/Ik7y3GfMa1o3wEIi4lRC+b/KmVbaI="},"string_from_base64":"hello my friend"}'
}

@test "exit with error message" {
cat <<EOF | save_asset error_message.data.json
{
Expand Down

0 comments on commit e49c939

Please sign in to comment.