Skip to content

Commit befa966

Browse files
committed
feat: Implement 'unset' for Bash objects. Closes #2
1 parent 4cdcd6f commit befa966

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

examples/script.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33

4+
lsobj() {
5+
local -i count=0
6+
while IFS= read -r line || [ -n "$line" ]; do
7+
((++count))
8+
if [[ $line =~ ^__bash_object ]]; then
9+
printf '%s\n' "$line"
10+
fi
11+
12+
done < <(set -o posix; set); unset line
13+
14+
printf 'COUNT: %d\n' "$count"
15+
}
16+
417
# Necessary for Basalt to load dependencies
518
eval "$(basalt-package-init)"
619
basalt.package-init
720
basalt.package-load
821

22+
lsobj
923

1024
declare -A root_object=()
1125
declare -A zulu_object=()
@@ -31,3 +45,7 @@ bobject get-string --value root_object '.["zulu"].["yankee"].["xray"].["foxtrot"
3145
printf '%s - %s\n' "$REPLY" rho
3246

3347
bobject.print 'root_object'
48+
49+
bobject.unset 'root_object'
50+
51+
lsobj

pkg/src/parse.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bash_object.parse_querytree() {
99

1010
local flag_parser_type=
1111

12+
local arg=
1213
for arg; do case $arg in
1314
--simple)
1415
flag_parser_type='simple'

pkg/src/public/bobject-unset.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# shellcheck shell=bash
2+
3+
bobject.unset() {
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.unset "$object_name"
17+
}

pkg/src/util/util.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,49 @@ bash_object.util.print_hierarchy() {
112112
fi
113113
}
114114

115+
# @description Prints the contents of a particular variable
116+
# or vobject
117+
bash_object.util.unset() {
118+
local object_name="$1"
119+
120+
if object_type="$(declare -p "$object_name" 2>/dev/null)"; then :; else
121+
bash_object.util.die 'ERROR_NOT_FOUND' "The variable '$object_name' does not exist"
122+
return
123+
fi
124+
object_type="${object_type#declare -}"
125+
126+
local -n _object="$object_name"
127+
if [ "${object_type::1}" = 'A' ]; then
128+
for object_key in "${!_object[@]}"; do
129+
local object_value="${_object[$object_key]}"
130+
if [ "${object_value::2}" = $'\x1C\x1D' ]; then
131+
# object_value is a vobject
132+
bash_object.parse_virtual_object "$object_value"
133+
local virtual_object_name="$REPLY1"
134+
local vmd_dtype="$REPLY2"
135+
136+
bash_object.util.unset "$virtual_object_name"
137+
unset "$virtual_object_name"
138+
fi
139+
done; unset object_key
140+
elif [ "${object_type::1}" = 'a' ]; then
141+
for object_value in "${_object[@]}"; do
142+
# object_value is a vobject
143+
if [ "${object_value::2}" = $'\x1C\x1D' ]; then
144+
bash_object.parse_virtual_object "$object_value"
145+
local virtual_object_name="$REPLY1"
146+
local vmd_dtype="$REPLY2"
147+
148+
bash_object.util.unset "$virtual_object_name"
149+
unset "$virtual_object_name"
150+
fi
151+
done
152+
else
153+
bash_object.util.die 'ERROR_ARGUMENTS_INVALID_TYPE' "The type of the named object ($object_name) is neither an array nor an object"
154+
return
155+
fi
156+
}
157+
115158
# @description A stringified version of the querytree
116159
# stack. This is used when generating objects to prevent
117160
# conflicts

0 commit comments

Comments
 (0)