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