Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): ✨ first implementation of pointer system in zencode #752

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ tests = [
'generic_ecdh', 'generic_eddsa', 'generic_schnorr', 'generic_es256',
'given', 'hash', 'http', 'keys', 'kyber', 'ntrup', 'numbers',
'output', 'pack', 'parser', 'planetmint', 'pvss', 'random', 'reflow',
'rules', 'secshare', 'then', 'w3c', 'w3c_did', 'when', 'zencode_exec',
'zenswarm', 'zkp', 'zkp_multi_petitions', 'logfmt', 'scope',
'fsp'
'rules', 'secshare', 'table', 'then', 'w3c', 'w3c_did', 'when',
'zencode_exec', 'zenswarm', 'zkp', 'zkp_multi_petitions', 'logfmt',
'scope', 'fsp'
]
foreach test_suite : tests
test('zencode_'+test_suite.underscorify(),
Expand All @@ -224,7 +224,7 @@ benches = [ 'and', 'array', 'bbs_sha', 'bbs_shake', 'bbs_zkp',
'foreach', 'generic_bbs', 'generic_dilithium', 'generic_ecdh',
'generic_eddsa', 'generic_schnorr', 'given', 'hash', 'kyber', 'ntrup',
'numbers', 'output', 'pack', 'parser', 'planetmint', 'pvss', 'random',
'reflow', 'secshare', 'then', 'w3c', 'w3c_did', 'when',
'reflow', 'secshare', 'table', 'then', 'w3c', 'w3c_did', 'when',
'zencode_exec', 'zenswarm', 'zkp', 'zkp_multi_petitions',
'fsp' ]

Expand Down
31 changes: 31 additions & 0 deletions src/lua/zencode_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,34 @@ When("create copy of last element from ''", function(obj_name)
end
new_codec('copy_of_last_element', n_codec)
end)

When("set pointer to ''", function(obj_name)
local obj, obj_codec = have(obj_name)
empty "pointer"
ACK.pointer = obj
new_codec("pointer", nil, obj_name)
end)

When("enter '' with pointer", function(key_name)
local key_o, key_codec = mayhave(key_name)
local p, p_codec = have("pointer")
local key_s
if key_o then
key_s = get_encoding_function(key_codec.encoding)(key_o)
else
key_s = key_name
end
if p_codec.zentype == "a" then
key_s = tonumber(key_s)
zencode_assert(key_s, "Pointer is an array but key is not a position number: "..key_name)
end
zencode_assert(p[key_s], "Cannot find "..key_s.." in pointer")
ACK.pointer = p[key_s]
local n_codec = {encoding = p_codec.encoding}
if p_codec.schema then
n_codec.schema = p_codec.schema
n_codec.zentype = "e"
end
CODEC.pointer = nil
new_codec("pointer", n_codec)
end)
107 changes: 107 additions & 0 deletions test/zencode/table.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
load ../bats_setup
load ../bats_zencode
SUBDOC=table


@test "move with pointer" {
cat <<EOF | save_asset pointer.data
{
"dataFromEndpoint": {
"cod": "200",
"count": 1,
"list": [
{
"clouds": {
"all": 20
},
"coord": {
"lat": 43.7667,
"lon": 11.25
},
"dt": 1624277371,
"id": 3176959,
"main": {
"feels_like": 305.32,
"humidity": 48,
"pressure": 1010,
"temp": 304.11,
"temp_max": 304.82,
"temp_min": 303.15
},
"name": "Florence",
"sys": {
"country": "IT"
},
"weather": [
{
"description": "few clouds",
"icon": "02d",
"id": 801,
"main": "Clouds"
}
],
"wind": {
"deg": 0,
"speed": 2.06
} }
],
"message": "accurate"
},
"key_inside_list": "main"
}
EOF
cat <<EOF | zexe pointer.zen pointer.data
Given I have a 'string dictionary' named 'dataFromEndpoint'
and I have a 'string' named 'key_inside_list'

When I set pointer to 'dataFromEndpoint'
and I enter 'list' with pointer
and I enter '1' with pointer
and I enter 'key_inside_list' with pointer
and I move 'key_inside_list' in 'pointer'

Then print 'pointer'
and print 'dataFromEndpoint'
EOF
save_output 'pointer.out'
assert_output '{"dataFromEndpoint":{"cod":"200","count":1,"list":[{"clouds":{"all":20},"coord":{"lat":43.7667,"lon":11.25},"dt":1624277371,"id":3176959.0,"main":{"feels_like":305.32,"humidity":48,"key_inside_list":"main","pressure":1010,"temp":304.11,"temp_max":304.82,"temp_min":303.15},"name":"Florence","sys":{"country":"IT"},"weather":[{"description":"few clouds","icon":"02d","id":801,"main":"Clouds"}],"wind":{"deg":0,"speed":2.06}}],"message":"accurate"},"pointer":{"feels_like":305.32,"humidity":48,"key_inside_list":"main","pressure":1010,"temp":304.11,"temp_max":304.82,"temp_min":303.15}}'
}

@test "pointer fails" {
cat <<EOF | save_asset pointer_fails.data
{
"dict": {
"list": [
"hello",
"world"
]
},
"not an array key": "not a number",
"non existing key": "not a dict key"
}
EOF
cat <<EOF | save_asset pointer_fails_array.zen
Given I have a 'string dictionary' named 'dict'
and I have a 'string' named 'not an array key'

When I set pointer to 'dict'
and I enter 'list' with pointer
and I enter 'not an array key' with pointer

Then print 'pointer'
EOF
run $ZENROOM_EXECUTABLE -z -a pointer_fails.data pointer_fails_array.zen
assert_line --partial 'Pointer is an array but key is not a position number: not_an_array_key'

cat <<EOF | save_asset pointer_fails_dict.zen
Given I have a 'string dictionary' named 'dict'
and I have a 'string' named 'non existing key'

When I set pointer to 'dict'
and I enter 'non existing key' with pointer

Then print 'pointer'
EOF
run $ZENROOM_EXECUTABLE -z -a pointer_fails.data pointer_fails_dict.zen
assert_line --partial 'Cannot find not a dict key in pointer'
}
Loading