Skip to content

Commit 8eaad6b

Browse files
committed
feat: Add bobject.print to print objects
1 parent eb98752 commit 8eaad6b

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

pkg/lib/public/bobject-print.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# shellcheck shell=bash
22

3-
bobject-print() {
4-
# TODO
5-
:
3+
bobject.print() {
4+
local object_name="$1"
5+
6+
if [ -z "$object_name" ]; then
7+
bash_object.util.die 'ERROR_ARGUMENTS_INVALID' 'Positional parameter 1 is empty. Please check passed parameters'
8+
return
9+
fi
10+
11+
if declare -p "$object_name" &>/dev/null; then :; else
12+
bash_object.util.die 'ERROR_NOT_FOUND' "The variable '$object_name' does not exist"
13+
return
14+
fi
15+
16+
bash_object.util.print_hierarchy "$object_name" 0
617
}

pkg/lib/util/util.sh

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,62 @@ bash_object.util.generate_vobject_name() {
5454
random_string="${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}_${RANDOM}"
5555
fi
5656

57-
printf -v REPLY '%q' "__bash_object_${root_object_name}_${root_object_query}_${random_string}"
57+
printf -v REPLY '%q' "__bash_object_${root_object_name}___${root_object_query}_${random_string}"
58+
}
59+
60+
# @description Prints the contents of a particular variable
61+
# or vobject
62+
bash_object.util.print_hierarchy() {
63+
local object_name="$1"
64+
local current_indent="$2"
65+
66+
if object_type="$(declare -p "$object_name" 2>/dev/null)"; then :; else
67+
bash_object.util.die 'ERROR_NOT_FOUND' "The variable '$object_name' does not exist"
68+
return
69+
fi
70+
object_type="${object_type#declare -}"
71+
72+
local -n _object="$object_name"
73+
if [ "${object_type::1}" = 'A' ]; then
74+
for object_key in "${!_object[@]}"; do
75+
local object_value="${_object[$object_key]}"
76+
if [ "${object_value::2}" = $'\x1C\x1D' ]; then
77+
# object_value is a vobject
78+
bash_object.parse_virtual_object "$object_value"
79+
local virtual_object_name="$REPLY1"
80+
local vmd_dtype="$REPLY2"
81+
82+
printf '%*s' "$current_indent" ''
83+
printf '%s\n' "|__ $object_key ($virtual_object_name)"
84+
85+
bash_object.util.print_hierarchy "$virtual_object_name" $((current_indent+3))
86+
else
87+
# object_value is a string
88+
printf '%*s' "$current_indent" ''
89+
printf '%s\n' "|__ $object_key"
90+
fi
91+
done; unset object_key
92+
elif [ "${object_type::1}" = 'a' ]; then
93+
for object_value in "${_object[@]}"; do
94+
# object_value is a vobject
95+
if [ "${object_value::2}" = $'\x1C\x1D' ]; then
96+
bash_object.parse_virtual_object "$object_value"
97+
local virtual_object_name="$REPLY1"
98+
local vmd_dtype="$REPLY2"
99+
100+
printf '%*s' "$current_indent" ''
101+
printf '%s\n' "|- $object_value ($virtual_object_name)"
102+
103+
bash_object.util.print_hierarchy "$virtual_object_name" $((current_indent+2))
104+
else
105+
printf '%*s' "$current_indent" ''
106+
printf '%s\n' "|- $object_value"
107+
fi
108+
done
109+
else
110+
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "The type of the named object ($object_name) is neither an array nor an object"
111+
return
112+
fi
58113
}
59114

60115
# @description A stringified version of the querytree

tests/bobject-print.bats

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats
2+
3+
load './util/init.sh'
4+
5+
@test "Errors if first parameter is invalid" {
6+
run bobject.print
7+
8+
assert_failure
9+
assert_line -p 'ERROR_ARGUMENTS_INVALID'
10+
assert_line -p 'Positional parameter 1 is empty. Please check passed parameters'
11+
}
12+
13+
@test "Errors if passed parameter is not a variable" {
14+
run bobject.print 'variable'
15+
16+
assert_failure
17+
assert_line -p 'ERROR_NOT_FOUND'
18+
assert_line -p "The variable 'variable' does not exist"
19+
}
20+
21+
@test "Errors if passed parameter is neither an object nor an array" {
22+
variable=
23+
run bobject.print 'variable'
24+
25+
assert_failure
26+
assert_line -p 'ERROR_ARGUMENTS_INVALID_TYPE'
27+
assert_line -p "is neither an array nor an object"
28+
}

0 commit comments

Comments
 (0)