autobuild3 icon indicating copy to clipboard operation
autobuild3 copied to clipboard

Base_Data: Switch to arrays for what should be arrays.

Open Artoria2e5 opened this issue 8 years ago • 3 comments

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 avatar Aug 25 '15 03:08 Artoria2e5

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

Artoria2e5 avatar Aug 25 '15 03:08 Artoria2e5

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

Artoria2e5 avatar Oct 13 '15 19:10 Artoria2e5

Looks like I forgot all the opts in official_builds…

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

Artoria2e5 avatar Mar 15 '16 02:03 Artoria2e5