0db946f9a5319d265867b26ff51c480ab8710575
[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 sub _clean_message {
69     my $message = shift @_;
70     $message =~s/MooseX::Types::Structured:://g;
71     return $message;
72 }
73
74 override 'validate' => sub {
75     my ($self, $value, $message_stack) = @_;
76     unless ($message_stack) {
77         $message_stack = MooseX::Types::Structured::MessageStack->new();
78     }
79
80     $message_stack->inc_level;
81
82     if ($self->_compiled_type_constraint->($value, $message_stack)) {
83         ## Everything is good, no error message to return
84         return undef;
85     } else {
86         ## Whoops, need to figure out the right error message
87         my $args = Devel::PartialDump::dump($value);
88         $message_stack->dec_level;
89         if($message_stack->has_messages) {
90             if($message_stack->level) {
91                 ## we are inside a deeply structured constraint
92                 return $self->get_message($args);
93             } else {
94                 my $message_str = $message_stack->as_string;
95                 return _clean_message($self->get_message("$args, Internal Validation Error is: $message_str"));
96             }
97         } else {
98             return $self->get_message($args);
99         }
100     }
101 };
102
103 =method generate_constraint_for ($type_constraints)
104
105 Given some type constraints, use them to generate validation rules for an ref
106 of values (to be passed at check time)
107
108 =cut
109
110 sub generate_constraint_for {
111     my ($self, $type_constraints) = @_;
112     return $self->constraint_generator->($self, $type_constraints);
113 }
114
115 =method parameterize (@type_constraints)
116
117 Given a ref of type constraints, create a structured type.
118
119 =cut
120
121 sub parameterize {
122     my ($self, @type_constraints) = @_;
123     my $class = ref $self;
124     my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
125     my $constraint_generator = $self->__infer_constraint_generator;
126
127     return $class->new(
128         name => $name,
129         parent => $self,
130         type_constraints => \@type_constraints,
131         constraint_generator => $constraint_generator,
132     );
133 }
134
135 =method __infer_constraint_generator
136
137 This returns a CODEREF which generates a suitable constraint generator.  Not
138 user servicable, you'll never call this directly.
139
140 =cut
141
142 sub __infer_constraint_generator {
143     my ($self) = @_;
144     if($self->has_constraint_generator) {
145         return $self->constraint_generator;
146     } else {
147         return sub {
148             ## I'm not sure about this stuff but everything seems to work
149             my $tc = shift @_;
150             my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
151             $self->constraint->($merged_tc, @_);
152         };
153     }
154 }
155
156 =method compile_type_constraint
157
158 hook into compile_type_constraint so we can set the correct validation rules.
159
160 =cut
161
162 around 'compile_type_constraint' => sub {
163     my ($compile_type_constraint, $self, @args) = @_;
164
165     if($self->has_type_constraints) {
166         my $type_constraints = $self->type_constraints;
167         my $constraint = $self->generate_constraint_for($type_constraints);
168         $self->_set_constraint($constraint);
169     }
170
171     return $self->$compile_type_constraint(@args);
172 };
173
174 =method create_child_type
175
176 modifier to make sure we get the constraint_generator
177
178 =cut
179
180 around 'create_child_type' => sub {
181     my ($create_child_type, $self, %opts) = @_;
182     return $self->$create_child_type(
183         %opts,
184         constraint_generator => $self->__infer_constraint_generator,
185     );
186 };
187
188 =method is_a_type_of
189
190 =method is_subtype_of
191
192 =method equals
193
194 Override the base class behavior.
195
196 =cut
197
198 sub equals {
199     my ( $self, $type_or_name ) = @_;
200     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
201
202     return unless $other->isa(__PACKAGE__);
203
204     return (
205         $self->parent->equals($other->parent)
206             and
207         $self->type_constraints_equals($other)
208     );
209 }
210
211 sub is_a_type_of {
212     my ( $self, $type_or_name ) = @_;
213     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
214
215     if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
216         if ( $self->parent->is_a_type_of($other->parent) ) {
217             return $self->_type_constraints_op_all($other, "is_a_type_of");
218         } elsif ( $self->parent->is_a_type_of($other) ) {
219             return 1;
220             # FIXME compare?
221         } else {
222             return 0;
223         }
224     } else {
225         return $self->SUPER::is_a_type_of($other);
226     }
227 }
228
229 sub is_subtype_of {
230     my ( $self, $type_or_name ) = @_;
231     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
232     if ( $other->isa(__PACKAGE__) ) {
233         if ( $other->type_constraints and $self->type_constraints ) {
234             if ( $self->parent->is_a_type_of($other->parent) ) {
235                 return (
236                     $self->_type_constraints_op_all($other, "is_a_type_of")
237                       and
238                     $self->_type_constraints_op_any($other, "is_subtype_of")
239                 );
240             } elsif ( $self->parent->is_a_type_of($other) ) {
241                 return 1;
242                 # FIXME compare?
243             } else {
244                 return 0;
245             }
246         } else {
247             if ( $self->type_constraints ) {
248                 if ( $self->SUPER::is_subtype_of($other) ) {
249                     return 1;
250                 } else {
251                     return;
252                 }
253             } else {
254                 return $self->parent->is_subtype_of($other->parent);
255             }
256         }
257     } else {
258         return $self->SUPER::is_subtype_of($other);
259     }
260 }
261
262 =method type_constraints_equals
263
264 Checks to see if the internal type constraints are equal.
265
266 =cut
267
268 sub type_constraints_equals {
269     my ( $self, $other ) = @_;
270     $self->_type_constraints_op_all($other, "equals");
271 }
272
273 sub _type_constraints_op_all {
274     my ($self, $other, $op) = @_;
275
276     return unless $other->isa(__PACKAGE__);
277
278     my @self_type_constraints = @{$self->type_constraints||[]};
279     my @other_type_constraints = @{$other->type_constraints||[]};
280
281     return unless @self_type_constraints == @other_type_constraints;
282
283     ## Incoming ay be either arrayref or hashref, need top compare both
284     while(@self_type_constraints) {
285         my $self_type_constraint = shift @self_type_constraints;
286         my $other_type_constraint = shift @other_type_constraints;
287
288         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
289           for $self_type_constraint, $other_type_constraint;
290
291         my $result = $self_type_constraint->$op($other_type_constraint);
292         return unless $result;
293     }
294
295     return 1; ##If we get this far, everything is good.
296 }
297
298 sub _type_constraints_op_any {
299     my ($self, $other, $op) = @_;
300
301     return unless $other->isa(__PACKAGE__);
302
303     my @self_type_constraints = @{$self->type_constraints||[]};
304     my @other_type_constraints = @{$other->type_constraints||[]};
305
306     return unless @self_type_constraints == @other_type_constraints;
307
308     ## Incoming ay be either arrayref or hashref, need top compare both
309     while(@self_type_constraints) {
310         my $self_type_constraint = shift @self_type_constraints;
311         my $other_type_constraint = shift @other_type_constraints;
312
313         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
314           for $self_type_constraint, $other_type_constraint;
315
316         return 1 if $self_type_constraint->$op($other_type_constraint);
317     }
318
319     return 0;
320 }
321
322 =method get_message
323
324 Give you a better peek into what's causing the error.  For now we stringify the
325 incoming deep value with L<Devel::PartialDump> and pass that on to either your
326 custom error message or the default one.  In the future we'll try to provide a
327 more complete stack trace of the actual offending elements
328
329 =cut
330
331 around 'get_message' => sub {
332     my ($get_message, $self, $value) = @_;
333     $value = Devel::PartialDump::dump($value)
334      if ref $value;
335     return $self->$get_message($value);
336 };
337
338 =head1 SEE ALSO
339
340 The following modules or resources may be of interest.
341
342 L<Moose>, L<Moose::Meta::TypeConstraint>
343
344 =cut
345
346 __PACKAGE__->meta->make_immutable(inline_constructor => 0);