1 package ## Hide from PAUSE
2 MooseX::Meta::TypeConstraint::Structured;
5 use Devel::PartialDump;
6 use Moose::Util::TypeConstraints ();
7 use MooseX::Meta::TypeCoercion::Structured;
8 extends 'Moose::Meta::TypeConstraint';
12 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
16 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
17 such a way as that they are all applied to an incoming list of arguments. The
18 idea here is that a Type Constraint could be something like, "An Int followed by
19 an Int and then a Str" and that this could be done so with a declaration like:
21 Tuple[Int,Int,Str]; ## Example syntax
23 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
24 example) which are intended to function together.
28 This class defines the following attributes.
30 =head2 type_constraints
32 A list of L<Moose::Meta::TypeConstraint> objects.
36 has 'type_constraints' => (
39 predicate=>'has_type_constraints',
42 =head2 constraint_generator
44 A subref or closure that contains the way we validate incoming values against
45 a set of type constraints.
49 has 'constraint_generator' => (
52 predicate=>'has_constraint_generator',
58 builder => '_build_coercion',
63 This class defines the following methods.
73 return MooseX::Meta::TypeCoercion::Structured->new(
74 type_constraint => $self,
80 Messing with validate so that we can support niced error messages.
83 override 'validate' => sub {
84 my ($self, @args) = @_;
85 my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
87 if ($self->_compiled_type_constraint->(@args, $message)) {
88 ## Everything is good, no error message to return
91 ## Whoops, need to figure out the right error message
92 my $args = Devel::PartialDump::dump(@args);
93 if(my $message = $message->{message}) {
94 return $self->get_message("$args, Internal Validation Error is: $message");
96 return $self->get_message($args);
101 =head2 generate_constraint_for ($type_constraints)
103 Given some type constraints, use them to generate validation rules for an ref
104 of values (to be passed at check time)
108 sub generate_constraint_for {
109 my ($self, $type_constraints) = @_;
112 my $constraint_generator = $self->constraint_generator;
113 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
118 =head2 parameterize (@type_constraints)
120 Given a ref of type constraints, create a structured type.
126 my ($self, @type_constraints) = @_;
127 my $class = ref $self;
128 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
129 my $constraint_generator = $self->__infer_constraint_generator;
134 type_constraints => \@type_constraints,
135 constraint_generator => $constraint_generator,
139 =head2 __infer_constraint_generator
141 This returns a CODEREF which generates a suitable constraint generator. Not
142 user servicable, you'll never call this directly.
146 sub __infer_constraint_generator {
148 if($self->has_constraint_generator) {
149 return $self->constraint_generator;
152 ## I'm not sure about this stuff but everything seems to work
154 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
155 $self->constraint->($merged_tc, @_);
160 =head2 compile_type_constraint
162 hook into compile_type_constraint so we can set the correct validation rules.
166 around 'compile_type_constraint' => sub {
167 my ($compile_type_constraint, $self, @args) = @_;
169 if($self->has_type_constraints) {
170 my $type_constraints = $self->type_constraints;
171 my $constraint = $self->generate_constraint_for($type_constraints);
172 $self->_set_constraint($constraint);
175 return $self->$compile_type_constraint(@args);
178 =head2 create_child_type
180 modifier to make sure we get the constraint_generator
184 around 'create_child_type' => sub {
185 my ($create_child_type, $self, %opts) = @_;
186 return $self->$create_child_type(
188 constraint_generator => $self->__infer_constraint_generator,
198 Override the base class behavior.
203 my ( $self, $type_or_name ) = @_;
204 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
206 return unless $other->isa(__PACKAGE__);
209 $self->parent->equals($other->parent)
211 $self->type_constraints_equals($other)
216 my ( $self, $type_or_name ) = @_;
217 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
219 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
220 if ( $self->parent->is_a_type_of($other->parent) ) {
221 return $self->_type_constraints_op_all($other, "is_a_type_of");
222 } elsif ( $self->parent->is_a_type_of($other) ) {
229 return $self->SUPER::is_a_type_of($other);
234 my ( $self, $type_or_name ) = @_;
236 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
238 if ( $other->isa(__PACKAGE__) ) {
239 if ( $other->type_constraints and $self->type_constraints ) {
240 if ( $self->parent->is_a_type_of($other->parent) ) {
242 $self->_type_constraints_op_all($other, "is_a_type_of")
244 $self->_type_constraints_op_any($other, "is_subtype_of")
246 } elsif ( $self->parent->is_a_type_of($other) ) {
253 if ( $self->type_constraints ) {
254 if ( $self->SUPER::is_subtype_of($other) ) {
260 return $self->parent->is_subtype_of($other->parent);
264 return $self->SUPER::is_subtype_of($other);
268 =head2 type_constraints_equals
270 Checks to see if the internal type constraints are equal.
274 sub type_constraints_equals {
275 my ( $self, $other ) = @_;
276 $self->_type_constraints_op_all($other, "equals");
279 sub _type_constraints_op_all {
280 my ($self, $other, $op) = @_;
282 return unless $other->isa(__PACKAGE__);
284 my @self_type_constraints = @{$self->type_constraints||[]};
285 my @other_type_constraints = @{$other->type_constraints||[]};
287 return unless @self_type_constraints == @other_type_constraints;
289 ## Incoming ay be either arrayref or hashref, need top compare both
290 while(@self_type_constraints) {
291 my $self_type_constraint = shift @self_type_constraints;
292 my $other_type_constraint = shift @other_type_constraints;
294 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
295 for $self_type_constraint, $other_type_constraint;
297 $self_type_constraint->$op($other_type_constraint) or return;
300 return 1; ##If we get this far, everything is good.
303 sub _type_constraints_op_any {
304 my ($self, $other, $op) = @_;
306 return unless $other->isa(__PACKAGE__);
308 my @self_type_constraints = @{$self->type_constraints||[]};
309 my @other_type_constraints = @{$other->type_constraints||[]};
311 return unless @self_type_constraints == @other_type_constraints;
313 ## Incoming ay be either arrayref or hashref, need top compare both
314 while(@self_type_constraints) {
315 my $self_type_constraint = shift @self_type_constraints;
316 my $other_type_constraint = shift @other_type_constraints;
318 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
319 for $self_type_constraint, $other_type_constraint;
321 return 1 if $self_type_constraint->$op($other_type_constraint);
329 Give you a better peek into what's causing the error. For now we stringify the
330 incoming deep value with L<Devel::PartialDump> and pass that on to either your
331 custom error message or the default one. In the future we'll try to provide a
332 more complete stack trace of the actual offending elements
336 around 'get_message' => sub {
337 my ($get_message, $self, $value) = @_;
338 $value = Devel::PartialDump::dump($value)
340 return $self->$get_message($value);
345 The following modules or resources may be of interest.
347 L<Moose>, L<Moose::Meta::TypeConstraint>
351 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
353 =head1 COPYRIGHT & LICENSE
355 This program is free software; you can redistribute it and/or modify
356 it under the same terms as Perl itself.
360 __PACKAGE__->meta->make_immutable(inline_constructor => 0);