Skip to content

Commit c61f532

Browse files
committed
fix: Error when setting string on object
1 parent d1d2d65 commit c61f532

File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed

pkg/lib/cmd/bobject-print.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# shellcheck shell=bash
2+
3+
for f in "$BASH_OBJECT_LIB_DIR"/{,util/}?*.sh; do
4+
source "$f"
5+
done
6+
7+
bobject-print() {
8+
:
9+
}

pkg/lib/traverse.sh

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# shellcheck shell=bash
22

3-
43
bash_object.traverse() {
54
REPLY=
65
local flag_variable=
@@ -24,6 +23,8 @@ bash_object.traverse() {
2423
esac
2524
done
2625

26+
# TODO: errors if vmd_dtype, final_value_type is not one of the known ones
27+
2728
local action="$1"
2829
local final_value_type="$2"
2930
local root_object_name="$3"
@@ -201,22 +202,77 @@ bash_object.traverse() {
201202
# If 'key' is already a member of object, use it if it's a virtual object. If
202203
# it's not a virtual object, then a throw an error
203204
else
204-
local key_value="${current_object["$key"]}"
205+
if ((i+1 < ${#REPLIES[@]})); then
206+
local key_value="${current_object["$key"]}"
205207

206-
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
207-
virtual_item="${key_value#??}"
208+
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
209+
virtual_item="${key_value#??}"
208210

209-
bash_object.parse_virtual_object "$virtual_item"
210-
local current_object_name="$REPLY1"
211-
local vmd_dtype="$REPLY2"
211+
bash_object.parse_virtual_object "$virtual_item"
212+
local current_object_name="$REPLY1"
213+
local vmd_dtype="$REPLY2"
212214

213-
local -n current_object="$current_object_name"
215+
local -n current_object="$current_object_name"
214216

215-
# TODO: arrays, objects
216-
current_object["$key"]="$final_value"
217-
else
218-
# TODO: throw error
217+
# Get the next value (number, string), and construct the next
218+
# element accordingly
219+
case "$vmd_dtype" in
220+
object)
221+
;;
222+
array) ;;
223+
esac
224+
else
225+
# TODO: throw error
226+
echo "phi" >&3
227+
exit 1
228+
fi
219229
:
230+
elif ((i+1 == ${#REPLIES[@]})); then
231+
local key_value="${current_object["$key"]}"
232+
233+
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
234+
virtual_item="${key_value#??}"
235+
236+
bash_object.parse_virtual_object "$virtual_item"
237+
local current_object_name="$REPLY1"
238+
local vmd_dtype="$REPLY2"
239+
240+
local -n current_object="$current_object_name"
241+
242+
if [ "$final_value_type" = object ]; then
243+
case "$vmd_dtype" in
244+
object)
245+
246+
;;
247+
array)
248+
;;
249+
esac
250+
elif [ "$final_value_type" = array ]; then
251+
case "$vmd_dtype" in
252+
object)
253+
;;
254+
array)
255+
;;
256+
esac
257+
elif [ "$final_value_type" = string ]; then
258+
case "$vmd_dtype" in
259+
object)
260+
# TODO: test this
261+
echo "Error: Cannot set string on object"
262+
exit 1
263+
;;
264+
array)
265+
echo "Error: Cannot set string on array"
266+
exit 1
267+
;;
268+
esac
269+
fi
270+
current_object["$key"]="$final_value"
271+
else
272+
# TODO: throw error
273+
echo "omicron" >&3
274+
exit 1
275+
fi
220276
fi
221277
fi
222278

tests/set-string.bats

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,45 @@
22

33
load './util/init.sh'
44

5+
@test "errors if setting string on existing object" {
6+
declare -A SUB_OBJECT=([nested]=woof)
7+
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
8+
9+
run bash_object.traverse set string 'OBJECT' '.my_key' 'my_value'
10+
11+
assert_failure
12+
assert_output -p "Cannot set string on object"
13+
}
14+
15+
@test "errors if setting string on existing object 1" {
16+
declare -A SUB_OBJECT=([nested]=woof)
17+
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
18+
19+
run bash_object.traverse set string 'OBJECT' '.my_key' 'my_value'
20+
21+
assert_failure
22+
assert_output -p "Cannot set string on object"
23+
}
24+
525
@test "properly sets 1" {
6-
declare -A OBJ=()
26+
declare -A OBJECT=()
727

8-
bash_object.traverse set string 'OBJ' '.my_key' 'my_value'
9-
assert [ "${OBJ[my_key]}" = 'my_value' ]
28+
bash_object.traverse set string 'OBJECT' '.my_key' 'my_value'
29+
assert [ "${OBJECT[my_key]}" = 'my_value' ]
1030

11-
bash_object.traverse get string 'OBJ' '.my_key'
31+
bash_object.traverse get string 'OBJECT' '.my_key'
1232
assert [ "$REPLY" = 'my_value' ]
1333
}
1434

1535
@test "properly sets 2" {
16-
declare -A OBJ=()
36+
declare -A OBJECT=()
1737

18-
bash_object.traverse set string 'OBJ' '.xray.yankee.zulu' 'boson'
19-
bash_object.traverse set string 'OBJ' '.xray.yankee.alfa' 'lithography'
38+
bash_object.traverse set string 'OBJECT' '.xray.yankee.zulu' 'boson'
39+
bash_object.traverse set string 'OBJECT' '.xray.yankee.alfa' 'lithography'
2040

21-
bash_object.traverse get string 'OBJ' '.xray.yankee.zulu'
41+
bash_object.traverse get string 'OBJECT' '.xray.yankee.zulu'
2242
assert [ "$REPLY" = 'boson' ]
2343

24-
bash_object.traverse get string 'OBJ' '.xray.yankee.alfa'
44+
bash_object.traverse get string 'OBJECT' '.xray.yankee.alfa'
2545
assert [ "$REPLY" = 'lithography' ]
2646
}

0 commit comments

Comments
 (0)