From 2222dd7bf24df5081180c3c737c0297477d5b440 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 08:17:19 +0100 Subject: [PATCH 1/7] stop assuming that "$type_constraint" and $type_constraint->name are the same thing; they are not in Type::Tiny and Specio --- lib/MooseX/Storage/Engine.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 5f4f680..aedbce4 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -317,8 +317,8 @@ sub find_type_handler { # this should handle most type usages # since they they are usually just # the standard set of built-ins - return $TYPES{$type_constraint->name} - if exists $TYPES{$type_constraint->name}; + return $TYPES{$type_constraint} + if exists $TYPES{$type_constraint}; # the next possibility is they are # a subtype of the built-in types, From 407308a0cf9f739c042f45a55f50fa4762a9ed49 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 08:28:28 +0100 Subject: [PATCH 2/7] test case --- t/006_w_custom_type_handlers_typetiny.t | 99 +++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 t/006_w_custom_type_handlers_typetiny.t diff --git a/t/006_w_custom_type_handlers_typetiny.t b/t/006_w_custom_type_handlers_typetiny.t new file mode 100644 index 0000000..03a95a5 --- /dev/null +++ b/t/006_w_custom_type_handlers_typetiny.t @@ -0,0 +1,99 @@ +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Deep; +use Test::Fatal; + +use Test::Requires { + 'Types::Standard' => '0.008', +}; + +BEGIN { + use_ok('MooseX::Storage'); + use_ok('MooseX::Storage::Engine'); +} + +require Types::Standard; + +=pod + +This is just a simple example of defining +a custom type handler to take care of custom +inflate and deflate needs. + +=cut + +{ + package Bar; + use Moose; + + has 'baz' => (is => 'rw', isa => 'Str'); + has 'boo' => (is => 'rw', isa => 'Str'); + + sub encode { + my $self = shift; + $self->baz . '|' . $self->boo; + } + + sub decode { + my ($class, $packed) = @_; + my ($baz, $boo) = split /\|/ => $packed; + $class->new( + baz => $baz, + boo => $boo, + ); + } + + MooseX::Storage::Engine->add_custom_type_handler( + Types::Standard::InstanceOf['Bar'] => ( + expand => sub { Bar->decode(shift) }, + collapse => sub { (shift)->encode }, + ) + ); + + package Foo; + use Moose; + use MooseX::Storage; + + with Storage; + + has 'bar' => ( + is => 'ro', + isa => Types::Standard::InstanceOf['Bar'], + default => sub { + Bar->new(baz => 'BAZ', boo => 'BOO') + } + ); +} + +my $foo = Foo->new; +isa_ok($foo, 'Foo'); + +isa_ok($foo->bar, 'Bar'); + +cmp_deeply( +$foo->pack, +{ + __CLASS__ => "Foo", + bar => "BAZ|BOO", +}, +'... got correct packed structure'); + +{ + my $foo = Foo->unpack({ + __CLASS__ => "Foo", + bar => "BAZ|BOO", + }); + isa_ok($foo, 'Foo'); + + isa_ok($foo->bar, 'Bar'); + + is($foo->bar->baz, 'BAZ', '... got the right stuff'); + is($foo->bar->boo, 'BOO', '... got the right stuff'); +} + + + + + From 96d503332a2812684fa35113e6a0f81ab7c87080 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 08:38:18 +0100 Subject: [PATCH 3/7] changelog --- Changes | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changes b/Changes index 44c9dc3..158d989 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,9 @@ Revision history for {{$dist->name}} {{$NEXT}} + - consistently use stringification (i.e. "" overload) of type constraint + objects in MooseX::Storage::Engine, instead of sometimes calling 'name' + method. 0.43 2013-09-11 01:47:40Z (Karen Etheridge) - removed use of deprecated Class::MOP::load_class From 108da2dd9ab7c1b54954f247df73239bfa91e370 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 08:55:45 +0100 Subject: [PATCH 4/7] test Specio too --- t/006_w_custom_type_handlers_specio.t | 103 ++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 t/006_w_custom_type_handlers_specio.t diff --git a/t/006_w_custom_type_handlers_specio.t b/t/006_w_custom_type_handlers_specio.t new file mode 100644 index 0000000..8681fee --- /dev/null +++ b/t/006_w_custom_type_handlers_specio.t @@ -0,0 +1,103 @@ +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Deep; +use Test::Fatal; + +use Test::Requires { + 'Specio::Declare' => '0.08', +}; +use Test::Requires { + 'Moose' => '2.1000', +}; + +BEGIN { + use_ok('MooseX::Storage'); + use_ok('MooseX::Storage::Engine'); +} + +use Specio::Declare 'object_isa_type'; +my $BAR = object_isa_type('Bar'); + +=pod + +This is just a simple example of defining +a custom type handler to take care of custom +inflate and deflate needs. + +=cut + +{ + package Bar; + use Moose; + + has 'baz' => (is => 'rw', isa => 'Str'); + has 'boo' => (is => 'rw', isa => 'Str'); + + sub encode { + my $self = shift; + $self->baz . '|' . $self->boo; + } + + sub decode { + my ($class, $packed) = @_; + my ($baz, $boo) = split /\|/ => $packed; + $class->new( + baz => $baz, + boo => $boo, + ); + } + + MooseX::Storage::Engine->add_custom_type_handler( + $BAR => ( + expand => sub { Bar->decode(shift) }, + collapse => sub { (shift)->encode }, + ) + ); + + package Foo; + use Moose; + use MooseX::Storage; + + with Storage; + + has 'bar' => ( + is => 'ro', + isa => $BAR, + default => sub { + Bar->new(baz => 'BAZ', boo => 'BOO') + } + ); +} + +my $foo = Foo->new; +isa_ok($foo, 'Foo'); + +isa_ok($foo->bar, 'Bar'); + +cmp_deeply( +$foo->pack, +{ + __CLASS__ => "Foo", + bar => "BAZ|BOO", +}, +'... got correct packed structure'); + +{ + my $foo = Foo->unpack({ + __CLASS__ => "Foo", + bar => "BAZ|BOO", + }); + isa_ok($foo, 'Foo'); + + isa_ok($foo->bar, 'Bar'); + + is($foo->bar->baz, 'BAZ', '... got the right stuff'); + is($foo->bar->boo, 'BOO', '... got the right stuff'); +} + + + + + From cacf343b4a0140e0e35c4bc8656f0aa665a9ea93 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 08:58:07 +0100 Subject: [PATCH 5/7] this fixes an occasional bug using Specio, because Specio does not provide an is_subtype_of method --- lib/MooseX/Storage/Engine.pm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index aedbce4..a7d40ca 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -327,9 +327,11 @@ sub find_type_handler { # 100% ideal though, but until I # come up with a decent test case # it will do for now. - foreach my $type (keys %TYPES) { - return $TYPES{$type} - if $type_constraint->is_subtype_of($type); + if ($type_constraint->can('is_subtype_of') { + foreach my $type (keys %TYPES) { + return $TYPES{$type} + if $type_constraint->is_subtype_of($type); + } } # NOTE: From 150bc08eacf60eec8fdc5be64fa5faca9f1f4b01 Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 11:23:07 +0100 Subject: [PATCH 6/7] fix syntax error --- lib/MooseX/Storage/Engine.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index a7d40ca..846d8cc 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -327,7 +327,7 @@ sub find_type_handler { # 100% ideal though, but until I # come up with a decent test case # it will do for now. - if ($type_constraint->can('is_subtype_of') { + if ($type_constraint->can('is_subtype_of')) { foreach my $type (keys %TYPES) { return $TYPES{$type} if $type_constraint->is_subtype_of($type); From b0d5f6c288b648e47ba3201151817c49e420382c Mon Sep 17 00:00:00 2001 From: Toby Inkster Date: Fri, 13 Sep 2013 11:23:47 +0100 Subject: [PATCH 7/7] Specio does provide an is_a_type_of method (like Moose and Type::Tiny do), so use that instead. --- lib/MooseX/Storage/Engine.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 846d8cc..0a0fd36 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -327,10 +327,10 @@ sub find_type_handler { # 100% ideal though, but until I # come up with a decent test case # it will do for now. - if ($type_constraint->can('is_subtype_of')) { + if ($type_constraint->can('is_a_type_of')) { foreach my $type (keys %TYPES) { return $TYPES{$type} - if $type_constraint->is_subtype_of($type); + if $type_constraint->is_a_type_of($type); } }