Skip to content

Commit 55e4635

Browse files
committed
fix: Check if variable is already set before creating another one
1 parent 4e83023 commit 55e4635

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

pkg/lib/traverse-set.sh

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ bash_object.traverse-set() {
66
stdtrace.log 0 "CALL: bash_object.traverse-set: $*"
77
fi
88

9-
# TODO: errors if vmd_dtype, final_value_type is not one of the known ones
10-
119
local final_value_type="$1"
1210
local root_object_name="$2"
1311
local filter="$3"
@@ -41,7 +39,6 @@ bash_object.traverse-set() {
4139
# # be the name of a new _array_
4240
# local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
4341

44-
# # TODO: double-check if new_current_object_name only has underscores, dots, etc. (printf %q?)
4542
# if ! eval "declare -gA $new_current_object_name=()"; then
4643
# printf '%s\n' 'Error: bash-object: eval declare failed'
4744
# return 1
@@ -55,34 +52,43 @@ bash_object.traverse-set() {
5552
# If we are at the last element in the query
5653
elif ((i+1 == ${#REPLIES[@]})); then
5754
if [ "$final_value_type" = object ]; then
58-
# TODO: late bash srandom
59-
local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
60-
61-
# TODO: test if name is already used
55+
bash_object.util.generate_vobject_name
56+
local new_current_object_name="$REPLY"
6257

6358
# TODO: double-check if new_current_object_name only has underscores, dots, etc. (printf %q?)
6459

65-
# 1. Create object
60+
if ((BASH_VERSINFO[0] >= 5)) || ((BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 2)); then
61+
if [[ -v "$new_current_object_name" ]]; then
62+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' "Autogenerated variable '$new_current_object_name' already exists. Did RANDOM or SRANDOM get unset?"
63+
return
64+
fi
65+
else
66+
if ! eval "
67+
if ! [ -z \${$new_current_object_name+x} ]; then
68+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' \"Autogenerated variable '$new_current_object_name' already exists. Did RANDOM or SRANDOM get unset?\"
69+
return
70+
fi
71+
"; then
72+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' 'Eval unset test'
73+
return
74+
fi
75+
fi
76+
6677
if ! eval "declare -gA $new_current_object_name=()"; then
67-
# TODO: error
68-
printf '%s\n' 'Error: bash-object: eval declare failed'
69-
return 1
78+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' 'Eval declare failed'
79+
return
7080
fi
7181

72-
# 2. Create virtual object
82+
local -n new_current_object="$new_current_object_name"
83+
7384
current_object["$key"]=$'\x1C\x1D'"type=object;&$new_current_object_name"
7485

75-
local -n new_current_object="$new_current_object_name"
7686
local -n object_to_copy="$final_value"
7787
# test if the object_to_copy is of the right type
7888
for key in "${!object_to_copy[@]}"; do
7989
new_current_object["$key"]="${object_to_copy["$key"]}"
8090
done
8191
elif [ "$final_value_type" = array ]; then
82-
# local new_current_object_name="__bash_object_${root_object_name}_tree_${key}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
83-
# current_object["$key"]=$'\x1C\x1D'"type=array;&$new_current_object_name"
84-
85-
# local -n new_array = "$final_value"
8692
:
8793
# current_object["$key"]=("${new_array[@]}")
8894
elif [ "$final_value_type" = string ]; then

pkg/lib/util/util.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare -gA ERRORS_BASH_OBJECT=(
77
[ERROR_INVALID_ARGS]='Invalid arguments'
88
[ERROR_INTERNAL_INVALID_VOBJ]='Internal virtual object has incorrect metadata'
99
[ERROR_INTERNAL_INVALID_PARAM]='Internal parameter has an incorrect value'
10-
10+
[ERROR_INTERNAL_MISCELLANEOUS]='Miscellaneous error occured'
1111
)
1212

1313
bash_object.util.die() {
@@ -37,6 +37,19 @@ bash_object.util.die() {
3737
return 2
3838
}
3939

40+
bash_object.util.generate_vobject_name() {
41+
unset REPLY
42+
43+
local random_string=
44+
if ((BASH_VERSINFO[0] >= 6)) || ((BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1)); then
45+
random_string="${SRANDOM}_${SRANDOM}_${SRANDOM}_${SRANDOM}_${SRANDOM}"
46+
else
47+
random_string="${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
48+
fi
49+
50+
REPLY="__bash_object_${root_object_name}_tree_${key}_${random_string}"
51+
}
52+
4053
# TODO
4154
stdtrace.log() {
4255
local level="$1"

tests/set.bats

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# shellcheck shell=bash
2+
3+
load './util/init.sh'
4+
5+
@test "Error if random variable already exists" {
6+
bash_object.util.generate_vobject_name() {
7+
REPLY="some_other_var"
8+
}
9+
declare -g some_other_var=
10+
11+
run bash_object.traverse-set object 'OBJECT' '.obj' obj
12+
13+
assert_failure
14+
assert_output -p 'ERROR_INTERNAL_MISCELLANEOUS'
15+
assert_output -p "Autogenerated variable 'some_other_var' already exists"
16+
}

0 commit comments

Comments
 (0)