all tests passing again after move to message stack
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured.pm
1 package ## Hide from PAUSE
2  MooseX::Meta::TypeConstraint::Structured;
3 # ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
4
5 use Moose;
6 use Devel::PartialDump;
7 use Moose::Util::TypeConstraints ();
8 use MooseX::Meta::TypeCoercion::Structured;
9 extends 'Moose::Meta::TypeConstraint';
10
11
12 =head1 DESCRIPTION
13
14 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
15 such a way as that they are all applied to an incoming list of arguments.  The
16 idea here is that a Type Constraint could be something like, "An Int followed by
17 an Int and then a Str" and that this could be done so with a declaration like:
18
19     Tuple[Int,Int,Str]; ## Example syntax
20
21 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
22 example) which are intended to function together.
23
24 =attr type_constraints
25
26 A list of L<Moose::Meta::TypeConstraint> objects.
27
28 =cut
29
30 has 'type_constraints' => (
31     is=>'ro',
32     isa=>'Ref',
33     predicate=>'has_type_constraints',
34 );
35
36 =attr constraint_generator
37
38 A subref or closure that contains the way we validate incoming values against
39 a set of type constraints.
40
41 =cut
42
43 has 'constraint_generator' => (
44     is=>'ro',
45     isa=>'CodeRef',
46     predicate=>'has_constraint_generator',
47 );
48
49 has coercion => (
50     is      => 'ro',
51     isa     => 'Object',
52     builder => '_build_coercion',
53 );
54
55 sub _build_coercion {
56     my ($self) = @_;
57     return MooseX::Meta::TypeCoercion::Structured->new(
58         type_constraint => $self,
59     );
60 }
61
62 =method validate
63
64 Messing with validate so that we can support niced error messages.
65
66 =cut
67
68 override 'validate' => sub {
69     my ($self, $value, $message_stack) = @_;
70     unless ($message_stack) {
71         $message_stack = MooseX::Types::Structured::MessageStack->new();
72     }
73
74     $message_stack->inc_level;
75
76     if ($self->_compiled_type_constraint->($value, $message_stack)) {
77         ## Everything is good, no error message to return
78         return undef;
79     } else {
80         ## Whoops, need to figure out the right error message
81         my $args = Devel::PartialDump::dump($value);
82         $message_stack->dec_level;
83         if($message_stack->has_messages) {
84             if($message_stack->level) {
85                 ## we are inside a deeply structured constraint
86                 return $self->get_message($args);
87             } else {
88                 my $message_str = $message_stack->as_string;
89                 return $self->get_message("$args, Internal Validation Error is: $message_str");
90             }
91         } else {
92             return $self->get_message($args);
93         }
94     }
95 };
96
97 =method generate_constraint_for ($type_constraints)
98
99 Given some type constraints, use them to generate validation rules for an ref
100 of values (to be passed at check time)
101
102 =cut
103
104 sub generate_constraint_for {
105     my ($self, $type_constraints) = @_;
106     return sub {
107         my $arg =  shift @_;
108         my $constraint_generator = $self->constraint_generator;
109         my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
110         return $result;
111     };
112 }
113
114 =method parameterize (@type_constraints)
115
116 Given a ref of type constraints, create a structured type.
117
118 =cut
119
120 sub parameterize {
121     my ($self, @type_constraints) = @_;
122     my $class = ref $self;
123     my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
124     my $constraint_generator = $self->__infer_constraint_generator;
125
126     return $class->new(
127         name => $name,
128         parent => $self,
129         type_constraints => \@type_constraints,
130         constraint_generator => $constraint_generator,
131     );
132 }
133
134 =method __infer_constraint_generator
135
136 This returns a CODEREF which generates a suitable constraint generator.  Not
137 user servicable, you'll never call this directly.
138
139 =cut
140
141 sub __infer_constraint_generator {
142     my ($self) = @_;
143     if($self->has_constraint_generator) {
144         return $self->constraint_generator;
145     } else {
146         return sub {
147             ## I'm not sure about this stuff but everything seems to work
148             my $tc = shift @_;
149             my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
150             $self->constraint->($merged_tc, @_);
151         };
152     }
153 }
154
155 =method compile_type_constraint
156
157 hook into compile_type_constraint so we can set the correct validation rules.
158
159 =cut
160
161 around 'compile_type_constraint' => sub {
162     my ($compile_type_constraint, $self, @args) = @_;
163
164     if($self->has_type_constraints) {
165         my $type_constraints = $self->type_constraints;
166         my $constraint = $self->generate_constraint_for($type_constraints);
167         $self->_set_constraint($constraint);
168     }
169
170     return $self->$compile_type_constraint(@args);
171 };
172
173 =method create_child_type
174
175 modifier to make sure we get the constraint_generator
176
177 =cut
178
179 around 'create_child_type' => sub {
180     my ($create_child_type, $self, %opts) = @_;
181     return $self->$create_child_type(
182         %opts,
183         constraint_generator => $self->__infer_constraint_generator,
184     );
185 };
186
187 =method is_a_type_of
188
189 =method is_subtype_of
190
191 =method equals
192
193 Override the base class behavior.
194
195 =cut
196
197 sub equals {
198     my ( $self, $type_or_name ) = @_;
199     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
200
201     return unless $other->isa(__PACKAGE__);
202
203     return (
204         $self->parent->equals($other->parent)
205             and
206         $self->type_constraints_equals($other)
207     );
208 }
209
210 sub is_a_type_of {
211     my ( $self, $type_or_name ) = @_;
212     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
213
214     if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
215         if ( $self->parent->is_a_type_of($other->parent) ) {
216             return $self->_type_constraints_op_all($other, "is_a_type_of");
217         } elsif ( $self->parent->is_a_type_of($other) ) {
218             return 1;
219             # FIXME compare?
220         } else {
221             return 0;
222         }
223     } else {
224         return $self->SUPER::is_a_type_of($other);
225     }
226 }
227
228 sub is_subtype_of {
229     my ( $self, $type_or_name ) = @_;
230     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
231     if ( $other->isa(__PACKAGE__) ) {
232         if ( $other->type_constraints and $self->type_constraints ) {
233             if ( $self->parent->is_a_type_of($other->parent) ) {
234                 return (
235                     $self->_type_constraints_op_all($other, "is_a_type_of")
236                       and
237                     $self->_type_constraints_op_any($other, "is_subtype_of")
238                 );
239             } elsif ( $self->parent->is_a_type_of($other) ) {
240                 return 1;
241                 # FIXME compare?
242             } else {
243                 return 0;
244             }
245         } else {
246             if ( $self->type_constraints ) {
247                 if ( $self->SUPER::is_subtype_of($other) ) {
248                     return 1;
249                 } else {
250                     return;
251                 }
252             } else {
253                 return $self->parent->is_subtype_of($other->parent);
254             }
255         }
256     } else {
257         return $self->SUPER::is_subtype_of($other);
258     }
259 }
260
261 =method type_constraints_equals
262
263 Checks to see if the internal type constraints are equal.
264
265 =cut
266
267 sub type_constraints_equals {
268     my ( $self, $other ) = @_;
269     $self->_type_constraints_op_all($other, "equals");
270 }
271
272 sub _type_constraints_op_all {
273     my ($self, $other, $op) = @_;
274
275     return unless $other->isa(__PACKAGE__);
276
277     my @self_type_constraints = @{$self->type_constraints||[]};
278     my @other_type_constraints = @{$other->type_constraints||[]};
279
280     return unless @self_type_constraints == @other_type_constraints;
281
282     ## Incoming ay be either arrayref or hashref, need top compare both
283     while(@self_type_constraints) {
284         my $self_type_constraint = shift @self_type_constraints;
285         my $other_type_constraint = shift @other_type_constraints;
286
287         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
288           for $self_type_constraint, $other_type_constraint;
289
290         my $result = $self_type_constraint->$op($other_type_constraint);
291         return unless $result;
292     }
293
294     return 1; ##If we get this far, everything is good.
295 }
296
297 sub _type_constraints_op_any {
298     my ($self, $other, $op) = @_;
299
300     return unless $other->isa(__PACKAGE__);
301
302     my @self_type_constraints = @{$self->type_constraints||[]};
303     my @other_type_constraints = @{$other->type_constraints||[]};
304
305     return unless @self_type_constraints == @other_type_constraints;
306
307     ## Incoming ay be either arrayref or hashref, need top compare both
308     while(@self_type_constraints) {
309         my $self_type_constraint = shift @self_type_constraints;
310         my $other_type_constraint = shift @other_type_constraints;
311
312         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
313           for $self_type_constraint, $other_type_constraint;
314
315         return 1 if $self_type_constraint->$op($other_type_constraint);
316     }
317
318     return 0;
319 }
320
321 =method get_message
322
323 Give you a better peek into what's causing the error.  For now we stringify the
324 incoming deep value with L<Devel::PartialDump> and pass that on to either your
325 custom error message or the default one.  In the future we'll try to provide a
326 more complete stack trace of the actual offending elements
327
328 =cut
329
330 around 'get_message' => sub {
331     my ($get_message, $self, $value) = @_;
332     $value = Devel::PartialDump::dump($value)
333      if ref $value;
334     return $self->$get_message($value);
335 };
336
337 =head1 SEE ALSO
338
339 The following modules or resources may be of interest.
340
341 L<Moose>, L<Moose::Meta::TypeConstraint>
342
343 =cut
344
345 __PACKAGE__->meta->make_immutable(inline_constructor => 0);