Skip to content

Commit

Permalink
Introduce toenv for datastructures
Browse files Browse the repository at this point in the history
toenv is like dump, but it exports the internal representation of
the datastructures.

The output format looks like this:

_T1='Foo'
_T2='Bar'
_L1='_T1
_T2'

Which is exactly how variables are spread in the shell process to
store each piece of data.

This helps not only troubleshooting, but fast save/restore of state
without having to re-interpret expressions and call constructors
all over again (although you might override stuff if the counters
are not properly accounted for).
  • Loading branch information
alganet committed Jul 7, 2024
1 parent b9c13c0 commit dcec6f8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
PERFORMANCE OF THIS SOFTWARE.
35 changes: 32 additions & 3 deletions idiom/data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ dump () {
REPLY="$dump"
}

toenv () {
local dump= item= len=0 count=0
case $1 in
_T*) eval dump=\"$1=\'\$$1\'\" ;;
_[LS]*)
dump=
eval "REPLY=\"\$$1\""
for item in $REPLY
do
toenv $item
dump="$dump$REPLY${__EOL__}"
done
eval "dump=\"\${dump}$1='\$$1'\"\${__EOL__}"
;;
_A*)
eval dump=\"$1=\'\$$1\'\${__EOL__}\"
eval "len=\"\$(($1))\""
while test $count -lt $len
do
eval "toenv \$$1i$count"
eval "dump=\"\${dump}$1i$count=\$$1i$count\"\${__EOL__}"
dump="$dump$REPLY${__EOL__}"
count=$((count + 1))
done
;;
esac
REPLY="$dump"
}

# The Txt pseudotype constructor
Txt () {
_T=$((_T + 1))
Expand Down Expand Up @@ -127,12 +156,12 @@ Set () {
}

Set_add () {
_R=$1
_R="$1"
while test $# -gt 1
do shift ; eval "
case \"\$$_R\$__EOL__\" in
case \"\$__EOL__\$$_R\$__EOL__\" in
*\"\$__EOL__\$1\$__EOL__\"*);;
*)$_R=\"\$$_R\$__EOL__\$1\";;
*)$_R=\"\$$_R\${$_R:+\$__EOL__}\$1\";;
esac"
done
}
Expand Down
12 changes: 6 additions & 6 deletions test/_idiom/004-Set.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
test_Set_noargs () {
Set
eval deref=\$$_R
tap_assert "${__EOL__}" "$deref"
tap_assert "" "$deref"
}

test_Set_unary_zerolength () {
Set ""
eval deref=\$$_R
tap_assert "${__EOL__}" "$deref"
tap_assert "" "$deref"
}

test_Set_simple () {
Set foo bar baz
eval deref=\$$_R
tap_assert "${__EOL__}foo${__EOL__}bar${__EOL__}baz" "$deref"
tap_assert "foo${__EOL__}bar${__EOL__}baz" "$deref"
}

test_Set_uniqueness_A () {
Set foo foo bar
eval deref=\$$_R
tap_assert "${__EOL__}foo${__EOL__}bar" "$deref"
tap_assert "foo${__EOL__}bar" "$deref"
}

test_Set_uniqueness_B () {
Set foo bar foo
eval deref=\$$_R
tap_assert "${__EOL__}foo${__EOL__}bar" "$deref"
tap_assert "foo${__EOL__}bar" "$deref"
}

test_Set_uniqueness_C () {
Set bar foo foo
eval deref=\$$_R
tap_assert "${__EOL__}bar${__EOL__}foo" "$deref"
tap_assert "bar${__EOL__}foo" "$deref"
}
40 changes: 40 additions & 0 deletions test/_idiom/007-dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ test_dump_Txt () {
tap_assert "'Lorem'" "$REPLY"
}

test_toenv_Txt () {
val first_name = "Lorem"
local ref=$_R
toenv $first_name
tap_assert "$_R='Lorem'" "$REPLY"
}

test_dump_Lst () {
val first_name = "Lorem"
val last_name = "Ipsum"
Expand All @@ -15,6 +22,17 @@ test_dump_Lst () {
tap_assert "[ Lst 'Lorem' 'Ipsum' ]" "$REPLY"
}

test_toenv_Lst () {
val first_name = "Lorem"
local r1=$_R
val last_name = "Ipsum"
local r2=$_R
val name_parts = [ Lst $first_name $last_name ]
local r3=$_R
toenv $name_parts
tap_assert "$r1='Lorem'${__EOL__}$r2='Ipsum'${__EOL__}$r3='$r1${__EOL__}$r2'${__EOL__}" "$REPLY"
}

test_dump_Set () {
val first_name = "Lorem"
val last_name = "Ipsum"
Expand All @@ -23,6 +41,17 @@ test_dump_Set () {
tap_assert "[ Set 'Lorem' 'Ipsum' ]" "$REPLY"
}

test_toenv_Set () {
val first_name = "Lorem"
local r1=$_R
val last_name = "Ipsum"
local r2=$_R
val name_parts = [ Set $first_name $last_name ]
local r3=$_R
toenv $name_parts
tap_assert "$r1='Lorem'${__EOL__}$r2='Ipsum'${__EOL__}$r3='$r1${__EOL__}$r2'${__EOL__}" "$REPLY"
}

test_dump_Arr () {
val first_name = "Lorem"
val last_name = "Ipsum"
Expand All @@ -31,6 +60,17 @@ test_dump_Arr () {
tap_assert "[ Arr 'Lorem' 'Ipsum' ]" "$REPLY"
}

test_toenv_Arr () {
val first_name = "Lorem"
local r1=$_R
val last_name = "Ipsum"
local r2=$_R
val name_parts = [ Arr $first_name $last_name ]
local r3=$_R
toenv $name_parts
tap_assert "$r3='2'${__EOL__}${r3}i0=$r1${__EOL__}$r1='Lorem'${__EOL__}${r3}i1=$r2${__EOL__}$r2='Ipsum'${__EOL__}" "$REPLY"
}

test_dump_nested_Lst () {
val first_name = "Lorem"
val last_name = "Ipsum"
Expand Down

0 comments on commit dcec6f8

Please sign in to comment.