Skip to content

Commit 0858db2

Browse files
committed
fix: Remove irrelevant TODOs and cleanup traverse functions
1 parent f7db673 commit 0858db2

File tree

6 files changed

+151
-123
lines changed

6 files changed

+151
-123
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ bpm install
5555
```
5656

5757
## TODO
58-
- ensure error (for set primarily) if the virtual object references a variable that does not exist
5958
- error if set in array out of bounds?
59+
- clean up argument parsing?
60+
- do something with 'is_index_of_array?' (do not allow for using index notation to get keys of objects)
61+
- set element of the same type that already exists (--overwrite?)
62+
- filter => query

pkg/lib/traverse-get.sh

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ bash_object.traverse-get() {
8989
fi
9090

9191
filter_stack+=("$key")
92+
# bash_object.util.generate_filter_stack_string
93+
# local filter_stack_string="$REPLY"
9294

9395
bash_object.trace_loop
9496

@@ -110,86 +112,57 @@ bash_object.traverse-get() {
110112

111113
virtual_item="${key_value#??}"
112114
bash_object.parse_virtual_object "$virtual_item"
115+
# shellcheck disable=SC2153
113116
local current_object_name="$REPLY1"
117+
# shellcheck disable=SC2153
114118
local vmd_dtype="$REPLY2"
115119
local -n current_object="$current_object_name"
116120

117-
if ((i+1 < ${#REPLIES[@]})); then
118-
# echo aa >&3
119-
# if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
120-
# echo jj >&3
121-
# fi
122-
123-
# TODO: test these internal invalid errors (error when type=array references object, etc.)
124-
# Do nothing (assuming the type is correct), we have already set 'current_object'
125-
# for the next iteration
126-
:
127-
# case "$vmd_dtype" in
128-
# object)
129-
# if [ "$is_index_of_array" = yes ]; then
130-
# bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Expected object, but reference to array was found"
131-
# return
132-
# fi
133-
# ;;
134-
# array)
135-
# if [ "$is_index_of_array" = no ]; then
136-
# bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Expected array, but reference to object was found"
137-
# return
138-
# fi
139-
# ;;
140-
# *)
141-
# bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Unexpected vmd_dtype '$vmd_dtype'"
142-
# return
143-
# ;;
144-
# esac
145-
146-
# Ensure no circular references (WET)
147-
if [ "$old_current_object_name" = "$current_object_name" ]; then
148-
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
121+
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
122+
# Ensure the 'final_value' is the same type as specified by the user (WET)
123+
local current_object_type=
124+
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
125+
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
149126
return
150127
fi
151-
elif ((i+1 == ${#REPLIES[@]})); then
152-
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
153-
# Ensure the 'final_value' is the same type as specified by the user
154-
local current_object_type=
155-
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
156-
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
128+
current_object_type="${current_object_type#declare -}"
129+
case "${current_object_type::1}" in
130+
A) current_object_type='object' ;;
131+
a) current_object_type='array' ;;
132+
-) current_object_type='string' ;;
133+
*) current_object_type='other' ;;
134+
esac
135+
case "$vmd_dtype" in
136+
object)
137+
if [ "$current_object_type" != object ]; then
138+
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
157139
return
158140
fi
159-
current_object_type="${current_object_type#declare -}"
160-
case "${current_object_type::1}" in
161-
A) current_object_type='object' ;;
162-
a) current_object_type='array' ;;
163-
-) current_object_type='string' ;;
164-
*) current_object_type='other' ;;
165-
esac
166-
167-
case "$vmd_dtype" in
168-
object)
169-
if [ "$current_object_type" != object ]; then
170-
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
171-
return
172-
fi
173-
;;
174-
array)
175-
if [ "$current_object_type" != array ]; then
176-
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
177-
return
178-
fi
179-
;;
180-
*)
181-
bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Unexpected vmd_dtype '$vmd_dtype'"
141+
;;
142+
array)
143+
if [ "$current_object_type" != array ]; then
144+
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
182145
return
183-
;;
184-
esac
185-
fi
186-
187-
# Ensure no circular references (WET)
188-
if [ "$old_current_object_name" = "$current_object_name" ]; then
189-
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
146+
fi
147+
;;
148+
*)
149+
bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Unexpected vmd_dtype '$vmd_dtype'"
190150
return
191-
fi
151+
;;
152+
esac
153+
fi
154+
155+
# Ensure no circular references (WET)
156+
if [ "$old_current_object_name" = "$current_object_name" ]; then
157+
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
158+
return
159+
fi
192160

161+
if ((i+1 < ${#REPLIES[@]})); then
162+
# Do nothing, and continue to next element in query. We already check for the
163+
# validity of the virtual object above, so no need to do anything here
164+
:
165+
elif ((i+1 == ${#REPLIES[@]})); then
193166
# We are last element of query, return the object
194167
if [ "$final_value_type" = object ]; then
195168
case "$vmd_dtype" in
@@ -251,11 +224,8 @@ bash_object.traverse-get() {
251224
fi
252225

253226
if ((i+1 < ${#REPLIES[@]})); then
254-
# Means the query is one level deeper than expected. Expected
255-
# object/array, but got string
256-
# TODO
257-
echo "mu '$key_value'" >&3
258-
# return 2
227+
bash_object.util.die 'ERROR_NOT_FOUND' "The passed filter implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
228+
return
259229
elif ((i+1 == ${#REPLIES[@]})); then
260230
local value="${current_object["$key"]}"
261231
if [ "$final_value_type" = object ]; then

pkg/lib/traverse-set.sh

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bash_object.traverse-set() {
4242
fi
4343

4444
local final_value_type root_object_name filter final_value
45-
# `set -u` compat
45+
# for compat with 'set -u'
4646
if [ -n "${args[0]+x}" ]; then
4747
final_value_type="${args[0]}"
4848
fi
@@ -173,12 +173,6 @@ bash_object.traverse-set() {
173173
# If we are at the last element in the query, and it doesn't exist, create it
174174
elif ((i+1 == ${#REPLIES[@]})); then
175175
if [ "$final_value_type" = object ]; then
176-
# TODO: test this
177-
# shellcheck disable=SC1087
178-
if bash_object.ensure.variable_does_exist "$final_value"; then :; else
179-
return
180-
fi
181-
182176
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
183177
local global_object_name="$REPLY"
184178

@@ -193,22 +187,14 @@ bash_object.traverse-set() {
193187

194188
current_object["$key"]=$'\x1C\x1D'"type=object;&$global_object_name"
195189

196-
local -n globel_object="$global_object_name"
190+
local -n global_object="$global_object_name"
197191
local -n object_to_copy_from="$final_value"
198192

199-
# TODO: test if object_to_copy is of the correct type
200-
201193
for key in "${!object_to_copy_from[@]}"; do
202194
# shellcheck disable=SC2034
203-
globel_object["$key"]="${object_to_copy_from["$key"]}"
195+
global_object["$key"]="${object_to_copy_from["$key"]}"
204196
done
205197
elif [ "$final_value_type" = array ]; then
206-
# TODO: test this
207-
# shellcheck disable=SC1087
208-
if bash_object.ensure.variable_does_exist "$final_value"; then :; else
209-
return
210-
fi
211-
212198
bash_object.util.generate_vobject_name "$root_object_name" "$filter_stack_string"
213199
local global_array_name="$REPLY"
214200

@@ -226,14 +212,10 @@ bash_object.traverse-set() {
226212
local -n global_array="$global_array_name"
227213
local -n array_to_copy_from="$final_value"
228214

229-
# TODO: test if object_to_copy is of the correct type
230-
231215
# shellcheck disable=SC2034
232216
global_array=("${array_to_copy_from[@]}")
233217
elif [ "$final_value_type" = string ]; then
234-
# TODO: ensure correct type
235-
local -n string_to_copy_from="$final_value"
236-
current_object["$key"]="$string_to_copy_from"
218+
current_object["$key"]="${!final_value}"
237219
else
238220
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "Unexpected final_value_type '$final_value_type'"
239221
return
@@ -255,24 +237,54 @@ bash_object.traverse-set() {
255237
bash_object.parse_virtual_object "$virtual_item"
256238
local current_object_name="$REPLY1"
257239
local vmd_dtype="$REPLY2"
240+
# shellcheck disable=SC2178
258241
local -n current_object="$current_object_name"
259242

260-
if ((i+1 < ${#REPLIES[@]})); then
261-
# TODO: test these internal invalid errors (error when type=array references object, etc.)?
262-
:
243+
# Ensure no circular references (WET)
244+
if [ "$old_current_object_name" = "$current_object_name" ]; then
245+
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
246+
return
247+
fi
263248

264-
# Ensure no circular references (WET)
265-
if [ "$old_current_object_name" = "$current_object_name" ]; then
266-
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
249+
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
250+
# Ensure the 'final_value' is the same type as specified by the user (WET)
251+
local current_object_type=
252+
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
253+
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
267254
return
268255
fi
269-
elif ((i+1 == ${#REPLIES[@]})); then
270-
# Ensure no circular references (WET)
271-
if [ "$old_current_object_name" = "$current_object_name" ]; then
272-
bash_object.util.die 'ERROR_SELF_REFERENCE' "Virtual object '$current_object_name' cannot reference itself"
256+
current_object_type="${current_object_type#declare -}"
257+
case "${current_object_type::1}" in
258+
A) current_object_type='object' ;;
259+
a) current_object_type='array' ;;
260+
-) current_object_type='string' ;;
261+
*) current_object_type='other' ;;
262+
esac
263+
case "$vmd_dtype" in
264+
object)
265+
if [ "$current_object_type" != object ]; then
266+
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
267+
return
268+
fi
269+
;;
270+
array)
271+
if [ "$current_object_type" != array ]; then
272+
bash_object.util.die 'ERROR_VOBJ_INCORRECT_TYPE' "Virtual object has a reference of type '$vmd_dtype', but when dereferencing, a variable of type '$current_object_type' was found"
273+
return
274+
fi
275+
;;
276+
*)
277+
bash_object.util.die 'ERROR_VOBJ_INVALID_TYPE' "Unexpected vmd_dtype '$vmd_dtype'"
273278
return
274-
fi
279+
;;
280+
esac
281+
fi
275282

283+
if ((i+1 < ${#REPLIES[@]})); then
284+
# Do nothing, and continue to next element in query. We already check for the
285+
# validity of the virtual object above, so no need to do anything here
286+
:
287+
elif ((i+1 == ${#REPLIES[@]})); then
276288
# We are last element of query, but do not set the object there is one that already exists
277289
if [ "$final_value_type" = object ]; then
278290
case "$vmd_dtype" in
@@ -325,8 +337,7 @@ bash_object.traverse-set() {
325337
fi
326338

327339
if ((i+1 < ${#REPLIES[@]})); then
328-
# TODO error message
329-
bash_object.util.die 'ERROR_NOT_FOUND' "Encountered string using accessor '$key', but expected to find either an object or array, in accordance with the filter"
340+
bash_object.util.die 'ERROR_NOT_FOUND' "The passed filter implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
330341
return
331342
elif ((i+1 == ${#REPLIES[@]})); then
332343
if [ "$final_value_type" = object ]; then

tests/get-errors.bats

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
load './util/init.sh'

tests/set-errors-param.bats

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ load './util/init.sh'
170170
assert_line -p "The variable 'str' does not exist"
171171
}
172172

173-
@test "Error if final_value_type is 'object', but is really 'array'" {
173+
@test "Error if final_value_type is 'object', but is actually 'array'" {
174174
export VERIFY_BASH_OBJECT=
175175
declare -A OBJECT=()
176176
declare -a obj=()
@@ -182,7 +182,7 @@ load './util/init.sh'
182182
assert_line -p ", but a variable with type 'array' was passed"
183183
}
184184

185-
@test "Error if final_value_type is 'object', but is really 'string'" {
185+
@test "Error if final_value_type is 'object', but is actually 'string'" {
186186
export VERIFY_BASH_OBJECT=
187187
declare -A OBJECT=()
188188
declare obj=
@@ -194,7 +194,7 @@ load './util/init.sh'
194194
assert_line -p ", but a variable with type 'string' was passed"
195195
}
196196

197-
@test "Error if final_value_type is 'object', but is really 'other'" {
197+
@test "Error if final_value_type is 'object', but is actually 'other'" {
198198
export VERIFY_BASH_OBJECT=
199199
declare -A OBJECT=()
200200
declare -i obj=
@@ -218,7 +218,7 @@ load './util/init.sh'
218218
assert_line -p "The variable 'str' does not exist"
219219
}
220220

221-
@test "Error if final_value_type is 'array', but is really 'object'" {
221+
@test "Error if final_value_type is 'array', but is actually 'object'" {
222222
export VERIFY_BASH_OBJECT=
223223
declare -A OBJECT=()
224224
declare -A obj=()
@@ -230,7 +230,7 @@ load './util/init.sh'
230230
assert_line -p ", but a variable with type 'object' was passed"
231231
}
232232

233-
@test "Error if final_value_type is 'array', but is really 'string'" {
233+
@test "Error if final_value_type is 'array', but is actually 'string'" {
234234
export VERIFY_BASH_OBJECT=
235235
declare -A OBJECT=()
236236
declare obj=
@@ -242,7 +242,7 @@ load './util/init.sh'
242242
assert_line -p ", but a variable with type 'string' was passed"
243243
}
244244

245-
@test "Error if final_value_type is 'array', but is really 'other'" {
245+
@test "Error if final_value_type is 'array', but is actually 'other'" {
246246
export VERIFY_BASH_OBJECT=
247247
declare -A OBJECT=()
248248
declare -i obj=
@@ -266,7 +266,7 @@ load './util/init.sh'
266266
assert_line -p "The variable 'str' does not exist"
267267
}
268268

269-
@test "Error if final_value_type is 'string', but is really 'object'" {
269+
@test "Error if final_value_type is 'string', but is actually 'object'" {
270270
export VERIFY_BASH_OBJECT=
271271
declare -A OBJECT=()
272272
declare -A obj=()
@@ -278,7 +278,7 @@ load './util/init.sh'
278278
assert_line -p ", but a variable with type 'object' was passed"
279279
}
280280

281-
@test "Error if final_value_type is 'string', but is really 'array'" {
281+
@test "Error if final_value_type is 'string', but is actually 'array'" {
282282
export VERIFY_BASH_OBJECT=
283283
declare -A OBJECT=()
284284
declare -a obj=()
@@ -290,7 +290,7 @@ load './util/init.sh'
290290
assert_line -p ", but a variable with type 'array' was passed"
291291
}
292292

293-
@test "Error if final_value_type is 'string', but is really 'other'" {
293+
@test "Error if final_value_type is 'string', but is actually 'other'" {
294294
export VERIFY_BASH_OBJECT=
295295
declare -A OBJECT=()
296296
declare -i obj=
@@ -304,7 +304,7 @@ load './util/init.sh'
304304

305305
# Data validation should not be done if the keys to set are
306306
# being passed as value
307-
@test "Do not error if final_value_type is 'string', but is really 'array' on --value" {
307+
@test "Do not error if final_value_type is 'string', but is actually 'array' on --value" {
308308
export VERIFY_BASH_OBJECT=
309309
declare -A OBJECT=()
310310
declare -a obj=()

0 commit comments

Comments
 (0)