Created
April 18, 2019 23:23
-
-
Save traviscj/9e6589c3f73f130f2376e0db70724602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# scenario: want to make this json data structure from a shell script (and store it in a variable): | |
# | |
# "menuitem": [ | |
# {"value": "New", "onclick": "CreateNewDoc()"}, | |
# {"value": "Open", "onclick": "OpenDoc()"}, | |
# {"value": "Close", "onclick": "CloseDoc()"} | |
# ] | |
function newBuilder { | |
echo | |
} | |
function newList { | |
echo | |
} | |
function set { | |
INPUT=$(cat -) | |
FIELD=$1 | |
VALUE=$2 | |
if [[ $INPUT != "" ]]; then | |
echo -n ${INPUT}, | |
fi | |
echo -n '"'$FIELD'":'$VALUE | |
} | |
function setOrClear { | |
INPUT=$(cat -) | |
FIELD=$1 | |
VALUE=$2 | |
if [[ $VALUE != "" && $VALUE != "\"\"" ]]; then | |
echo -n ${INPUT} | set $FIELD $VALUE | |
else | |
echo -n ${INPUT} | |
fi | |
} | |
function add { | |
INPUT=$(cat -) | |
THING=$1 | |
if [[ $INPUT != "" ]]; then | |
echo -n ${INPUT}, | |
fi | |
echo -n $THING | |
} | |
function quoted { | |
THING=$1 | |
echo -n '"'$THING'"' | |
} | |
function buildList { | |
INPUT=$(cat -) | |
echo -n "[$INPUT]" | |
} | |
function build { | |
INPUT=$(cat -) | |
echo -n "{$INPUT}" | |
} | |
MENUITEMS=$(newList \ | |
| add $(newBuilder \ | |
| set value $(quoted New) \ | |
| set onclick $(quoted CreateNewDoc\(\)) \ | |
| build) \ | |
| add $(newBuilder \ | |
| set value $(quoted Open) \ | |
| set onclick $(quoted OpenDoc\(\)) \ | |
| build) \ | |
| add $(newBuilder \ | |
| set value $(quoted Close) \ | |
| set onclick $(quoted CloseDoc\(\)) \ | |
| build) \ | |
| buildList) | |
function mkList { | |
mktemp /tmp/XXXX | |
} | |
function appendList { | |
LISTFILE=$1 | |
ELEMENT=$2 | |
cat ${LISTFILE} | add ${ELEMENT} > ${LISTFILE}.new | |
mv ${LISTFILE}.new ${LISTFILE} | |
} | |
function finishList { | |
LISTFILE=$1 | |
cat ${LISTFILE} | buildList > ${LISTFILE}.new | |
mv ${LISTFILE}.new ${LISTFILE} | |
OUT=$(cat ${LISTFILE}) | |
rm $LISTFILE | |
echo $OUT | |
} | |
cat > map << EOF | |
New CreateNewDoc | |
Open OpenDoc | |
Close CloseDoc | |
EOF | |
LIST=$(mkList) | |
for fn in New Open Close; do | |
# pretend this is arbitrary logic: | |
click=$(grep $fn map | cut -d' ' -f 2) | |
appendList $LIST $(newBuilder \ | |
| set value $(quoted $fn) \ | |
| set onclick $(quoted $click\(\)) \ | |
| build) | |
done | |
MENUITEMSLOOP=$(finishList $LIST) | |
function newAction { | |
fn=$1 | |
click=$2 | |
newBuilder \ | |
| set value $(quoted $fn) \ | |
| set onclick $(quoted $click\(\)) \ | |
| build | |
} | |
MENUITEMSCONCISE=$(newList \ | |
| add $(newAction New CreateNewDoc) \ | |
| add $(newAction Open OpenDoc) \ | |
| add $(newAction Close CloseDoc) \ | |
| buildList) | |
EMPTY= | |
POPULATED="populated" | |
testy1=$(newBuilder \ | |
| setOrClear field1 $(quoted ${POPULATED}) \ | |
| set field2 $(quoted ${EMPTY}) \ | |
| build) | |
testy2=$(newBuilder \ | |
| setOrClear field1 $(quoted ${EMPTY}) \ | |
| set field2 $(quoted ${POPULATED}) \ | |
| build) | |
testy3=$(newBuilder \ | |
| set field1 $(quoted ${POPULATED}) \ | |
| setOrClear field2 $(quoted ${EMPTY}) \ | |
| build) | |
testy4=$(newBuilder \ | |
| set field1 $(quoted ${EMPTY}) \ | |
| setOrClear field2 $(quoted ${POPULATED}) \ | |
| build) | |
echo $testy1 | |
echo $testy2 | |
echo $testy3 | |
echo $testy4 | |
echo $MENUITEMS | |
echo $MENUITEMSLOOP | |
echo $MENUITEMSCONCISE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment