MooseX::Meta::TypeConstraint::Structured;
use Moose;
+use Devel::PartialDump;
use Moose::Util::TypeConstraints ();
use MooseX::Meta::TypeCoercion::Structured;
extends 'Moose::Meta::TypeConstraint';
return $self;
};
+=head2 validate
+
+Messing with validate so that we can support niced error messages.
+=cut
+
+override 'validate' => sub {
+ my ($self, @args) = @_;
+ my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
+
+ if ($self->_compiled_type_constraint->(@args, $message)) {
+ ## 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(@args);
+ if(my $message = $message->{message}) {
+ return $self->get_message("$args, Internal Validation Error is: $message");
+ } else {
+ return $self->get_message($args);
+ }
+ }
+};
+
=head2 generate_constraint_for ($type_constraints)
Given some type constraints, use them to generate validation rules for an ref
sub generate_constraint_for {
my ($self, $type_constraints) = @_;
return sub {
- my (@args) = @_;
+ my $arg = shift @_;
my $constraint_generator = $self->constraint_generator;
- return $constraint_generator->($type_constraints, @args);
+ my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
+ return $result;
};
}
return unless $other->isa(__PACKAGE__);
return (
- $self->type_constraints_equals($other)
+ $self->parent->equals($other->parent)
and
- $self->parent->equals( $other->parent )
+ $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);
+ }
+}
+
=head2 type_constraints_equals
Checks to see if the internal type contraints are equal.
=cut
sub type_constraints_equals {
- my ($self, $other) = @_;
+ 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
- || return; ## $other needs the same number of children.
+ my $other_type_constraint = shift @other_type_constraints;
- if( ref $self_type_constraint) {
- $self_type_constraint->equals($other_type_constraint)
- || return; ## type constraints obviously need top be equal
- } else {
- $self_type_constraint eq $other_type_constraint
- || return; ## strings should be equal
- }
+ $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
+ for $self_type_constraint, $other_type_constraint;
+ $self_type_constraint->$op($other_type_constraint) or return;
}
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;
+}
+
=head2 get_message
-May want to override this to set a more useful error message
+Give you a better peek into what's causing the error. For now we stringify the
+incoming deep value with L<Devel::PartialDump> 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);
+};
=head1 SEE ALSO
=cut
-1;
\ No newline at end of file
+__PACKAGE__->meta->make_immutable(inline_constructor => 0);