Skip to content

Commit 9ae29a6

Browse files
Merge branch 'pr/7'
This closes #7, Deep nested objects fix (RT Bug #81236) Wrong packing/unpacking for nested objects ```perl my $obj = StorableClass->new( h => { a => [ StorableClass->new(s => 'value'), ... ], ... } ); my $hashref = $obj->pack; ``` Packs into: ```perl { __CLASS__ => 'StorableClass', h => { a => [ bless({s => 'value'}, 'StorableClass') ] # Blessed! XXX } } ```
2 parents cd26b19 + eccec8b commit 9ae29a6

File tree

2 files changed

+569
-8
lines changed

2 files changed

+569
-8
lines changed

lib/MooseX/Storage/Engine.pm

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ my %OBJECT_HANDLERS = (
211211
);
212212

213213

214-
my %TYPES = (
214+
my %TYPES;
215+
%TYPES = (
215216
# NOTE:
216217
# we need to make sure that we properly numify the numbers
217218
# before and after them being futzed with, because some of
@@ -237,9 +238,17 @@ my %TYPES = (
237238
expand => sub {
238239
my ( $array, @args ) = @_;
239240
foreach my $i (0 .. $#{$array}) {
240-
next unless ref($array->[$i]) eq 'HASH'
241-
&& exists $array->[$i]->{$CLASS_MARKER};
242-
$array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
241+
if (ref($array->[$i]) eq 'HASH') {
242+
if (exists($array->[$i]{$CLASS_MARKER})) {
243+
$array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
244+
}
245+
else {
246+
$array->[$i] = $TYPES{HashRef}{expand}->($array->[$i], @args);
247+
}
248+
}
249+
elsif (ref($array->[$i]) eq 'ARRAY') {
250+
$array->[$i] = $TYPES{ArrayRef}{expand}->($array->[$i], @args);
251+
}
243252
}
244253
$array;
245254
},
@@ -252,6 +261,8 @@ my %TYPES = (
252261
[ map {
253262
blessed($_)
254263
? $OBJECT_HANDLERS{collapse}->($_, @args)
264+
: $TYPES{ref($_)}
265+
? $TYPES{ref($_)}->{collapse}->($_, @args)
255266
: $_
256267
} @$array ]
257268
}
@@ -260,9 +271,17 @@ my %TYPES = (
260271
expand => sub {
261272
my ( $hash, @args ) = @_;
262273
foreach my $k (keys %$hash) {
263-
next unless ref($hash->{$k}) eq 'HASH'
264-
&& exists $hash->{$k}->{$CLASS_MARKER};
265-
$hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
274+
if (ref($hash->{$k}) eq 'HASH' ) {
275+
if (exists($hash->{$k}->{$CLASS_MARKER})) {
276+
$hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
277+
}
278+
else {
279+
$hash->{$k} = $TYPES{HashRef}{expand}->($hash->{$k}, @args);
280+
}
281+
}
282+
elsif (ref($hash->{$k}) eq 'ARRAY') {
283+
$hash->{$k} = $TYPES{ArrayRef}{expand}->($hash->{$k}, @args);
284+
}
266285
}
267286
$hash;
268287
},
@@ -275,6 +294,8 @@ my %TYPES = (
275294
+{ map {
276295
blessed($hash->{$_})
277296
? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
297+
: $TYPES{ref($hash->{$_})}
298+
? ($_ => $TYPES{ref($hash->{$_})}{collapse}->($hash->{$_}, @args))
278299
: ($_ => $hash->{$_})
279300
} keys %$hash }
280301
}
@@ -290,6 +311,13 @@ my %TYPES = (
290311
#}
291312
);
292313

314+
%TYPES = (
315+
%TYPES,
316+
'HASH' => $TYPES{HashRef},
317+
'ARRAY' => $TYPES{ArrayRef},
318+
);
319+
320+
293321
sub add_custom_type_handler {
294322
my ($self, $type_name, %handlers) = @_;
295323
(exists $handlers{expand} && exists $handlers{collapse})

0 commit comments

Comments
 (0)