package MooseX::Types::Structured;
use 5.008;
+
use Moose::Util::TypeConstraints;
use MooseX::Meta::TypeConstraint::Structured;
use MooseX::Types -declare => [qw(Dict Tuple Optional)];
+use Sub::Exporter -setup => { exports => [ qw(Dict Tuple Optional slurpy) ] };
our $VERSION = '0.07';
our $AUTHORITY = 'cpan:JJNAPIORK';
Moose::Util::TypeConstraints::add_parameterizable_type($Optional);
}
+sub slurpy($) {
+ my $tc = shift @_;
+ ## we don't want to force the TC to be a Moose::Meta::TypeConstraint, we
+ ## just want to make sure it provides the minimum needed bits to function.
+ if($tc and ref $tc and $tc->can('check') and $tc->can('is_subtype_of') ) {
+ return sub {
+ if($tc->is_subtype_of('HashRef')) {
+ return $tc->check(+{@_});
+ } elsif($tc->is_subtype_of('ArrayRef')) {
+ return $tc->check([@_]);
+ } else {
+ return;
+ }
+ };
+ } else {
+ ## For now just pass it all to check and cross our fingers
+ return sub {
+ return $tc->check(@_);
+ };
+ }
+}
=head1 SEE ALSO
}
use Moose::Util::TypeConstraints;
-use MooseX::Types::Structured qw(Dict Tuple);
-use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
-
-
-sub merge(&$) {
- my ($code, $tc) = @_;
- return sub {
- my @tail_args = @_;
- $tc->check($code->(@tail_args));
- }
-}
+use MooseX::Types::Structured qw(Dict Tuple slurpy);
+use MooseX::Types::Moose qw(Int Str ArrayRef HashRef Object);
my $array_tailed_tuple =
subtype 'array_tailed_tuple',
as Tuple[
Int,
Str,
- merge {[@_]} ArrayRef[Int],
+ slurpy ArrayRef[Int],
];
ok !$array_tailed_tuple->check(['ss',1]), 'correct fail';
as Tuple[
Int,
Str,
- merge {+{@_}} HashRef[Int],
+ slurpy HashRef[Int],
];
ok !$hash_tailed_tuple->check(['ss',1]), 'correct fail';
as Dict[
name=>Str,
age=>Int,
- merge {+{@_}} HashRef[Int],
+ slurpy HashRef[Int],
];
ok !$hash_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail';
as Dict[
name=>Str,
age=>Int,
- merge {[@_]} ArrayRef[Int],
+ slurpy ArrayRef[Int],
];
ok !$array_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail';
ok !$array_tailed_dict->check([]), 'correct fail';
ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1,2}), 'correct pass with tail';
ok !$array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1, "hello"}), 'correct fail with tail';
+
+my $insane_tc =
+ subtype 'insane_tc',
+ as Tuple[
+ Object,
+ slurpy Dict[
+ name=>Str,
+ age=>Int,
+ slurpy ArrayRef[Int],
+ ]
+ ];
+
+ok $insane_tc->check([$insane_tc, name=>"John", age=>25, 1,2,3]),
+ 'validated: [$insane_tc, name=>"John", age=>25, 1,2,3]';
\ No newline at end of file