From e362f56e7b5a5bc9843d8cc811de17757c4eb127 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Wed, 3 Apr 2024 12:00:46 +0200 Subject: [PATCH 1/6] =?UTF-8?q?feat(foreach):=20=E2=9C=A8=20add=20statemen?= =?UTF-8?q?t=20to=20exit=20from=20a=20foreach=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/zencode_foreach.lua | 5 +++++ test/zencode/foreach.bats | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/lua/zencode_foreach.lua b/src/lua/zencode_foreach.lua index 1979adf3a..deaff2e17 100644 --- a/src/lua/zencode_foreach.lua +++ b/src/lua/zencode_foreach.lua @@ -64,3 +64,8 @@ Foreach("'' in sequence from '' to '' with step ''", function(name, from_name, t end end end) + +When("exit from foreach loop", function() + zencode_assert(ZEN.ITER and ZEN.ITER.pos ~=0, "Can only exit from foreach loop") + ZEN.ITER.pos = 0 +end) diff --git a/test/zencode/foreach.bats b/test/zencode/foreach.bats index 87ed7fa0b..233087490 100644 --- a/test/zencode/foreach.bats +++ b/test/zencode/foreach.bats @@ -271,3 +271,47 @@ EOF assert_output '{"floats":[5,6,10,9,8,7,6,5,4,3]}' } +@test "exit from foreach loop" { + cat << EOF | save_asset exit_from_foreach.data +{ + "numbers": [1,2,3,4,5,6,7,8], + "limit": 4, + "zero": 0, + "two": 2, + "ten": 10 +} +EOF + + cat << EOF | zexe exit_from_foreach.zen exit_from_foreach.data +Given I have a 'float array' named 'numbers' +Given I have a 'float' named 'limit' +Given I have a 'float' named 'zero' +Given I have a 'float' named 'two' +Given I have a 'float' named 'ten' + +When I create the 'float array' named 'y' +Foreach 'x' in 'numbers' +If I verify 'x' is equal to 'limit' +When I exit from foreach loop +EndIf +When I move 'x' in 'y' +EndForeach + +If I verify 'x' is found +When I remove 'x' +EndIf + +When I create the 'float array' named 'z' +Foreach 'x' in sequence from 'zero' to 'ten' with step 'two' +If I verify 'x' is equal to 'limit' +When I exit from foreach loop +EndIf +When I copy 'x' in 'z' +EndForeach + +Then print 'y' +Then print 'z' +EOF + save_output "exit_from_foreach.out" + assert_output '{"y":[1,2,3],"z":[0,2]}' +} From e72e5bd20856d23544d3db3120740cab6234d6e3 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Thu, 4 Apr 2024 09:22:25 +0200 Subject: [PATCH 2/6] fix: add possibility to start a new foreach loop right after the end of a previous one --- src/lua/zencode.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua/zencode.lua b/src/lua/zencode.lua index 3cd201391..93d276a53 100644 --- a/src/lua/zencode.lua +++ b/src/lua/zencode.lua @@ -399,7 +399,7 @@ function ZEN:begin(new_heap) {name = 'enter_ifforeach', from = {'foreach', 'whenforeach', 'ifforeach', 'endifforeach'}, to = 'ifforeach'}, {name = 'enter_whenifforeach', from = {'ifforeach', 'whenifforeach'}, to = 'whenifforeach'}, {name = 'enter_endifforeach', from = {'ifforeach', 'whenifforeach'}, to = 'endifforeach'}, - {name = 'enter_foreach', from = {'given', 'when', 'endif', 'foreach'}, to = 'foreach'}, + {name = 'enter_foreach', from = {'given', 'when', 'endif', 'foreach', 'endforeach'}, to = 'foreach'}, {name = 'enter_whenforeach', from = {'foreach', 'whenforeach', 'endifforeach'}, to = 'whenforeach'}, {name = 'enter_endforeach', from = {'whenforeach', 'endifforeach'}, to = 'endforeach'}, {name = 'enter_and', from = 'when', to = 'when'}, From c7beb51bd629655880674ada1b6b5d33ff774a1f Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Thu, 4 Apr 2024 09:25:24 +0200 Subject: [PATCH 3/6] =?UTF-8?q?fix(foreach):=20=F0=9F=90=9B=20remove=20ite?= =?UTF-8?q?rators=20variable=20from=20ACK=20memory=20at=20the=20end=20of?= =?UTF-8?q?=20a=20foreach=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/zencode_foreach.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lua/zencode_foreach.lua b/src/lua/zencode_foreach.lua index deaff2e17..322090f01 100644 --- a/src/lua/zencode_foreach.lua +++ b/src/lua/zencode_foreach.lua @@ -1,3 +1,12 @@ +local function clear_iterators() + if not ZEN.ITER.names then return end + for _,v in pairs(ZEN.ITER.names) do + ACK[v] = nil + CODEC[v] = nil + end + ZEN.ITER.names = nil +end + Foreach("'' in ''", function(name, collection) local info = ZEN.ITER local col = have(collection) @@ -5,11 +14,14 @@ Foreach("'' in ''", function(name, collection) -- in the first itaration decale the index variable if info.pos == 1 or not ACK[name] then empty(name) + if info.names then table.insert(info.names, name) + else info.names = {name} end end -- skip execution in the last iteration if info.pos == #col+1 then info.pos = 0 + clear_iterators() else -- for each iteration read the value in the collection ACK[name] = col[info.pos] @@ -27,6 +39,8 @@ Foreach("'' in sequence from '' to '' with step ''", function(name, from_name, t if info.pos == 1 or not ACK[name] then empty(name) + if info.names then table.insert(info.names, name) + else info.names = {name} end end zencode_assert(type(from) == type(to) and type(to) == type(step), "Types must be equal in foreach declaration") @@ -56,6 +70,7 @@ Foreach("'' in sequence from '' to '' with step ''", function(name, from_name, t if finished then info.pos = 0 + clear_iterators() else ACK[name] = current_value if not CODEC[name] then @@ -68,4 +83,5 @@ end) When("exit from foreach loop", function() zencode_assert(ZEN.ITER and ZEN.ITER.pos ~=0, "Can only exit from foreach loop") ZEN.ITER.pos = 0 + clear_iterators() end) From b1ad82527daa5fcd2b1ed79abae3baae568921c8 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Thu, 4 Apr 2024 09:26:54 +0200 Subject: [PATCH 4/6] =?UTF-8?q?test(zencode):=20=F0=9F=A7=AA=20two=20near?= =?UTF-8?q?=20foreach=20loop=20with=20the=20same=20iterator=20variable=20n?= =?UTF-8?q?ame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/zencode/foreach.bats | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/zencode/foreach.bats b/test/zencode/foreach.bats index 233087490..9bd1d07b7 100644 --- a/test/zencode/foreach.bats +++ b/test/zencode/foreach.bats @@ -315,3 +315,34 @@ EOF save_output "exit_from_foreach.out" assert_output '{"y":[1,2,3],"z":[0,2]}' } + +@test "Two foreach with the same iterator variable name" { + cat << EOF | save_asset two_foreach_same_var.data +{ + "arr1": [ + "str1", + "str2" + ], + "arr2": [ + "str3" + ] +} +EOF + cat << EOF | zexe two_foreach_same_var.zen two_foreach_same_var.data +Given I have a 'string array' named 'arr1' +Given I have a 'string array' named 'arr2' + +When I create the 'string array' named 'res' +Foreach 'x' in 'arr1' +When I copy 'x' in 'res' +Endforeach + +Foreach 'x' in 'arr2' +When I copy 'x' in 'res' +Endforeach + +Then print the 'res' +EOF + save_output two_foreach_same_var.out + assert_output '{"res":["str1","str2","str3"]}' +} From 49a3588cc0c891ff7d7e403f2759eba5c8b5cbb9 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Thu, 4 Apr 2024 15:47:31 +0200 Subject: [PATCH 5/6] refactor(foreach): better statement to exit a foreach loop Add also break alias --- src/lua/zencode_foreach.lua | 7 +++++-- test/zencode/foreach.bats | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lua/zencode_foreach.lua b/src/lua/zencode_foreach.lua index 322090f01..41ff5d9e8 100644 --- a/src/lua/zencode_foreach.lua +++ b/src/lua/zencode_foreach.lua @@ -80,8 +80,11 @@ Foreach("'' in sequence from '' to '' with step ''", function(name, from_name, t end end) -When("exit from foreach loop", function() +local function break_foreach() zencode_assert(ZEN.ITER and ZEN.ITER.pos ~=0, "Can only exit from foreach loop") ZEN.ITER.pos = 0 clear_iterators() -end) +end + +When("exit foreach", break_foreach) +When("break foreach", break_foreach) diff --git a/test/zencode/foreach.bats b/test/zencode/foreach.bats index 9bd1d07b7..ee5506637 100644 --- a/test/zencode/foreach.bats +++ b/test/zencode/foreach.bats @@ -292,7 +292,7 @@ Given I have a 'float' named 'ten' When I create the 'float array' named 'y' Foreach 'x' in 'numbers' If I verify 'x' is equal to 'limit' -When I exit from foreach loop +When I break foreach EndIf When I move 'x' in 'y' EndForeach @@ -304,7 +304,7 @@ EndIf When I create the 'float array' named 'z' Foreach 'x' in sequence from 'zero' to 'ten' with step 'two' If I verify 'x' is equal to 'limit' -When I exit from foreach loop +When I exit foreach EndIf When I copy 'x' in 'z' EndForeach From a20689f982e65f8324c9812fed68c890d8c8d56c Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Thu, 4 Apr 2024 17:12:47 +0200 Subject: [PATCH 6/6] =?UTF-8?q?fix(foreach):=20=F0=9F=90=9B=20improve=20su?= =?UTF-8?q?pport=20for=20looping=20over=20array=20of=20shcema=20by=20savin?= =?UTF-8?q?g=20the=20shcema=20in=20the=20iterator=20codec=20when=20present?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/zencode_foreach.lua | 10 ++++++++-- test/zencode/foreach.bats | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/lua/zencode_foreach.lua b/src/lua/zencode_foreach.lua index 41ff5d9e8..719b8efab 100644 --- a/src/lua/zencode_foreach.lua +++ b/src/lua/zencode_foreach.lua @@ -10,7 +10,8 @@ end Foreach("'' in ''", function(name, collection) local info = ZEN.ITER local col = have(collection) - zencode_assert(CODEC[collection].zentype == "a", "Can only iterate over arrays") + local collection_codec = CODEC[collection] + zencode_assert(collection_codec.zentype == "a", "Can only iterate over arrays") -- in the first itaration decale the index variable if info.pos == 1 or not ACK[name] then empty(name) @@ -26,7 +27,12 @@ Foreach("'' in ''", function(name, collection) -- for each iteration read the value in the collection ACK[name] = col[info.pos] if not CODEC[name] then - new_codec(name, {encoding = CODEC[collection].encoding}) + local n_codec = {encoding = collection_codec.encoding} + if collection_codec.schema then + n_codec.schema = collection_codec.schema + n_codec.zentype = "e" + end + new_codec(name, n_codec) end end end) diff --git a/test/zencode/foreach.bats b/test/zencode/foreach.bats index ee5506637..37ec1ae09 100644 --- a/test/zencode/foreach.bats +++ b/test/zencode/foreach.bats @@ -346,3 +346,28 @@ EOF save_output two_foreach_same_var.out assert_output '{"res":["str1","str2","str3"]}' } + +@test "Foreach on array of schema" { + cat << EOF | save_asset foreach_schema.data +{ + "arr": [ + "eyJhbGciOiAiRVMyNTYiLCAidHlwIjogInZjK3NkLWp3dCJ9.eyJfc2QiOiBbInZucGt1cWZBSWFBTlBmZXl6WXhUVllWUGxJY3JBWlVvU3N5TGFhQ0tlWUkiLCAiM1BEeUhOMXphcklJMG1TdDUwMkV2ZVIwVHhlOHlTQ1hDOFlYd1NvV1lZWSIsICJFMS12Wnl1Wmhlbkhlam1nYi1kTFhtaDBPODFLTUtWU25RYjN2Mjl6SGlRIl0sICJfc2RfYWxnIjogInNoYS0yNTYiLCAiaXNzIjogImh0dHA6Ly9leGFtcGxlLm9yZyIsICJzdWIiOiAidXNlciA0MiJ9.-BY5L0dcz2p-nCIhmL_0RK5QjzmKOI45E7anmZqg0Vct16lyF2_em3R8GzewmcrVT-NnZaT6EKrO79F4VGkFeQ~WyJ1eURGMUgwQVlSYmI0TVFubUx2dmVBIiwgImdpdmVuX25hbWUiLCAiSm9obiJd~WyJWam9zM2ZoOFZVU3Z2NHVYUVhuSWlRIiwgImZhbWlseV9uYW1lIiwgIkRvZSJd~WyIzV2JpbGZtNk1rdDFBUzlERXFtOS1nIiwgImVtYWlsIiwgImpvaG5kb2VAZXhhbXBsZS5jb20iXQ~", + "eyJhbGciOiAiRVMyNTYiLCAidHlwIjogInZjK3NkLWp3dCJ9.eyJfc2QiOiBbIlVaNG1MUV9MWUQ3QWc5RlpBSWMzX0Iyd0ZPR29DdlpBRGg1X2V4U3VHWUUiLCAibGM4bFZmT0ZMaXFSUU80T2lHeTFJWEh5SmpMT0dRSFY3c094eEQ0N1MySSIsICItYkIyZTVVdVZQZEltMzRWWVVpU2FfRHFJV1Ytc2tWSVphUl96MVNwYmFnIiwgInNkT0RjeXdEcVFVaVVnREV5UERyWFZaNTdkeEswNVhoZG5DWFhRRTlOX2ciXSwgIl9zZF9hbGciOiAic2hhLTI1NiIsICJpc3MiOiAiaHR0cDovL2V4YW1wbGUub3JnIiwgInN1YiI6ICJ1c2VyIDQyIn0.tZRB6X7fgASuHmPrhpIK_4veA6c2Ue0g3xGxIrPjHBsWXeROpAEmvMn593x5zkenuvRoO2r2wZNxnHJhqu0l0Q~WyJ0Tm9iM0NBTVhMcUVQWXRBWHBuVmdBIiwgInBob25lX251bWJlciIsICIrMS0yMDItNTU1LTAxMDEiXQ~WyJCbndnQThSODAtYUJtUVpuUzlrSnZnIiwgInBob25lX251bWJlcl92ZXJpZmllZCIsIHRydWVd~WyJhOVA3bENDZHVnNEZXWG95b21FWHZ3IiwgImFkZHJlc3MiLCB7ImNvdW50cnkiOiAiVVMiLCAibG9jYWxpdHkiOiAiQW55dG93biIsICJyZWdpb24iOiAiQW55c3RhdGUiLCAic3RyZWV0X2FkZHJlc3MiOiAiMTIzIE1haW4gU3QifV0~WyJJcURNYkUzeEtVOXAtOXlRTDVKN01BIiwgImJpcnRoZGF0ZSIsICIxOTQwLTAxLTAxIl0~" + ] +} +EOF + cat << EOF | zexe foreach_schema.zen foreach_schema.data +Scenario 'sd_jwt' : sd_jwt + +Given I have a 'signed_selective_disclosure array' named 'arr' + +Foreach 'x' in 'arr' +When I rename 'x' to 'res' +and I break the foreach +Endforeach + +Then print the 'res' +EOF + save_output foreach_schema.out + assert_output '{"res":"eyJhbGciOiAiRVMyNTYiLCAidHlwIjogInZjK3NkLWp3dCJ9.eyJfc2QiOiBbInZucGt1cWZBSWFBTlBmZXl6WXhUVllWUGxJY3JBWlVvU3N5TGFhQ0tlWUkiLCAiM1BEeUhOMXphcklJMG1TdDUwMkV2ZVIwVHhlOHlTQ1hDOFlYd1NvV1lZWSIsICJFMS12Wnl1Wmhlbkhlam1nYi1kTFhtaDBPODFLTUtWU25RYjN2Mjl6SGlRIl0sICJfc2RfYWxnIjogInNoYS0yNTYiLCAiaXNzIjogImh0dHA6Ly9leGFtcGxlLm9yZyIsICJzdWIiOiAidXNlciA0MiJ9.-BY5L0dcz2p-nCIhmL_0RK5QjzmKOI45E7anmZqg0Vct16lyF2_em3R8GzewmcrVT-NnZaT6EKrO79F4VGkFeQ~WyJ1eURGMUgwQVlSYmI0TVFubUx2dmVBIiwgImdpdmVuX25hbWUiLCAiSm9obiJd~WyJWam9zM2ZoOFZVU3Z2NHVYUVhuSWlRIiwgImZhbWlseV9uYW1lIiwgIkRvZSJd~WyIzV2JpbGZtNk1rdDFBUzlERXFtOS1nIiwgImVtYWlsIiwgImpvaG5kb2VAZXhhbXBsZS5jb20iXQ~"}' +}