chiark / gitweb /
TOML::Tiny: Fix incorrect writing algorithm
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Jun 2020 23:20:12 +0000 (00:20 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Jun 2020 23:20:12 +0000 (00:20 +0100)
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 <ijackson@chiark.greenend.org.uk>
TOML-Tiny/lib/TOML/Tiny/Writer.pm

index 4e2275add7c5fead85b3ea332dda437dcc96c553..2d2b79b107811d1a77568a83d7b2bb37718f232b 100644 (file)
@@ -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 {