Skip to content

Commit

Permalink
fix: Use underscore when using local -n. Closes #12
Browse files Browse the repository at this point in the history
This prevents issues with name conflicts
  • Loading branch information
hyperupcall committed Nov 1, 2021
1 parent 74cd7eb commit 8eb920a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
40 changes: 20 additions & 20 deletions pkg/lib/traverse-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bash_object.traverse-get() {

# Start traversing at the root object
local current_object_name="$root_object_name"
local -n current_object="$root_object_name"
local -n __current_object="$root_object_name"

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

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

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

if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
# Ensure the 'final_value' is the same type as specified by the user (WET)
local current_object_type=
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
local __current_object_type=
if ! __current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
return
fi
current_object_type="${current_object_type#declare -}"
case "${current_object_type::1}" in
A) current_object_type='object' ;;
a) current_object_type='array' ;;
-) current_object_type='string' ;;
*) current_object_type='other' ;;
__current_object_type="${__current_object_type#declare -}"
case "${__current_object_type::1}" in
A) __current_object_type='object' ;;
a) __current_object_type='array' ;;
-) __current_object_type='string' ;;
*) __current_object_type='other' ;;
esac
case "$vmd_dtype" in
object)
if [ "$current_object_type" != object ]; then
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"
if [ "$__current_object_type" != object ]; then
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"
return
fi
;;
array)
if [ "$current_object_type" != array ]; then
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"
if [ "$__current_object_type" != array ]; then
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"
return
fi
;;
Expand Down Expand Up @@ -164,8 +164,8 @@ bash_object.traverse-get() {
if [ "$flag_as_what" = 'as-value' ]; then
declare -gA REPLY=()
local key=
for key in "${!current_object[@]}"; do
REPLY["$key"]="${current_object[$key]}"
for key in "${!__current_object[@]}"; do
REPLY["$key"]="${__current_object[$key]}"
done
elif [ "$flag_as_what" = 'as-ref' ]; then
bash_object.util.die 'ERROR_INTERNAL' "--ref not implemented"
Expand Down Expand Up @@ -195,7 +195,7 @@ bash_object.traverse-get() {
if [ "$flag_as_what" = 'as-value' ]; then
declare -ga REPLY=()
# shellcheck disable=SC2190
REPLY=("${current_object[@]}")
REPLY=("${__current_object[@]}")
elif [ "$flag_as_what" = 'as-ref' ]; then
bash_object.util.die 'ERROR_INTERNAL' "--ref not implemented"
return
Expand Down Expand Up @@ -238,7 +238,7 @@ bash_object.traverse-get() {
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"
return
elif ((i+1 == ${#REPLIES[@]})); then
local value="${current_object[$key]}"
local value="${__current_object[$key]}"
if [ "$final_value_type" = object ]; then
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Queried for $final_value_type, but found existing string '$value'"
return
Expand Down
56 changes: 28 additions & 28 deletions pkg/lib/traverse-set.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ bash_object.traverse-set() {

# Start traversing at the root object
local current_object_name="$root_object_name"
local -n current_object="$root_object_name"
local -n __current_object="$root_object_name"

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

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

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

local -n object_to_copy_from="$final_value"
local -n ___object_to_copy_from="$final_value"

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

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

local -n array_to_copy_from="$final_value"
local -n ___array_to_copy_from="$final_value"

# shellcheck disable=SC2034
global_array=("${array_to_copy_from[@]}")
global_array=("${___array_to_copy_from[@]}")
elif [ "$final_value_type" = string ]; then
local -n string_to_copy_from="$final_value"
current_object["$key"]="$string_to_copy_from"
local -n ___string_to_copy_from="$final_value"
__current_object["$key"]="$___string_to_copy_from"
else
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "Unexpected final_value_type '$final_value_type'"
return
fi
fi
# If 'key' is already a member of object or index of array
else
local key_value="${current_object[$key]}"
local key_value="${__current_object[$key]}"

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

if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
# Ensure the 'final_value' is the same type as specified by the user (WET)
local current_object_type=
if ! current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
local __current_object_type=
if ! __current_object_type="$(declare -p "$current_object_name" 2>/dev/null)"; then
bash_object.util.die 'ERROR_INTERNAL' "The variable '$current_object_name' does not exist"
return
fi
current_object_type="${current_object_type#declare -}"
case "${current_object_type::1}" in
A) current_object_type='object' ;;
a) current_object_type='array' ;;
-) current_object_type='string' ;;
*) current_object_type='other' ;;
__current_object_type="${__current_object_type#declare -}"
case "${__current_object_type::1}" in
A) __current_object_type='object' ;;
a) __current_object_type='array' ;;
-) __current_object_type='string' ;;
*) __current_object_type='other' ;;
esac
case "$vmd_dtype" in
object)
if [ "$current_object_type" != object ]; then
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"
if [ "$__current_object_type" != object ]; then
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"
return
fi
;;
array)
if [ "$current_object_type" != array ]; then
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"
if [ "$__current_object_type" != array ]; then
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"
return
fi
;;
Expand Down Expand Up @@ -384,16 +384,16 @@ bash_object.traverse-set() {
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"
return
elif ((i+1 == ${#REPLIES[@]})); then
local value="${current_object[$key]}"
local value="${__current_object[$key]}"
if [ "$final_value_type" = object ]; then
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Assigning an $final_value_type, but found existing string '$value'"
return
elif [ "$final_value_type" = array ]; then
bash_object.util.die 'ERROR_ARGUMENTS_INCORRECT_TYPE' "Assigning an $final_value_type, but found existing string '$value'"
return
elif [ "$final_value_type" = string ]; then
local -n string_to_copy_from="$final_value"
current_object["$key"]="$string_to_copy_from"
local -n ___string_to_copy_from="$final_value"
__current_object["$key"]="$___string_to_copy_from"
else
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "Unexpected final_value_type '$final_value_type'"
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/util/ensure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bash_object.ensure.variable_does_exist() {
local variable_name="$1"

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

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

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

Expand Down

0 comments on commit 8eb920a

Please sign in to comment.