[WIP] A bash tool to read/write in tsv like db files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

134 lines
2.1 KiB

shopt -s expand_aliases
alias Object='declare -A'
alias Map='declare -A'
alias Array='declare -a'
alias Int='declare -i'
alias String='declare'
debug()
{
for var in "$@"
do echo -e $c_purple$var$c_="`exp $var`"
done
}
#exp(){ src="$(declare -p $1)"; echo "${src#*=}"; }
exp()
{
# echo ""
# echo ""
echo "Expose $@"
local path=($(echo "$@" | awk -F '[[\].]' '{for(i=1;i<=NF;i++){printf "%s ", $i}; printf "\n"}'))
# declare -p path
# local cur="$(declare -p ${path[0]})"
local src="$(declare -p $path)"
src="${src#*=}"
Object obj=$src
# local cur=($path)
path=(${path[@]:1})
# declare -p obj
while [[ -n $path ]]
do
# echo "while) " $path "${obj[$path]}"
# cur+=$path
src=${obj[$path]}
Object obj=$src
# declare -p obj
# src="$(declare -p obj)"
# src="${src#*=}"
# declare -p src
# obj=$src
path=(${path[@]:1})
done
# src="$(declare -p cur)"
echo "$src"
}
function exp()
{
declare -p $1 | sed 's/^declare -. [^=]*=//' # | sed 's/)$//'
}
function Object()
{
# echo o
case "${@:2}" in
\"*) String $1 ${@:2} ;;
\(\[[0-9]*) Array $1 ${@:2} ;;
\(\[*) Assoc $1 ${@:2} ;;
*) echo "declare $1${@:2}"
esac
}
function tg()
{
src="${@:2}"
is_str=`echo "${@:2}" | grep -v '^".*"$'`
is_arr=`echo "${@:2}" | grep -v '^\(\['`
is_arr=$is_arr`echo "${@:2}" | grep '^\[[0-9]*\]='`
[[ -z $is_arr ]] && echo "declare -A $1=(${@:2})" || echo "declare -a $1=(${@:2})"
}
#Assoc()
#{
# echo "declare -A $1${@:2}"
#}
#function Array()
#{
# echo "declare -a $1${@:2}"
#}
#function String()
#{
# echo "declare $1${@:2}"
#}
map2keys()
{
Map obj=$1
for key in ${obj[@]}
do echo $key
done
}
array2map()
{
Array fields=$1
Array values=$2
Map result=()
for i in ${!fields[@]}
do
result[${fields[$i]}]=${values[$i]}
done
exp result
}
map2json()
{
# eval $1
Map data=$1
printf "{"
for key in ${!data[@]}
do
printf '"%s":' $key
case "${data[$key]}" in
true|false|yes|no)printf '%s' "${data[$key]}" ;;
[0-9.,]) printf '%s' "${data[$key]}" ;;
*) printf '"%s"' "${data[$key]}" ;;
esac
printf ","
# printf '"%s",' "${data[$key]}"
done | sed 's/,$//'
printf "}"
}