From c1260ca34d9b41d617a4398759443a1df1abfab7 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jan 2024 10:33:27 -0500 Subject: factoring --- wordpress/export-json.bash | 172 ++++++++++++++++++++++++++++++++------------- 1 file changed, 122 insertions(+), 50 deletions(-) diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash index 3852dea..5404ceb 100644 --- a/wordpress/export-json.bash +++ b/wordpress/export-json.bash @@ -85,6 +85,34 @@ arg1_to_env0() fi } +# for_each - environmentally-hygienic loop +# +# The first argument is de-quoted as a command +# evaluated once against each of the other +# supplied arguments, in order, aborting if the +# command returns an error. +# +# Doesn't touch the bash environment. +for_each() +{ + while [ $# -ge 2 ] + do + $1 "$2" || return + # This just removes $2 from the arglist: + eval 'shift 2; set -- '"${1@Q}"' "$@"' || return + done +} + +# IDEA: bash module w/ some simple FORTH style +# point-free ops on arglist. To eliminate the +# evals. Of course, there is not much use for +# point-free code like this, it only makes +# sense if you care about the code providing a +# pristine environment for the callback -- which +# only matters for highly general library style +# code meant to be reused in unknown future +# environments. + export_to_env0() { while [ $# -gt 0 ] @@ -96,56 +124,14 @@ export_to_env0() to_JSON_all() { - ( - if [ "$1" = '-a' ] - then - jq_env - fi - set -- $(compgen -A arrayvar) - while [ $# -gt 0 ] - do - to_JSON "$1" - shift - done - ) | - jq -s 'add' -} - -to_JSON() -{ - case "$1" in - -A | -a ) - to_JSON_all "$@" - return - ;; - esac - [ $# = 1 ] || return - case "${!1@a}" in - *a* ) - ( - eval "set -- \"\$1\" --args \"\${${1}[@]}\"" && - jq -n '{ ($k): $ARGS.positional }' \ - --arg k "$@" - ) - ;; - *A* ) - ( - eval 'set -- --arg k "'${1}'" --args \ - "${!'${1}'[@]}" \ - "${'${1}'[@]}"' - if [ $# -gt 4 ] - then - jq -n "{ (\$k): $(jq_zip2) }" "$@" - else - jq -n '{ ($k): {} }' "$@" - fi - ) - ;; - * ) - [ -v "$1" ] && - jq -n '{ ($k): $v }' --arg k "$1" --arg v "${!1}" - ;; - esac + jq -s 'add' < \ + <( + if [ "$1" = '-a' ] + then + jq_env + fi + for_each to_JSON1 $(compgen -A arrayvar) + ) } jq_zip2() @@ -162,6 +148,79 @@ add END } +json_encode_associative_array() +{ + eval 'set -- --arg k "'"$1"'" --args \ + "${!'"$1"'[@]}" \ + "${'"$1"'[@]}"' && + if [ $# -gt 4 ] + then + jq -n "{ (\$k): $(jq_zip2) }" "$@" + else + jq -n '{ ($k): {} }' "$@" + fi +} + +json_encode_indexed_array() +{ + eval 'set -- '"${1@Q}"' --args "${'"$1"'[@]}"' && + jq -n '{ ($k): $ARGS.positional }' --arg k "$@" +} + +# This uses the more complex implementation that, +# when used with multiple arguments, does not +# require extra calls to jq. Since this is only +# using a single argument, a more straightforward +# implementation could be used just as well. +json_encode_string() +{ + [ -v "$1" ] && + export_JSON_unsafe "$1" +} + +# The straightforward implementation: +json_encode_string() +{ + [ -v "$1" ] && + jq -n '{ ($k): $v }' --arg k "$1" --arg v "${!1}" +} +# But is having two implementations really +# straightforward? And what about this +# parenthetical commentary? Moreso wobbly, if not +# winding, error-prone complexity finding itself +# grinding up on the accidents of time passing it +# might be anyway but the takeaway is don't touch +# it till it croak, the tests will run and the +# result will speak, may truth spread through the +# dendrites of Samizdat! + +to_JSON() +{ + case $# in + 0) + to_JSON_all -a + ;; + *) + for_each to_JSON1 "$@" + ;; + esac +} + +to_JSON1() +{ + case "${!1@a}" in + *a* ) + json_encode_indexed_array "$1" + ;; + *A* ) + json_encode_associative_array "$1" + ;; + * ) + json_encode_string "$1" + ;; + esac +} + env0_to_JSON() { set -- @@ -241,3 +300,16 @@ runtest() try jq_env '.|{TERM,LANG,HOSTTYPE,EDITOR,SHELL}' try to_JSON PATH BASH_ARGV BASH_VERSINFO BASH_ALIASES BASH_CMDS } + +# code poetry flowetry toiletry coiled spring load +# the home row write tight c with terry davis down +# in a basement univalent foundation concrete on +# the bottom with donald while they all on top of +# things they found up there still we founding +# here building tall stacks of calls out to all to +# any accepting returns of types unknown or known +# +# defensively typed values for anomic millennial +# hackers +# +# atomized castaways of aging social bodies -- cgit v1.2.3