Skip to content

Commit 545ba94

Browse files
committed
BREAKING CHANGE: Current variable setting now defaults to
--pass-by-ref Objects, arrays, and strings (sine a few commits ago), are set by passing in the name of the variable that holds either the object, array, or string. bash-object would copy that info into the new location correctly. Now, we force the consumer to pass --pass-by-ref due to the introduction of the --pass-by-value option. --pass-by-value has not been implemented yet
1 parent ce70f07 commit 545ba94

File tree

8 files changed

+178
-101
lines changed

8 files changed

+178
-101
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ bpm install
6060
- ensure error (for set primarily) if the virtual object references a variable that does not exist
6161
- "queried for X, but found existing object": print object in error (same with indexed arrays)
6262
- zerocopy for get and set
63+
- -p flag?

pkg/lib/cmd/bobject.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ bobject() {
1919
bash_object.traverse-get object "$@"
2020
;;
2121
set-string)
22-
bash_object.traverse-set string "$@"
22+
bash_object.traverse-set --pass-by-ref string "$@"
2323
;;
2424
set-array)
25-
bash_object.traverse-set array "$@"
25+
bash_object.traverse-set --pass-by-ref array "$@"
2626
;;
2727
set-object)
28-
bash_object.traverse-set object "$@"
28+
bash_object.traverse-set --pass-by-ref object "$@"
2929
;;
3030
*)
3131
bash_object.util.die 'ERROR_INVALID_ARGS' "Subcommand '$subcmd' not recognized"

pkg/lib/traverse-set.sh

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
11
# shellcheck shell=bash
22

33
bash_object.traverse-set() {
4-
# TODO: rename zerocopy, as it's misleading - ex. 'zerocopy' object would be slower
5-
local flag_zerocopy='no'
6-
74
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
85
stdtrace.log 0 ''
96
stdtrace.log 0 "CALL: bash_object.traverse-set: $*"
107
fi
118

9+
local flag_pass_by_what=''
10+
11+
for arg; do case "$arg" in
12+
--pass-by-ref) flag_pass_by_what='by-ref'; shift ;;
13+
--pass-by-value) flag_pass_by_what='by-value'; shift ;;
14+
--) break ;;
15+
esac done
16+
17+
if [ -z "$flag_pass_by_what" ]; then
18+
bash_object.util.die 'ERROR_INVALID_ARGS' "Must pass either the '--pass-by-ref' or '--pass-by-value' flag"
19+
return
20+
fi
21+
1222
local final_value_type="$1"
1323
local root_object_name="$2"
1424
local filter="$3"
1525
local final_value="$4"
1626

