Skip to content

Commit fc8c244

Browse files
committed
fix: set-object with --value now properly works
1 parent 8eaad6b commit fc8c244

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

pkg/lib/traverse-set.sh

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,41 @@ bash_object.traverse-set() {
8181
return
8282
fi
8383
elif [ "$flag_pass_by_what" = 'by-value' ]; then
84-
if [ "$final_value_type" == object ]; then
85-
local -A temp_var_name="__bash_object_${RANDOM}_$RANDOM"
86-
local -n temp_var="$temp_var_name"
84+
if [ "$final_value_type" = object ]; then
85+
final_value="__bash_object_${RANDOM}_$RANDOM"
86+
local -A "$final_value"
87+
local -n final_value_ref="$final_value"
88+
final_value_ref=()
89+
8790
if [ "$1" != -- ]; then
8891
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "Must pass '--' and the value when using --value"
8992
return
9093
fi
94+
shift
95+
9196
if (( $# & 1 )); then
9297
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "When passing --value with set-object, an even number of values must be passed after the '--'"
9398
return
9499
fi
95-
for ((i=0; i<$#; i+2)); do
96-
temp_var["${!i}"]="${!i+1}"
97-
done
98-
elif [ "$final_value_type" == array ]; then
99-
local -a temp_var_name="__bash_object_${RANDOM}_$RANDOM"
100-
local -n temp_var="$temp_var_name"
100+
101+
while (( $# )); do
102+
local key="$1"
103+
if ! shift; then
104+
bash_object.util.die 'ERROR_INTERNAL' 'Shift failed, but was expected to succeed'
105+
return
106+
fi
107+
108+
local value="$1"
109+
if ! shift; then
110+
bash_object.util.die 'ERROR_INTERNAL' 'Shift failed, but was expected to succeed'
111+
return
112+
fi
113+
114+
final_value_ref["$key"]="$value"
115+
done; unset key value
116+
elif [ "$final_value_type" = array ]; then
117+
local -a final_value="__bash_object_${RANDOM}_$RANDOM"
118+
local -n final_value_ref="$final_value"
101119
if [ "$1" != -- ]; then
102120
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' "Must pass '--' and the value when using --value"
103121
return

tests/set-value.bats

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

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

5+
# object
6+
@test "Corretly get-object --value" {
7+
declare -A OBJECT=()
8+
9+
bobject set-object --value 'OBJECT' '.uwu' -- keyy valuee
10+
11+
bobject get-string --value 'OBJECT' '.uwu.keyy'
12+
assert [ "$REPLY" = 'valuee' ]
13+
}
14+
515
# array
616
@test "Correctly set-array --value" {
717
declare -A OBJECT=()

0 commit comments

Comments
 (0)