diff --git a/demo/associative-arrays b/demo/associative-arrays index 81e7f0b..329ea54 100755 --- a/demo/associative-arrays +++ b/demo/associative-arrays @@ -14,6 +14,6 @@ Accessing data directly: Things in DATA: {{#DATA}} - Item: {{.}} + {{$}}: {{.}} {{/DATA}} EOF diff --git a/demo/associative-cross-arrays b/demo/associative-cross-arrays new file mode 100755 index 0000000..5591016 --- /dev/null +++ b/demo/associative-cross-arrays @@ -0,0 +1,28 @@ +#!/bin/bash + +cd "$(dirname "$0")" # Go to the script's directory + +declare -A DATA OTHER_DATA NUM_DATA +DATA=([one]=111 [two]=222) + +# Lookup another array using same keys +OTHER_DATA=([one]="!!!" [two]="@@@") + +# Lookup another array using value as key +NUM_DATA=([111]="One" [222]="Two") + +. ../mo + +cat <&2 - echo ${names[$3]} -} - -link_url() { - echo "$@" >&2 - echo ${urls[$2]} -} - - -# Source mo in order to work with arrays +declare -A DATA +DATA=([one]=111 [two]=222) . ../mo -# Process the template -cat </dev/null) || return 1 + [[ "${moTestResult:0:10}" == "declare -A" ]] && return 0 + + return 1 +} + + # Internal: Determine if the given name is a defined function. # # $1 - Function name to check @@ -734,16 +803,22 @@ moParse() { moContent="${moBlock[2]}" elif moIsArray "$moTag"; then local moFullKey moValue + declare -a moKeys moKeys=($(eval "echo \"\${!${moTag}[@]}\"")) + for k in "${moKeys[@]}"; do moFullTagName moFullKey "$moTag" "$k" moValue=$(moShow "$moFullKey" "$moFullKey") + + if moIsAssocArray "$moValue"; then + moExpandAssoc "$moValue" + fi + moCurContext=$(moAppendContext "$moContext" "$k" "$moValue") moParse "${moBlock[0]}" "$moFullKey" false "$moCurContext" done; - #eval "moLoop \"\${moBlock[0]}\" \"$moTag\" \"\${!${moTag}[@]}\"" else moParse "${moBlock[0]}" "$moCurrent" true "$moContext" fi @@ -791,6 +866,16 @@ moParse() { moShow "$moCurrent" "$moCurrent" ;; + '$') + moStandaloneDenied moContent "${moContent[@]}" + # Current content (environment variable or function) + if [[ "$moCurrent" == *.* ]]; then + echo -n "${moCurrent#*.}" + else + echo -n "$moCurrent" + fi + ;; + '=') # Change delimiters # Any two non-whitespace sequences separated by whitespace. @@ -811,15 +896,9 @@ moParse() { moFullTagName moTag "$moCurrent" "$moTag" moContent=${moContent[1]} - moCurContext="$moContext" - if [[ ! -z "$moCurrent" ]]; then - moBlock=$(moShow "$moCurrent" "$moCurrent") - moCurContext=$(moAppendContext "$moContext" "$k" "$moValue") - fi - # Now show the value # Quote moArgs here, do not quote it later. - moShow "$moTag" "$moCurrent" "$moArgs" "$moCurContext" + moShow "$moTag" "$moCurrent" "$moArgs" "$moContext" ;; '&'*) @@ -839,14 +918,8 @@ moParse() { moArgs=${moArgs:${#moTag}} moFullTagName moTag "$moCurrent" "$moTag" - moCurContext="$moContext" - if [[ ! -z "$moCurrent" ]]; then - moBlock=$(moShow "$moCurrent" "$moCurrent") - moCurContext=$(moAppendContext "$moContext" "$k" "$moValue") - fi - # Quote moArgs here, do not quote it later. - moShow "$moTag" "$moCurrent" "$moArgs" "$moCurContext" + moShow "$moTag" "$moCurrent" "$moArgs" "$moContext" ;; esac @@ -992,6 +1065,7 @@ moShow() { fi } + # Internal: Split a larger string into an array. # # $1 - Destination variable diff --git a/tests/function-args-loop.env b/tests/function-args-loop.env index 298505d..36bc26d 100644 --- a/tests/function-args-loop.env +++ b/tests/function-args-loop.env @@ -1,9 +1,11 @@ -arr=( - "test1" - "test2" +arr1=( + "foo1" +) +arr2=( + "foo2" + "bar2" ) testArgs() { - value=$(cat) - echo "$value - $MO_FUNCTION_ARGS" + echo "$MO_FUNCTION_ARGS ${MO_LOOP_KEYS[0]} ${MO_LOOP[0]}" } diff --git a/tests/function-args-loop.expected b/tests/function-args-loop.expected index ca2af94..e042e21 100644 --- a/tests/function-args-loop.expected +++ b/tests/function-args-loop.expected @@ -1,8 +1,10 @@ -No args: [test1 - ] - done -One arg: [test1 - one] - done -Multiple arguments: [test1 - aa bb cc 'x' " ! {[_.|] - done -Evil: [test1 - bla; cat /etc/issue] - done -No args: [test2 - ] - done -One arg: [test2 - one] - done -Multiple arguments: [test2 - aa bb cc 'x' " ! {[_.|] - done -Evil: [test2 - bla; cat /etc/issue] - done + +No args: 0: foo2 [ 0 foo2] - done +One arg: 0: foo2 [one 0 foo2] - done +Multiple arguments: 0: foo2 [aa bb cc 'x' " ! {[_.| 0 foo2] - done +Evil: 0: foo2 [bla; cat /etc/issue 0 foo2] - done + +No args: 1: bar2 [ 1 bar2] - done +One arg: 1: bar2 [one 1 bar2] - done +Multiple arguments: 1: bar2 [aa bb cc 'x' " ! {[_.| 1 bar2] - done +Evil: 1: bar2 [bla; cat /etc/issue 1 bar2] - done diff --git a/tests/function-args-loop.template b/tests/function-args-loop.template index 4d73fea..5ba9f92 100644 --- a/tests/function-args-loop.template +++ b/tests/function-args-loop.template @@ -1,6 +1,8 @@ -{{#arr}} -No args: [{{testArgs}}] - done -One arg: [{{testArgs one}}] - done -Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done -Evil: [{{testArgs bla; cat /etc/issue}}] - done -{{/arr}} +{{#arr1}} +{{#arr2}} +No args: {{$}}: {{.}} [{{testArgs}}] - done +One arg: {{$}}: {{.}} [{{testArgs one}}] - done +Multiple arguments: {{$}}: {{.}} [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done +Evil: {{$}}: {{.}} [{{testArgs bla; cat /etc/issue}}] - done +{{/arr2}} +{{/arr1}}