autobuild3
autobuild3 copied to clipboard
Base_Data: Switch to arrays for what should be arrays.
In our code, multiple stuffs that should be arrays (e.g. damn flags) are stored as strings, causing undesired results sometime, especially with param expansions.
Basic compatibility for data patterns can be achived in such way:
# Ultra-ugly varnames to avoid colision
typeof(){ local ___t_{a,t,d,c}; for ___t_c; do read ___t_{a,t,d} <<< "$(declare -p "$___t_c")"; echo "$___t_t"; done; }
if [[ "$(typeof should_be_arr_but_was_not)" != *a* ]]; then
read -ra should_be_arr_but_was_not <<< "$should_be_arr_but_was_not"
fi
However, commands cannot return arrays. All they can do are strings. Fortunately, we have \0
:
foo_func(){
for i in a b c; do
if [ -e "$i" ]; then
magic_"$i" &&
printf '%s\0' "$i"
fi || break
done
}
# http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS= read -rd '' file; do
a[i++]="$file"
done < <(foo_func)
Or we will make all array-like outputting functions to put their output into a certain $_ab_tmp_arr
, or use declare -n
directives.
The current typeof
doesn't recurse into levels of references, so a recursive version is also needed.
[TG @JeffBai] @MingcongBai Updated read
trick. Can be slow.
Looks like I forgot all the opts in official_builds…
To-morrow, to-morrow, to-morrow!