stdenv: detect the type of variable in {prepend,append}ToVar

stdenv: error if using {prepend,append}ToVar on associative array

i don't know how to prepend to associative array
This commit is contained in:
Artturin 2022-12-10 01:09:40 +02:00
parent bf972f1873
commit 11c3127e38
2 changed files with 63 additions and 6 deletions

View File

@ -209,8 +209,31 @@ addToSearchPath() {
# Expressions for individual packages should simply switch to array
# syntax when they switch to setting __structuredAttrs = true.
prependToVar() {
local -n nameref="$1"; shift
local -n nameref="$1"
useArray=
if [ -n "$__structuredAttrs" ]; then
useArray=true
else
useArray=false
fi
# check if variable already exist and if it does then do extra checks
if declare -p "$1" 2> /dev/null | grep -q '^'; then
type="$(declare -p "$1")"
if [[ "$type" =~ "declare -A" ]]; then
echo "prependToVar(): ERROR: trying to use prependToVar on an associative array." >&2
return 1
elif [[ "$type" =~ "declare -a" ]]; then
useArray=true
else
useArray=false
fi
fi
shift
if $useArray; then
nameref=( "$@" ${nameref+"${nameref[@]}"} )
else
nameref="$* ${nameref-}"
@ -219,8 +242,31 @@ prependToVar() {
# Same as above
appendToVar() {
local -n nameref="$1"; shift
local -n nameref="$1"
useArray=
if [ -n "$__structuredAttrs" ]; then
useArray=true
else
useArray=false
fi
# check if variable already exist and if it does then do extra checks
if declare -p "$1" 2> /dev/null | grep -q '^'; then
type="$(declare -p "$1")"
if [[ "$type" =~ "declare -A" ]]; then
echo "appendToVar(): ERROR: trying to use appendToVar on an associative array, use variable+=([\"X\"]=\"Y\") instead." >&2
return 1
elif [[ "$type" =~ "declare -a" ]]; then
useArray=true
else
useArray=false
fi
fi
shift
if $useArray; then
nameref=( ${nameref+"${nameref[@]}"} "$@" )
else
nameref="${nameref-} $*"

View File

@ -60,10 +60,6 @@ let
};
passAsFile = [ "buildCommand" ] ++ lib.optionals (extraAttrs ? extraTest) [ "extraTest" ];
# FIXME: with structuredAttrs string is converted to a indexed array
# should a/pToVar check if the passed variable is a array or a string?
# declare -x string="testing-string"
# declare -ax string=([0]="world" [1]="testing-string" [2]="hello")
buildCommand = ''
declare -p string
appendToVar string hello
@ -71,8 +67,23 @@ let
prependToVar string "world"
declare -p string
declare -A associativeArray=(["X"]="Y")
[[ $(appendToVar associativeArray "fail" 2>&1) =~ "trying to use" ]] || (echo "prependToVar did not catch prepending associativeArray" && false)
[[ $(prependToVar associativeArray "fail" 2>&1) =~ "trying to use" ]] || (echo "prependToVar did not catch prepending associativeArray" && false)
[[ $string == "world testing-string hello" ]] || (echo "'\$string' was not 'world testing-string hello'" && false)
# test appending to a unset variable
appendToVar nonExistant created hello
typeset -p nonExistant
if [[ -n $__structuredAttrs ]]; then
[[ "''${nonExistant[@]}" == "created hello" ]]
else
# there's a extra " " in front here and a extra " " in the end of prependToVar
# shouldn't matter because these functions will mostly be used for $*Flags and the Flag variable will in most cases already exit
[[ "$nonExistant" == " created hello" ]]
fi
eval "$extraTest"
touch $out