From 95470a2f94bfd477c0ec4ed9282a6acd26074588 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 15 May 2013 11:32:34 +0200 Subject: [PATCH] Request::flatten sortiert jetzt Hashes nach keys. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Die Änderung war notwendig, weil in perl 5.18 die Hashrandomisierung deutlich aggressiver ist. Flatten war vorher schon auf dem Papier nicht deterministisch, das ist aber durch die Randomisierung von perl 5.10 bis 5.16 nie aufgefallen. Der neue Algorithmus sorgt dafür dass die Datenstruktur a => [ { b => 1, c => 2, } ] sowohl als a[+].b = 1 a[].c = 2 als auch als a[+].c = 2 a[].b = 1 zurückkommen kann, je nach Reihenfolge die keys zurückgibt. Ab jetzt wird immer die erste Version forciert. --- SL/Request.pm | 2 +- t/request/flatten.t | 46 ++++++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/SL/Request.pm b/SL/Request.pm index 4479bd73a..ec479f8e4 100644 --- a/SL/Request.pm +++ b/SL/Request.pm @@ -312,7 +312,7 @@ sub flatten { for (ref $source) { /^HASH$/ && do { my $first = 1; - for my $key (keys %$source) { + for my $key (sort keys %$source) { flatten($source->{$key} => $target, (defined $prefix ? $prefix . $arr_prefix->($first) . '.' : '') . $key); $first = 0; }; diff --git a/t/request/flatten.t b/t/request/flatten.t index 0cfd65f28..590f5cf28 100644 --- a/t/request/flatten.t +++ b/t/request/flatten.t @@ -1,5 +1,5 @@ use Test::More; -use Test::Deep; +use Test::Deep qw(cmp_deeply); use Data::Dumper; use_ok 'SL::Request', qw(flatten unflatten); @@ -13,8 +13,8 @@ sub f ($$$) { my $unflat = unflatten($flat); print Dumper($unflat) if DEBUG; - cmp_deeply($flat, $_[1], $_[2]); - cmp_deeply($unflat, $_[0], $_[2]); + cmp_deeply($flat, $_[1], $_[2] . " flatten"); + cmp_deeply($unflat, $_[0], $_[2] . " unflatten"); } f { @@ -55,12 +55,12 @@ f { 'q' => 4 }, } -}, bag( +}, [ [ 'x' => 1, ], [ 'y.a' => 2, ], [ 'y.b.p' => 3, ], [ 'y.b.q' => 4 ], -), 'Hash::Flatten 1'; +], 'Hash::Flatten 1'; f { @@ -70,13 +70,13 @@ f { }, 'a' => [1,2,3], }, -bag ( - ['x' => 1, ], +[ ['0.1' => 2, ], ['a[]' => 1, ], ['a[]' => 2, ], ['a[]' => 3, ], -), 'Hash::Flatten 2 - weird keys and values'; + ['x' => 1, ], +], 'Hash::Flatten 2 - weird keys and values'; f { @@ -95,15 +95,15 @@ f { }, ] }, -bag( +[ + [ 'ay.a' => 2, ], [ 'ay.b.p' => 3, ], [ 'ay.b.q' => 4, ], - [ 'ay.a' => 2, ], [ 'x' => 1, ], [ 'y[]' => 'a', ], [ 'y[]' => 2 ], [ 'y[+].baz' => 'bum', ], -), 'Hash::Flatten 3 - mixed'; +], 'Hash::Flatten 3 - mixed'; f { 'x' => 1, @@ -117,17 +117,17 @@ f { 'money', ] }, -bag( +[ [ 'x' => 1, ], - [ 'y[][]' => 'his', ], - [ 'y[][+][]' => 'parted', ], - [ 'y[][][]' => 'from', ], [ 'y[+][]' => 'a', ], - [ 'y[+][]' => 'easily', ], [ 'y[][]' => 'fool', ], [ 'y[][]' => 'is' ], + [ 'y[+][]' => 'easily', ], + [ 'y[][+][]' => 'parted', ], + [ 'y[][][]' => 'from', ], + [ 'y[][]' => 'his', ], [ 'y[]' => 'money', ], -), 'Hash::Flatten 4 - array nesting'; +], 'Hash::Flatten 4 - array nesting'; f { 'x' => 1, @@ -145,15 +145,15 @@ f { }, ] }, -bag( - [ 'x' => 1, ], - [ 's' => 'hey', ], +[ [ 'ay.a' => 2, ], - [ 'y[+].baz' => 'bum', ], [ 'ay.b.p' => 3, ], - [ 'y[]' => 'a', ], [ 'ay.b.q' => 4, ], + [ 's' => 'hey', ], + [ 'x' => 1, ], + [ 'y[]' => 'a', ], [ 'y[]' => 2 ], -), 'Hash::Flatten 5 - deep mix'; + [ 'y[+].baz' => 'bum', ], +], 'Hash::Flatten 5 - deep mix'; done_testing(); -- 2.20.1