Subject: | type constraint 'Maybe' isn't handled |
MooseX::Storage says:
"Cannot handle type constraint (Maybe[Str]) at
[snip]/MooseX/Storage/Engine.pm line 333"
Whenever you try to serialize an object with an attribute that has a
type that includes 'Maybe'.
This is probably just a special case of this already reported bug about
union or parameterised types:
http://rt.cpan.org/Ticket/Display.html?id=41025
When a fix comes along for bug 41025 I'm guessing it will take care of
this issue as well. However, in the mean time, since 'Maybe' is an easy
case and is a built-in constraint it seems like it's worth handling
specifically. I've attached a patch that does that. Test cases are
attached as well.
Note: This patch doesn't handle:
isa => 'Maybe[Foo]'
I couldn't figure out how to do that quickly so I left it out. The
related tests are commented out.
Subject: | 002_basic_w_maybes.t |
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 19;
BEGIN {
use_ok('MooseX::Storage');
}
{
package Foo;
use Moose;
use MooseX::Storage;
with Storage;
has 'number' => ( is => 'ro', isa => 'Maybe[Int]' );
has 'string' => ( is => 'ro', isa => 'Maybe[Str]' );
has 'boolean' => ( is => 'ro', isa => 'Maybe[Bool]' );
has 'float' => ( is => 'ro', isa => 'Maybe[Num]' );
has 'object' => ( is => 'ro', isa => 'Maybe[Object]' );
has 'foo' => ( is => 'ro', isa => 'Maybe[Foo]' );
}
#
# maybes defined
#
{
my $foo = Foo->new(
number => 10,
string => 'foo',
boolean => 1,
float => 10.5,
object => Foo->new( number => 2 ),
#foo => Foo->new( number => 3 ),
);
isa_ok( $foo, 'Foo' );
is_deeply(
$foo->pack,
{
__CLASS__ => 'Foo',
number => 10,
string => 'foo',
boolean => 1,
float => 10.5,
object => {
__CLASS__ => 'Foo',
number => 2
},
#foo => {
# __CLASS__ => 'Foo',
# number => 3
# },
},
'... got the right frozen class'
);
}
{
my $foo = Foo->unpack(
{
__CLASS__ => 'Foo',
number => 10,
string => 'foo',
boolean => 1,
float => 10.5,
object => {
__CLASS__ => 'Foo',
number => 2
},
#foo => {
# __CLASS__ => 'Foo',
# number => 3
# },
}
);
isa_ok( $foo, 'Foo' );
is( $foo->number, 10, '... got the right number' );
is( $foo->string, 'foo', '... got the right string' );
ok( $foo->boolean, '... got the right boolean' );
is( $foo->float, 10.5, '... got the right float' );
isa_ok( $foo->object, 'Foo' );
is( $foo->object->number, 2,
'... got the right number (in the embedded object)' );
#isa_ok( $foo->foo, 'Foo' );
#is( $foo->object->number, 3,
# '... got the right number (in the embedded object)' );
}
#
# 'Maybe' properties undefined
#
{
my $foo = Foo->new();
isa_ok( $foo, 'Foo' );
is_deeply(
$foo->pack,
{
__CLASS__ => 'Foo',
},
'... got the right frozen class'
);
}
{
my $foo = Foo->unpack(
{
__CLASS__ => 'Foo',
}
);
isa_ok( $foo, 'Foo' );
is( $foo->number, undef, '... got the right number' );
is( $foo->string, undef, '... got the right string' );
is( $foo->boolean, undef, '... got the right boolean' );
is( $foo->float, undef, '... got the right float' );
is( $foo->object, undef, '... got the right object');
is( $foo->foo, undef, '... got the right object');
}
Subject: | handle_maybes.patch |
diff -ur MooseX-Storage-0.15/lib/MooseX/Storage/Engine.pm MooseX-Storage/lib/MooseX/Storage/Engine.pm
--- MooseX-Storage-0.15/lib/MooseX/Storage/Engine.pm 2008-09-29 08:29:20.000000000 -0500
+++ MooseX-Storage/lib/MooseX/Storage/Engine.pm 2009-02-09 16:03:07.898887000 -0600
@@ -302,6 +302,12 @@
return $TYPES{$type_constraint->name}
if exists $TYPES{$type_constraint->name};
+ # this should handle most type usages
+ # that are wrapped in a 'Maybe'
+ return $TYPES{$type_constraint->type_parameter}
+ if $type_constraint->is_a_type_of('Maybe') &&
+ exists $TYPES{$type_constraint->type_parameter};
+
# the next possibility is they are
# a subtype of the built-in types,
# in which case this will DWIM in
Only in MooseX-Storage/t: 002_basic_w_maybes.t