Skip to content

Commit bf4d1d5

Browse files
committed
fix: Properly check for array existence
1 parent a5c3cc1 commit bf4d1d5

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

pkg/lib/traverse-set.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ bash_object.traverse-set() {
4444
if [ -z "${current_object["$key"]+x}" ]; then
4545
# If we are before the last element in the query, then error
4646
if ((i+1 < ${#REPLIES[@]})); then
47-
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "Key or index '$key' is not in '$filter_stack_string'"
48-
return
47+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "Key or index '$key' is not in '$filter_stack_string'"
48+
return
4949
# If we are at the last element in the query
5050
elif ((i+1 == ${#REPLIES[@]})); then
5151
if [ "$final_value_type" = object ]; then
5252
# TODO: test this
53-
if bash_object.ensure.variable_does_exist "$final_value"; then :; else
53+
# shellcheck disable=SC1087
54+
if bash_object.ensure.variable_does_exist "$final_value[@]"; then :; else
5455
return
5556
fi
5657

@@ -83,7 +84,8 @@ bash_object.traverse-set() {
8384
done
8485
elif [ "$final_value_type" = array ]; then
8586
# TODO: test this
86-
if bash_object.ensure.variable_does_exist "$final_value"; then :; else
87+
# shellcheck disable=SC1087
88+
if bash_object.ensure.variable_does_exist "$final_value[@]"; then :; else
8789
return
8890
fi
8991

pkg/lib/util/ensure.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ bash_object.ensure.variable_is_valid() {
55
return 0
66
}
77

8-
# TODO: swap with does_not
8+
# @description Ensure the variable already exists. Note that the variable _must_ be sanitized before using this function
99
bash_object.ensure.variable_does_exist() {
10-
if bash_object.ensure.variable_does_not_exist "$1"; then
11-
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' "Variable '$1' does not exist, but it should"
10+
local variable_name="$1"
11+
12+
if [ -z "$variable_name" ]; then
13+
bash_object.util.die "ERROR_INTERNAL_MISCELLANEOUS" "Parameter to function 'bash_object.ensure.variable_does_exist' was empty"
1214
return
15+
fi
16+
17+
if ((BASH_VERSINFO[0] >= 5)) || ((BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 2)); then
18+
if [[ ! -v "$variable_name" ]]; then
19+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' "Variable '$variable_name' does not exist, but it should"
20+
return
21+
fi
1322
else
23+
# TODO
1424
:
1525
fi
1626
}

pkg/lib/util/util.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ bash_object.util.die() {
1919
local error_output=
2020
case "$error_key" in
2121
ERROR_INVALID_FILTER)
22-
printf -v error_output 'Failed to perform operation:
22+
printf -v error_output 'Failed to parse filter:
2323
-> code: %s
2424
-> message: %s
2525
-> context: %s
26-
-> PARSER_COLUMN_NUMBER: %s' "$error_key" "$error_message" "$error_context" "$PARSER_COLUMN_NUMBER"
26+
-> PARSER_COLUMN_NUMBER: %s
27+
' "$error_key" "$error_message" "$error_context" "$PARSER_COLUMN_NUMBER"
2728
;;
2829
*)
29-
printf -v error_output 'Failed to parse filter:
30+
printf -v error_output 'Failed to perform operation:
3031
-> code: %s
3132
-> message: %s
32-
-> context: %s' "$error_key" "$error_message" "$error_context"
33+
-> context: %s
34+
' "$error_key" "$error_message" "$error_context"
3335
;;
3436
esac
3537

0 commit comments

Comments
 (0)