X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FMeta%2FTypeConstraint%2FStructured.pm;h=25b8c87bc991e427ff46cc51c59ca3db0c6a886a;hb=9448ea2c4ec450d2c5bbf8f10692457cb583facb;hp=e77704a498de25332596228f698e7923aca128ac;hpb=bc64165b6c92dd5a168d0af101facbbfc59f35db;p=gitmo%2FMooseX-Types-Structured.git diff --git a/lib/MooseX/Meta/TypeConstraint/Structured.pm b/lib/MooseX/Meta/TypeConstraint/Structured.pm index e77704a..25b8c87 100644 --- a/lib/MooseX/Meta/TypeConstraint/Structured.pm +++ b/lib/MooseX/Meta/TypeConstraint/Structured.pm @@ -1,165 +1,345 @@ -package MooseX::Meta::TypeConstraint::Structured; +package ## Hide from PAUSE + MooseX::Meta::TypeConstraint::Structured; +# ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints. -use 5.8.8; ## Minimum tested Perl Version use Moose; -use Moose::Util::TypeConstraints; - +use Devel::PartialDump; +use Moose::Util::TypeConstraints (); +use MooseX::Meta::TypeCoercion::Structured; extends 'Moose::Meta::TypeConstraint'; -our $AUTHORITY = 'cpan:JJNAPIORK'; -=head1 NAME +=head1 DESCRIPTION + +A structure is a set of L that are 'aggregated' in +such a way as that they are all applied to an incoming list of arguments. The +idea here is that a Type Constraint could be something like, "An Int followed by +an Int and then a Str" and that this could be done so with a declaration like: + + Tuple[Int,Int,Str]; ## Example syntax -MooseX::Meta::TypeConstraint::Structured - Structured Type Constraints +So a structure is a list of Type constraints (the "Int,Int,Str" in the above +example) which are intended to function together. -=head1 VERSION +=attr type_constraints -0.01 +A list of L objects. =cut -our $VERSION = '0.01'; +has 'type_constraints' => ( + is=>'ro', + isa=>'Ref', + predicate=>'has_type_constraints', +); -=head1 DESCRIPTION +=attr constraint_generator -Structured type constraints let you assign an internal pattern of type -constraints to a 'container' constraint. The goal is to make it easier to -declare constraints like "ArrayRef[Int, Int, Str]" where the constraint is an -ArrayRef of three elements and the internal constraint on the three is Int, Int -and Str. +A subref or closure that contains the way we validate incoming values against +a set of type constraints. -=head1 ATTRIBUTES +=cut -This class defines the following attributes. +has 'constraint_generator' => ( + is=>'ro', + isa=>'CodeRef', + predicate=>'has_constraint_generator', +); -=head2 parent +has coercion => ( + is => 'ro', + isa => 'Object', + builder => '_build_coercion', +); -additional details on the inherited parent attribute +sub _build_coercion { + my ($self) = @_; + return MooseX::Meta::TypeCoercion::Structured->new( + type_constraint => $self, + ); +} -=head2 signature +=method validate -This is a signature of internal contraints for the contents of the outer -contraint container. +Messing with validate so that we can support niced error messages. =cut -subtype 'MooseX::Meta::TypeConstraint::Structured::Signature', - as 'HashRef[Object]', - where { - my %signature = %$_; - foreach my $key (keys %signature) { - $signature{$key}->isa('Moose::Meta::TypeConstraint'); - } 1; - }; - -coerce 'MooseX::Meta::TypeConstraint::Structured::Signature', - from 'ArrayRef[Object]', - via { - my @signature = @$_; - my %hashed_signature = map { $_ => $signature[$_] } 0..$#signature; - \%hashed_signature; +override 'validate' => sub { + my ($self, $value, $message_stack) = @_; + unless ($message_stack) { + $message_stack = MooseX::Types::Structured::MessageStack->new(); + } + + $message_stack->inc_level; + + if ($self->_compiled_type_constraint->($value, $message_stack)) { + ## Everything is good, no error message to return + return undef; + } else { + ## Whoops, need to figure out the right error message + my $args = Devel::PartialDump::dump($value); + $message_stack->dec_level; + if($message_stack->has_messages) { + if($message_stack->level) { + ## we are inside a deeply structured constraint + return $self->get_message($args); + } else { + my $message_str = $message_stack->as_string; + return $self->get_message("$args, Internal Validation Error is: $message_str"); + } + } else { + return $self->get_message($args); + } + } +}; + +=method generate_constraint_for ($type_constraints) + +Given some type constraints, use them to generate validation rules for an ref +of values (to be passed at check time) + +=cut + +sub generate_constraint_for { + my ($self, $type_constraints) = @_; + return sub { + my $arg = shift @_; + my $constraint_generator = $self->constraint_generator; + my $result = $constraint_generator->($type_constraints, $arg, $_[0]); + return $result; }; +} -has 'signature' => ( - is=>'ro', - isa=>'MooseX::Meta::TypeConstraint::Structured::Signature', - coerce=>1, - required=>1, -); +=method parameterize (@type_constraints) + +Given a ref of type constraints, create a structured type. -=head1 METHODS +=cut -This class defines the following methods. +sub parameterize { + my ($self, @type_constraints) = @_; + my $class = ref $self; + my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']'; + my $constraint_generator = $self->__infer_constraint_generator; + + return $class->new( + name => $name, + parent => $self, + type_constraints => \@type_constraints, + constraint_generator => $constraint_generator, + ); +} -=head2 _normalize_args +=method __infer_constraint_generator -Get arguments into a known state or die trying. Ideally we try to make this -into a HashRef so we can match it up with the L HashRef. +This returns a CODEREF which generates a suitable constraint generator. Not +user servicable, you'll never call this directly. =cut -sub _normalize_args { - my ($self, $args) = @_; - if(defined $args) { - if(ref $args eq 'ARRAY') { - return map { $_ => $args->[$_] } (0..$#$args); - } elsif (ref $args eq 'HASH') { - return %$args; - } else { - confess 'Signature must be a reference'; - } +sub __infer_constraint_generator { + my ($self) = @_; + if($self->has_constraint_generator) { + return $self->constraint_generator; } else { - confess 'Signature cannot be empty'; + return sub { + ## I'm not sure about this stuff but everything seems to work + my $tc = shift @_; + my $merged_tc = [@$tc, @{$self->parent->type_constraints}]; + $self->constraint->($merged_tc, @_); + }; } } - -=head2 constraint -The constraint is basically validating the L against the incoming +=method compile_type_constraint + +hook into compile_type_constraint so we can set the correct validation rules. =cut -sub constraint { - my $self = shift; - return sub { - my %args = $self->_normalize_args(shift); - foreach my $idx (keys %{$self->signature}) { - my $type_constraint = $self->signature->{$idx}; - if(my $error = $type_constraint->validate($args{$idx})) { - confess $error; - } - } 1; - }; -} +around 'compile_type_constraint' => sub { + my ($compile_type_constraint, $self, @args) = @_; -=head2 equals + if($self->has_type_constraints) { + my $type_constraints = $self->type_constraints; + my $constraint = $self->generate_constraint_for($type_constraints); + $self->_set_constraint($constraint); + } -modifier to make sure equals descends into the L + return $self->$compile_type_constraint(@args); +}; + +=method create_child_type + +modifier to make sure we get the constraint_generator =cut -around 'equals' => sub { - my ($equals, $self, $compared_type_constraint) = @_; - - ## Make sure we are comparing typeconstraints of the same base class - return unless $compared_type_constraint->isa(__PACKAGE__); - - ## Make sure the base equals is also good - return unless $self->$equals($compared_type_constraint); - - ## Make sure the signatures match - return unless $self->signature_equals($compared_type_constraint); - - ## If we get this far, the two are equal - return 1; +around 'create_child_type' => sub { + my ($create_child_type, $self, %opts) = @_; + return $self->$create_child_type( + %opts, + constraint_generator => $self->__infer_constraint_generator, + ); }; -=head2 signature_equals +=method is_a_type_of + +=method is_subtype_of + +=method equals -Check that the signature equals another signature. +Override the base class behavior. =cut -sub signature_equals { - my ($self, $compared_type_constraint) = @_; - - foreach my $idx (keys %{$self->signature}) { - my $this = $self->signature->{$idx}; - my $that = $compared_type_constraint->signature->{$idx}; - return unless $this->equals($that); +sub equals { + my ( $self, $type_or_name ) = @_; + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + return unless $other->isa(__PACKAGE__); + + return ( + $self->parent->equals($other->parent) + and + $self->type_constraints_equals($other) + ); +} + +sub is_a_type_of { + my ( $self, $type_or_name ) = @_; + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) { + if ( $self->parent->is_a_type_of($other->parent) ) { + return $self->_type_constraints_op_all($other, "is_a_type_of"); + } elsif ( $self->parent->is_a_type_of($other) ) { + return 1; + # FIXME compare? + } else { + return 0; + } + } else { + return $self->SUPER::is_a_type_of($other); + } +} + +sub is_subtype_of { + my ( $self, $type_or_name ) = @_; + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + if ( $other->isa(__PACKAGE__) ) { + if ( $other->type_constraints and $self->type_constraints ) { + if ( $self->parent->is_a_type_of($other->parent) ) { + return ( + $self->_type_constraints_op_all($other, "is_a_type_of") + and + $self->_type_constraints_op_any($other, "is_subtype_of") + ); + } elsif ( $self->parent->is_a_type_of($other) ) { + return 1; + # FIXME compare? + } else { + return 0; + } + } else { + if ( $self->type_constraints ) { + if ( $self->SUPER::is_subtype_of($other) ) { + return 1; + } else { + return; + } + } else { + return $self->parent->is_subtype_of($other->parent); + } + } + } else { + return $self->SUPER::is_subtype_of($other); } - - return 1; } -=head1 AUTHOR +=method type_constraints_equals + +Checks to see if the internal type constraints are equal. + +=cut + +sub type_constraints_equals { + my ( $self, $other ) = @_; + $self->_type_constraints_op_all($other, "equals"); +} + +sub _type_constraints_op_all { + my ($self, $other, $op) = @_; + + return unless $other->isa(__PACKAGE__); + + my @self_type_constraints = @{$self->type_constraints||[]}; + my @other_type_constraints = @{$other->type_constraints||[]}; + + return unless @self_type_constraints == @other_type_constraints; + + ## Incoming ay be either arrayref or hashref, need top compare both + while(@self_type_constraints) { + my $self_type_constraint = shift @self_type_constraints; + my $other_type_constraint = shift @other_type_constraints; + + $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) + for $self_type_constraint, $other_type_constraint; + + my $result = $self_type_constraint->$op($other_type_constraint); + return unless $result; + } + + return 1; ##If we get this far, everything is good. +} + +sub _type_constraints_op_any { + my ($self, $other, $op) = @_; + + return unless $other->isa(__PACKAGE__); + + my @self_type_constraints = @{$self->type_constraints||[]}; + my @other_type_constraints = @{$other->type_constraints||[]}; + + return unless @self_type_constraints == @other_type_constraints; + + ## Incoming ay be either arrayref or hashref, need top compare both + while(@self_type_constraints) { + my $self_type_constraint = shift @self_type_constraints; + my $other_type_constraint = shift @other_type_constraints; + + $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) + for $self_type_constraint, $other_type_constraint; + + return 1 if $self_type_constraint->$op($other_type_constraint); + } + + return 0; +} + +=method get_message + +Give you a better peek into what's causing the error. For now we stringify the +incoming deep value with L and pass that on to either your +custom error message or the default one. In the future we'll try to provide a +more complete stack trace of the actual offending elements + +=cut + +around 'get_message' => sub { + my ($get_message, $self, $value) = @_; + $value = Devel::PartialDump::dump($value) + if ref $value; + return $self->$get_message($value); +}; -John James Napiorkowski +=head1 SEE ALSO -=head1 LICENSE +The following modules or resources may be of interest. -You may distribute this code under the same terms as Perl itself. +L, L =cut -no Moose; 1; +__PACKAGE__->meta->make_immutable(inline_constructor => 0);