From: Ian Jackson Date: Wed, 17 Jun 2020 23:20:12 +0000 (+0100) Subject: TOML::Tiny: Fix incorrect writing algorithm X-Git-Tag: nailing-cargo/1.0.0~182 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5489d0cf0fab34e8545ef78ee850e3c4ce2403b9;p=nailing-cargo.git TOML::Tiny: Fix incorrect writing algorithm Entries using [ ] and [[ ]] change the default context for assignments. So we must put all the bare assigments (using `=') (and plain values) first. Signed-off-by: Ian Jackson --- diff --git a/TOML-Tiny/lib/TOML/Tiny/Writer.pm b/TOML-Tiny/lib/TOML/Tiny/Writer.pm index 4e2275a..2d2b79b 100644 --- a/TOML-Tiny/lib/TOML/Tiny/Writer.pm +++ b/TOML-Tiny/lib/TOML/Tiny/Writer.pm @@ -17,7 +17,8 @@ use B qw( svref_2object SVf_IOK SVf_NOK ); sub to_toml { my $data = shift; my %param = @_; - my @buff; + my @buff_assign; + my @buff_tables; for (ref $data) { when ('HASH') { @@ -25,7 +26,7 @@ sub to_toml { for my $k (grep{ ref($data->{$_}) !~ /HASH|ARRAY/ } sort keys %$data) { my $key = to_toml_key($k); my $val = to_toml($data->{$k}, %param); - push @buff, "$key=$val"; + push @buff_assign, "$key=$val"; } # For values which are arrays, generate inline arrays for non-table @@ -34,7 +35,7 @@ sub to_toml { # Empty table if (!@{$data->{$k}}) { my $key = to_toml_key($k); - push @buff, "$key=[]"; + push @buff_assign, "$key=[]"; next ARRAY; } @@ -54,7 +55,7 @@ sub to_toml { if (@inline) { my $key = to_toml_key($k); my $val = to_toml(\@inline, %param); - push @buff, "$key=$val"; + push @buff_assign, "$key=$val"; } # Table values become an array-of-tables @@ -62,8 +63,8 @@ sub to_toml { push @KEYS, $k; for (@table_array) { - push @buff, '', '[[' . join('.', map{ to_toml_key($_) } @KEYS) . ']]'; - push @buff, to_toml($_); + push @buff_tables, '', '[[' . join('.', map{ to_toml_key($_) } @KEYS) . ']]'; + push @buff_tables, to_toml($_); } pop @KEYS; @@ -75,12 +76,12 @@ sub to_toml { if (!keys(%{$data->{$k}})) { # Empty table my $key = to_toml_key($k); - push @buff, "$key={}"; + push @buff_assign, "$key={}"; } else { # Generate [table] push @KEYS, $k; - push @buff, '', '[' . join('.', map{ to_toml_key($_) } @KEYS) . ']'; - push @buff, to_toml($data->{$k}, %param); + push @buff_tables, '', '[' . join('.', map{ to_toml_key($_) } @KEYS) . ']'; + push @buff_tables, to_toml($data->{$k}, %param); pop @KEYS; } } @@ -92,7 +93,7 @@ sub to_toml { die "toml: found heterogenous array, but strict_arrays is set ($err)\n" unless $ok; } - push @buff, '[' . join(', ', map{ to_toml($_, %param) } @$data) . ']'; + push @buff_tables, '[' . join(', ', map{ to_toml($_, %param) } @$data) . ']'; } when ('SCALAR') { @@ -101,7 +102,7 @@ sub to_toml { } elsif ($$_ eq '0') { return 'false'; } else { - push @buff, to_toml($$_, %param); + push @buff_assign, to_toml($$_, %param); } } @@ -157,7 +158,7 @@ sub to_toml { } } - join "\n", @buff; + join "\n", @buff_assign, @buff_tables; } sub to_toml_key {