Skip to content

Commit

Permalink
feat(table): ✨ first implementation of pointer system in zencode
Browse files Browse the repository at this point in the history
More testing is needed before merging
  • Loading branch information
matteo-cristino authored and jaromil committed Dec 3, 2023
1 parent 6085d87 commit a72d1a6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/lua/zencode_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,39 @@ 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"
zencode_assert(obj_codec.zentype ~= "e", "Cannot set pointer to an element")
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")
zencode_assert(luatype(p[key_s]) == "table",
"Cannot enter a "..luatype(p[key_s])..", is not a table")
-- do not point to elements
zencode_assert(not p_codec.schema, "Cannot set pointer to an element")
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)
68 changes: 68 additions & 0 deletions test/zencode/table.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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":1.624277e+09,"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}}'
}

0 comments on commit a72d1a6

Please sign in to comment.