AOSC-Archive / autobuild3

AOSC OS package maintenance toolkit (version 3)
https://aosc.io
GNU General Public License v2.0
24 stars 17 forks source link

Base_Data: Switch to arrays for what should be arrays. #62

Open Artoria2e5 opened 8 years ago

Artoria2e5 commented 8 years ago

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.

Artoria2e5 commented 8 years ago

The current typeof doesn't recurse into levels of references, so a recursive version is also needed.

Artoria2e5 commented 8 years ago

[TG @JeffBai] @MingcongBai Updated read trick. Can be slow.

Artoria2e5 commented 8 years ago

Looks like I forgot all the opts in official_builds…

To-morrow, to-morrow, to-morrow!