Skip to content

Commit 8eb920a

Browse files
committed
fix: Use underscore when using local -n. Closes #12
This prevents issues with name conflicts
1 parent 74cd7eb commit 8eb920a

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

Diff for: pkg/lib/traverse-get.sh

+20-20
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bash_object.traverse-get() {
6565

6666
# Start traversing at the root object
6767
local current_object_name="$root_object_name"
68-
local -n current_object="$root_object_name"
68+
local -n __current_object="$root_object_name"
6969

7070
# A stack of all the evaluated querytree elements
7171
# local -a querytree_stack=()
@@ -91,12 +91,12 @@ bash_object.traverse-get() {
9191
bash_object.trace_loop
9292

9393
# If 'key' is not a member of object or index of array, error
94-
if [ -z "${current_object[$key]+x}" ]; then
94+
if [ -z "${__current_object[$key]+x}" ]; then
9595
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (querytree index '$i') does not exist"
9696
return
9797
# If 'key' is a member of an object or index of array
9898
else
99-
local key_value="${current_object[$key]}"
99+
local key_value="${__current_object[$key]}"
100100

101101
# If 'key_value' is a virtual object, dereference it
102102
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
@@ -110,32 +110,32 @@ bash_object.traverse-get() {
110110
bash_object.parse_virtual_object "$virtual_item"
111111
local current_object_name="$REPLY1"
112112
local vmd_dtype="$REPLY2"
113-
local -n current_object="$current_object_name"
113+
local -n __current_object="$current_object_name"
114114

115115
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
116116
# Ensure the 'final_value' is the same type as specified by the user (WET)
117-
local current_object_type=
118-
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
117+
local __current_object_type=
118+
if ! __current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
119119
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
120120
return
121121
fi
122-
current_object_type="${current_object_type#declare -}"
123-
case "${current_object_type::1}" in
124-
A) current_object_type='object' ;;
125-
a) current_object_type='array' ;;
126-
-) current_object_type='string' ;;
127-
*) current_object_type='other' ;;
122+
__current_object_type="${__current_object_type#declare -}"
123+
case "${__current_object_type::1}" in
124+
A) __current_object_type='object' ;;
125+
a) __current_object_type='array' ;;
126+
-) __current_object_type='string' ;;
127+
*) __current_object_type='other' ;;
128128
esac
129129
case "$vmd_dtype" in
130130
object)
131-
if [ "$current_object_type" != object ]; then
132-
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"
131+
if [ "$__current_object_type" != object ]; then
132+
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"
133133
return
134134
fi
135135
;;
136136
array)
137-
if [ "$current_object_type" != array ]; 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"
137+
if [ "$__current_object_type" != array ]; 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"
139139
return
140140
fi
141141
;;
@@ -164,8 +164,8 @@ bash_object.traverse-get() {
164164
if [ "$flag_as_what" = 'as-value' ]; then
165165
declare -gA REPLY=()
166166
local key=
167-
for key in "${!current_object[@]}"; do
168-
REPLY["$key"]="${current_object[$key]}"
167+
for key in "${!__current_object[@]}"; do
168+
REPLY["$key"]="${__current_object[$key]}"
169169
done
170170
elif [ "$flag_as_what" = 'as-ref' ]; then
171171
bash_object.util.die 'ERROR_INTERNAL' "--ref not implemented"
@@ -195,7 +195,7 @@ bash_object.traverse-get() {
195195
if [ "$flag_as_what" = 'as-value' ]; then
196196
declare -ga REPLY=()
197197
# shellcheck disable=SC2190
198-
REPLY=("${current_object[@]}")
198+
REPLY=("${__current_object[@]}")
199199
elif [ "$flag_as_what" = 'as-ref' ]; then
200200
bash_object.util.die 'ERROR_INTERNAL' "--ref not implemented"
201201
return
@@ -238,7 +238,7 @@ bash_object.traverse-get() {
238238
bash_object.util.die 'ERROR_NOT_FOUND' "The passed querytree implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
239239
return
240240
elif ((i+1 == ${#REPLIES[@]})); then
241-
local value="${current_object[$key]}"
241+
local value="${__current_object[$key]}"
242242
if [ "$final_value_type" = object ]; then
243243
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Queried for $final_value_type, but found existing string '$value'"
244244
return

Diff for: pkg/lib/traverse-set.sh

+28-28
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ bash_object.traverse-set() {
181181

182182
# Start traversing at the root object
183183
local current_object_name="$root_object_name"
184-
local -n current_object="$root_object_name"
184+
local -n __current_object="$root_object_name"
185185

186186
# A stack of all the evaluated querytree elements
187187
local -a querytree_stack=()
@@ -207,7 +207,7 @@ bash_object.traverse-set() {
207207
bash_object.trace_loop
208208

209209
# If 'key' is not a member of object or index of array, error
210-
if [ -z "${current_object[$key]+x}" ]; then
210+
if [ -z "${__current_object[$key]+x}" ]; then
211211
# If we are before the last element in the query, then error
212212
if ((i+1 < ${#REPLIES[@]})); then
213213
bash_object.util.die 'ERROR_NOT_FOUND' "Key or index '$key' (querytree index '$i') does not exist"
@@ -229,13 +229,13 @@ bash_object.traverse-set() {
229229
local -n global_object="$global_object_name"
230230
global_object=()
231231

232-
current_object["$key"]=$'\x1C\x1D'"type=object;&$global_object_name"
232+
__current_object["$key"]=$'\x1C\x1D'"type=object;&$global_object_name"
233233

234-
local -n object_to_copy_from="$final_value"
234+
local -n ___object_to_copy_from="$final_value"
235235

236-
for key in "${!object_to_copy_from[@]}"; do
236+
for key in "${!___object_to_copy_from[@]}"; do
237237
# shellcheck disable=SC2034
238-
global_object["$key"]="${object_to_copy_from[$key]}"
238+
global_object["$key"]="${___object_to_copy_from[$key]}"
239239
done
240240
elif [ "$final_value_type" = array ]; then
241241
bash_object.util.generate_vobject_name "$root_object_name" "$querytree_stack_string"
@@ -252,23 +252,23 @@ bash_object.traverse-set() {
252252
local -n global_array="$global_array_name"
253253
global_array=()
254254

255-
current_object["$key"]=$'\x1C\x1D'"type=array;&$global_array_name"
255+
__current_object["$key"]=$'\x1C\x1D'"type=array;&$global_array_name"
256256

257-
local -n array_to_copy_from="$final_value"
257+
local -n ___array_to_copy_from="$final_value"
258258

259259
# shellcheck disable=SC2034
260-
global_array=("${array_to_copy_from[@]}")
260+
global_array=("${___array_to_copy_from[@]}")
261261
elif [ "$final_value_type" = string ]; then
262-
local -n string_to_copy_from="$final_value"
263-
current_object["$key"]="$string_to_copy_from"
262+
local -n ___string_to_copy_from="$final_value"
263+
__current_object["$key"]="$___string_to_copy_from"
264264
else
265265
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "Unexpected final_value_type '$final_value_type'"
266266
return
267267
fi
268268
fi
269269
# If 'key' is already a member of object or index of array
270270
else
271-
local key_value="${current_object[$key]}"
271+
local key_value="${__current_object[$key]}"
272272

273273
# If 'key_value' is a virtual object, dereference it
274274
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
@@ -282,32 +282,32 @@ bash_object.traverse-set() {
282282
bash_object.parse_virtual_object "$virtual_item"
283283
local current_object_name="$REPLY1"
284284
local vmd_dtype="$REPLY2"
285-
local -n current_object="$current_object_name"
285+
local -n __current_object="$current_object_name"
286286

287287
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
288288
# Ensure the 'final_value' is the same type as specified by the user (WET)
289-
local current_object_type=
290-
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
289+
local __current_object_type=
290+
if ! __current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
291291
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
292292
return
293293
fi
294-
current_object_type="${current_object_type#declare -}"
295-
case "${current_object_type::1}" in
296-
A) current_object_type='object' ;;
297-
a) current_object_type='array' ;;
298-
-) current_object_type='string' ;;
299-
*) current_object_type='other' ;;
294+
__current_object_type="${__current_object_type#declare -}"
295+
case "${__current_object_type::1}" in
296+
A) __current_object_type='object' ;;
297+
a) __current_object_type='array' ;;
298+
-) __current_object_type='string' ;;
299+
*) __current_object_type='other' ;;
300300
esac
301301
case "$vmd_dtype" in
302302
object)
303-
if [ "$current_object_type" != object ]; then
304-
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"
303+
if [ "$__current_object_type" != object ]; then
304+
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"
305305
return
306306
fi
307307
;;
308308
array)
309-
if [ "$current_object_type" != array ]; then
310-
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"
309+
if [ "$__current_object_type" != array ]; then
310+
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"
311311
return
312312
fi
313313
;;
@@ -384,16 +384,16 @@ bash_object.traverse-set() {
384384
bash_object.util.die 'ERROR_NOT_FOUND' "The passed querytree implies that '$key' accesses an object or array, but a string with a value of '$key_value' was found instead"
385385
return
386386
elif ((i+1 == ${#REPLIES[@]})); then
387-
local value="${current_object[$key]}"
387+
local value="${__current_object[$key]}"
388388
if [ "$final_value_type" = object ]; then
389389
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Assigning an $final_value_type, but found existing string '$value'"
390390
return
391391
elif [ "$final_value_type" = array ]; then
392392
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Assigning an $final_value_type, but found existing string '$value'"
393393
return
394394
elif [ "$final_value_type" = string ]; then
395-
local -n string_to_copy_from="$final_value"
396-
current_object["$key"]="$string_to_copy_from"
395+
local -n ___string_to_copy_from="$final_value"
396+
__current_object["$key"]="$___string_to_copy_from"
397397
else
398398
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "Unexpected final_value_type '$final_value_type'"
399399
return

Diff for: pkg/lib/util/ensure.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ bash_object.ensure.variable_does_exist() {
55
local variable_name="$1"
66

77
if [ -z "$variable_name" ]; then
8-
bash_object.util.die "ERROR_INTERNAL" "Parameter to function 'bash_object.ensure.variable_does_exist' was empty"
8+
bash_object.util.die 'ERROR_INTERNAL' "Parameter to function 'bash_object.ensure.variable_does_exist' was empty"
99
return
1010
fi
1111

@@ -20,7 +20,7 @@ bash_object.ensure.variable_does_not_exist() {
2020
local variable_name="$1"
2121

2222
if [ -z "$variable_name" ]; then
23-
bash_object.util.die "ERROR_INTERNAL" "Parameter to function 'bash_object.ensure.variable_does_not_exist' was empty"
23+
bash_object.util.die 'ERROR_INTERNAL' "Parameter to function 'bash_object.ensure.variable_does_not_exist' was empty"
2424
return
2525
fi
2626

0 commit comments

Comments
 (0)