Skip to content

Commit a5c3cc1

Browse files
committed
fix: Properly error if key is not a member of object or index of array
1 parent 8a7c51a commit a5c3cc1

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ assert [ "$REPLY" = rho ]
5050
STATUS: IN DEVELOPMENT!
5151

5252
```sh
53-
echo "dependencies [ 'hyperupcall/bash-object' ]" > 'bpm.toml'
53+
echo "dependencies = [ 'hyperupcall/bash-object' ]" > 'bpm.toml'
5454
bpm install
5555
```
5656

5757
## TODO
58-
- do not auto create tree with set (or add -p flag that does so)
59-
- workshop set-* functions
60-
- add support for set-object and set-array
6158
- error on invalid references (`type=object` in virtual object metadata, when it is referencing an array)
6259
- add tests for array in array (like object in object)
6360
- ensure error (for set primarily) if the virtual object references a variable that does not exist

pkg/lib/traverse-get.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ 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-
3129
filter_stack+=("$key")
3230

31+
local oldIFS="$IFS"
32+
IFS='_'
33+
local filter_stack_string="${filter_stack[*]}"
34+
IFS="$oldIFS"
35+
3336
bash_object.trace_loop
3437

38+
local is_index_of_array='no'
3539
if [ "${key::1}" = $'\x1C' ]; then
3640
key="${key#?}"
3741
is_index_of_array='yes'
3842
fi
3943

4044
# If 'key' is not a member of object or index of array, error
4145
if [ -z "${current_object["$key"]+x}" ]; then
42-
echo "Error: Key '$key' is not in object '$current_object_name'"
43-
return 1
46+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "Key or index '$key' is not in '$filter_stack_string'"
47+
return
4448
# If 'key' is a member of an object or index of array
4549
else
4650
local key_value="${current_object["$key"]}"

pkg/lib/traverse-set.sh

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,19 @@ bash_object.traverse-set() {
3333
local key="${REPLIES[$i]}"
3434
filter_stack+=("$key")
3535

36+
local oldIFS="$IFS"
37+
IFS='_'
38+
local filter_stack_string="${filter_stack[*]}"
39+
IFS="$oldIFS"
40+
3641
bash_object.trace_loop
3742

3843
# If 'key' is not a member of object or index of array, error
3944
if [ -z "${current_object["$key"]+x}" ]; then
4045
# If we are before the last element in the query, then error
4146
if ((i+1 < ${#REPLIES[@]})); then
42-
echo "could not traverse property does not exist"
43-
return 2
44-
# # The variable is 'new_current_object_name', but it also could
45-
# # be the name of a new _array_
46-
# local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
47-
48-
# if ! eval "declare -gA $new_current_object_name=()"; then
49-
# printf '%s\n' 'Error: bash-object: eval declare failed'
50-
# return 1
51-
# fi
52-
53-
# current_object["$key"]=$'\x1C\x1D'"type=object;&$new_current_object_name"
54-
55-
# current_object_name="$new_current_object_name"
56-
# # shellcheck disable=SC2178
57-
# local -n current_object="$new_current_object_name"
47+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "Key or index '$key' is not in '$filter_stack_string'"
48+
return
5849
# If we are at the last element in the query
5950
elif ((i+1 == ${#REPLIES[@]})); then
6051
if [ "$final_value_type" = object ]; then
@@ -63,11 +54,6 @@ bash_object.traverse-set() {
6354
return
6455
fi
6556

66-
local oldIFS="$IFS"
67-
IFS='_'
68-
local filter_stack_string="${filter_stack[*]}"
69-
IFS="$oldIFS"
70-
7157
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
7258
local global_object_name="$REPLY"
7359

@@ -101,11 +87,6 @@ bash_object.traverse-set() {
10187
return
10288
fi
10389

104-
local oldIFS="$IFS"
105-
IFS='_'
106-
local filter_stack_string="${filter_stack[*]}"
107-
IFS="$oldIFS"
108-
10990
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
11091
local global_array_name="$REPLY"
11192

0 commit comments

Comments
 (0)