17-
if (( $# != 4)); then
27+
# We can only check the correct argument number under certain conditions
28+
if [ "$flag_pass_by_what" = 'by-ref' ] && (( $# != 4)); then
29+
bash_object.util.die 'ERROR_INVALID_ARGS' "Expected '4' arguments, but received '$#'"
30+
return
31+
elif [[ "$flag_pass_by_what" == 'by-value' && "$final_value_type" == 'string' ]] && (( $# != 4 )); then
1832
bash_object.util.die 'ERROR_INVALID_ARGS' "Expected '4' arguments, but received '$#'"
1933
return
2034
fi
2135

22-
# TODO: zerocopy check
2336
# Ensure parameters are not empty
24-
if [ "$flag_zerocopy" = no ]; then
25-
for ((i=1; i<=$#; i++)); do
26-
if [ -z "${!i}" ]; then
27-
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '$i' is empty. Please check passed parameters"
28-
return
29-
fi
30-
done
31-
elif [ "$flag_zerocopy" = yes ]; then
32-
# If we are zerocopying, it means the string or array is passed on the
33-
# command line; we don't check it since the string or the first element
34-
# of array or string could be empty
35-
for ((i=1; i<=$#-1; i++)); do
36-
if [ -z "${!i}" ]; then
37-
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '$i' is empty. Please check passed parameters"
38-
return
39-
fi
40-
done
41-
else
42-
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' "Unexpected final_value_type '$final_value_type $actual_final_value_type'"
37+
if [ -z "$final_value_type" ]; then
38+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '1' is empty. Please check passed parameters"
39+
return
40+
fi
41+
if [ -z "$root_object_name" ]; then
42+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '2' is empty. Please check passed parameters"
43+
return
44+
fi
45+
if [ -z "$filter" ]; then
46+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '3' is empty. Please check passed parameters"
4347
return
4448
fi
49+
if [[ "$flag_pass_by_what" == 'by-ref' && -z "$final_value" ]]; then
50+
# Can only check if passing by ref, since we do not want to error if
51+
# an empty string is passed (by value) or an array with empty string at
52+
# index 0 is passed (by value)
53+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '4' is empty. Please check passed parameters"
54+
return
55+
fi
56+
57+
shift 4
58+
# Do not do 'shift 5', since 5 is greater than 4, the minimum amount of valid parameters
59+
if [ "$1" = -- ]; then
60+
shift
61+
fi
4562

4663
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
4764
# Ensure the root object exists, and is an associative array
@@ -56,39 +73,40 @@ bash_object.traverse-set() {
5673
return
5774
fi
5875

59-
# TODO: dont' do this in zerocopy mode
60-
# Ensure the 'final_value' is the same type as specified by the user
61-
local actual_final_value_type=
62-
if ! actual_final_value_type="$(declare -p "$final_value" 2>/dev/null)"; then
63-
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The variable '$final_value' does not exist"
64-
return
65-
fi
66-
actual_final_value_type="${actual_final_value_type#declare -}"
67-
case "${actual_final_value_type::1}" in
68-
A) actual_final_value_type='object' ;;
69-
a) actual_final_value_type='array' ;;
70-
-) actual_final_value_type='string' ;;
71-
*) actual_final_value_type='other' ;;
72-
esac
73-
74-
if [ "$final_value_type" == object ]; then
75-
if [ "$actual_final_value_type" != object ]; then
76-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
77-
return
78-
fi
79-
elif [ "$final_value_type" == array ]; then
80-
if [ "$actual_final_value_type" != array ]; then
81-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
76+
if [ "$flag_pass_by_what" = 'by-ref' ]; then
77+
# Ensure the 'final_value' is the same type as specified by the user
78+
local actual_final_value_type=
79+
if ! actual_final_value_type="$(declare -p "$final_value" 2>/dev/null)"; then
80+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The variable '$final_value' does not exist"
8281
return
8382
fi
84-
elif [ "$final_value_type" == string ]; then
85-
if [ "$actual_final_value_type" != string ]; then
86-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
83+
actual_final_value_type="${actual_final_value_type#declare -}"
84+
case "${actual_final_value_type::1}" in
85+
A) actual_final_value_type='object' ;;
86+
a) actual_final_value_type='array' ;;
87+
-) actual_final_value_type='string' ;;
88+
*) actual_final_value_type='other' ;;
89+
esac
90+
91+
if [ "$final_value_type" == object ]; then
92+
if [ "$actual_final_value_type" != object ]; then
93+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
94+
return
95+
fi
96+
elif [ "$final_value_type" == array ]; then
97+
if [ "$actual_final_value_type" != array ]; then
98+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
99+
return
100+
fi
101+
elif [ "$final_value_type" == string ]; then
102+
if [ "$actual_final_value_type" != string ]; then
103+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
104+
return
105+
fi
106+
else
107+
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
87108
return
88109
fi
89-
else
90-
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
91-
return
92110
fi
93111
fi
94112

tests/set-array.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load './util/init.sh'
66
declare -a arr=(omicron pi rho)
77
declare -A OBJECT=()
88

9-
bash_object.traverse-set array 'OBJECT' '.arr' arr
9+
bash_object.traverse-set --pass-by-ref array 'OBJECT' '.arr' arr
1010

1111
bash_object.traverse-get array 'OBJECT' '.arr'
1212
assert [ ${#REPLY[@]} -eq 3 ]

0 commit comments

Comments
 (0)