Skip to content

Commit d0e1633

Browse files
committed
fix: Improved error handling
1 parent 7bb962b commit d0e1633

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

pkg/lib/traverse-get.sh

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# shellcheck shell=bash
22

33
bash_object.traverse-get() {
4-
REPLY=
4+
unset REPLY
55

66
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
77
stdtrace.log 0 ''
@@ -26,10 +26,17 @@ bash_object.traverse-get() {
2626
esac
2727
for ((i=0; i<${#REPLIES[@]}; i++)); do
2828
local key="${REPLIES[$i]}"
29+
local is_index_of_array='no'
30+
2931
filter_stack+=("$key")
3032

3133
bash_object.trace_loop
3234

35+
if [ "${key::1}" = $'\x1C' ]; then
36+
key="${key#?}"
37+
is_index_of_array='yes'
38+
fi
39+
3340
# If 'key' is not a member of object or index of array, error
3441
if [ -z "${current_object["$key"]+x}" ]; then
3542
echo "Error: Key '$key' is not in object '$current_object_name'"
@@ -54,9 +61,27 @@ bash_object.traverse-get() {
5461
local -n current_object="$current_object_name"
5562

5663
if ((i+1 < ${#REPLIES[@]})); then
57-
# Do nothing, we have already set 'current_object'
64+
# TODO: test these internal invalid errors
65+
# Do nothing (assuming the type is correct), we have already set 'current_object'
5866
# for the next iteration
59-
:
67+
case "$vmd_dtype" in
68+
object)
69+
if [ "$is_index_of_array" = yes ]; then
70+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "Expected object, but reference to array was found"
71+
return
72+
fi
73+
;;
74+
array)
75+
if [ "$is_index_of_array" = no ]; then
76+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "Expected array, but reference to object was found"
77+
return
78+
fi
79+
;;
80+
*)
81+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "vmd_dtype: $vmd_dtype"
82+
return
83+
;;
84+
esac
6085
elif ((i+1 == ${#REPLIES[@]})); then
6186
# We are last element of query, return the object
6287
if [ "$final_value_type" = object ]; then
@@ -80,6 +105,7 @@ bash_object.traverse-get() {
80105
return
81106
;;
82107
array)
108+
declare -ga REPLY=()
83109
local key=
84110
for key in "${!current_object[@]}"; do
85111
REPLY["$key"]="${current_object["$key"]}"
@@ -136,7 +162,7 @@ bash_object.traverse-get() {
136162

137163
bash_object.trace_current_object
138164
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
139-
stdtrace.log 1 "END BLOCK"
165+
stdtrace.log 0 "END BLOCK"
140166
fi
141167
fi
142168
done

tests/get-array.bats

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,41 @@ load './util/init.sh'
4545
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
4646
assert_line -p 'Queried for array, but found object'
4747
}
48+
49+
@test "correctly gets array" {
50+
declare -a SUB_ARRAY=(omicron pi rho)
51+
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_ARRAY')
52+
53+
bash_object.traverse-get array OBJECT '.my_key'
54+
55+
assert [ ${#REPLY[@]} -eq 3 ]
56+
assert [ "${REPLY[0]}" = omicron ]
57+
assert [ "${REPLY[1]}" = pi ]
58+
assert [ "${REPLY[2]}" = rho ]
59+
}
60+
61+
@test "correctly gets array in subobject" {
62+
declare -a SUB_SUB_ARRAY=(pi rho sigma)
63+
declare -A SUB_OBJECT=([subkey]=$'\x1C\x1Dtype=array;&SUB_SUB_ARRAY')
64+
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
65+
66+
bash_object.traverse-get array OBJECT '.my_key.subkey'
67+
68+
assert [ ${#REPLY[@]} -eq 3 ]
69+
assert [ "${REPLY[0]}" = pi ]
70+
assert [ "${REPLY[1]}" = rho ]
71+
assert [ "${REPLY[2]}" = sigma ]
72+
}
73+
74+
@test "correctly gets array in subarray" {
75+
declare -a SUB_SUB_ARRAY=(omicron pi rho)
76+
declare -a SUB_ARRAY=('foo' 'bar' $'\x1C\x1Dtype=array;&SUB_SUB_ARRAY')
77+
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_ARRAY')
78+
79+
bash_object.traverse-get array OBJECT '.["my_key"].[2]'
80+
81+
assert [ ${#REPLY[@]} -eq 3 ]
82+
assert [ "${REPLY[0]}" = omicron ]
83+
assert [ "${REPLY[1]}" = pi ]
84+
assert [ "${REPLY[2]}" = rho ]
85+
}

tests/get-object.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ load './util/init.sh'
4646
assert_line -p 'Queried for object, but found array'
4747
}
4848

49-
# # { "stars": { "cool": "Wolf 359" } }
49+
# { "stars": { "cool": "Wolf 359" } }
5050
@test "properly gets 4" {
5151
declare -A inner_object=([cool]='Wolf 359')
5252
declare -A OBJ=([stars]=$'\x1C\x1Dtype=object;&inner_object')
@@ -55,7 +55,7 @@ load './util/init.sh'
5555
assert [ "${REPLY[cool]}" = 'Wolf 359' ]
5656
}
5757

58-
# # { "stars": { "cool": "Wolf 359" } }
58+
# { "stars": { "cool": "Wolf 359" } }
5959
@test "properly gets 5" {
6060
declare -a inner_array=('Alpha Centauri A' 'Proxima Centauri')
6161
declare -A OBJ=([nearby]=$'\x1C\x1Dtype=object;&inner_array')

0 commit comments

Comments
 (0)