From 75b6a08431077dda4fa29eb5be900e971ec985f1 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Mon, 11 Mar 2024 14:46:50 +0100 Subject: [PATCH] =?UTF-8?q?feat(when):=20=E2=9C=A8=20add=20statement=20to?= =?UTF-8?q?=20move=20or=20copy=20to=20an=20element=20with=20a=20certain=20?= =?UTF-8?q?encoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove some debug leftover and move the fucntion to apply a new encoding in zencode_data.lua --- src/lua/zencode_data.lua | 41 +++++++++++++++++++++++++++++++++++++++ src/lua/zencode_table.lua | 37 +---------------------------------- src/lua/zencode_when.lua | 32 +++++++++++++++++++++++++----- test/zencode/when.bats | 25 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 41 deletions(-) diff --git a/src/lua/zencode_data.lua b/src/lua/zencode_data.lua index 14edb853e..84625643d 100644 --- a/src/lua/zencode_data.lua +++ b/src/lua/zencode_data.lua @@ -581,3 +581,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 diff --git a/src/lua/zencode_table.lua b/src/lua/zencode_table.lua index 486abf7d4..e63aec981 100644 --- a/src/lua/zencode_table.lua +++ b/src/lua/zencode_table.lua @@ -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] diff --git a/src/lua/zencode_when.lua b/src/lua/zencode_when.lua index bdd770088..23b77bef7 100644 --- a/src/lua/zencode_when.lua +++ b/src/lua/zencode_when.lua @@ -227,11 +227,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) diff --git a/test/zencode/when.bats b/test/zencode/when.bats index 37ccde359..cf99af5ca 100644 --- a/test/zencode/when.bats +++ b/test/zencode/when.bats @@ -376,3 +376,28 @@ EOF assert_line --partial 'To copy/move element in existing element use:' assert_line --partial "When I move/copy '' from '' in ''" } + +@test "move/copy as to" { + cat <