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 ) = @_; |
235 | |
236 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
237 | |
238 | if ( $other->isa(__PACKAGE__) ) { |
239 | if ( $other->type_constraints and $self->type_constraints ) { |
240 | if ( $self->parent->is_a_type_of($other->parent) ) { |
241 | return ( |
242 | $self->_type_constraints_op_all($other, "is_a_type_of") |
243 | and |
244 | $self->_type_constraints_op_any($other, "is_subtype_of") |
245 | ); |
246 | } elsif ( $self->parent->is_a_type_of($other) ) { |
247 | return 1; |
248 | # FIXME compare? |
249 | } else { |
250 | return 0; |
251 | } |
252 | } else { |
253 | if ( $self->type_constraints ) { |
254 | if ( $self->SUPER::is_subtype_of($other) ) { |
255 | return 1; |
256 | } else { |
257 | return; |
258 | } |
259 | } else { |
260 | return $self->parent->is_subtype_of($other->parent); |
261 | } |
262 | } |
263 | } else { |
264 | return $self->SUPER::is_subtype_of($other); |
265 | } |
266 | } |
267 | |
16aea7bf |
268 | =head2 type_constraints_equals |
269 | |
270 | Checks to see if the internal type contraints are equal. |
271 | |
272 | =cut |
273 | |
274 | sub type_constraints_equals { |
179af711 |
275 | my ( $self, $other ) = @_; |
276 | $self->_type_constraints_op_all($other, "equals"); |
277 | } |
278 | |
279 | sub _type_constraints_op_all { |
280 | my ($self, $other, $op) = @_; |
281 | |
282 | return unless $other->isa(__PACKAGE__); |
283 | |
16aea7bf |
284 | my @self_type_constraints = @{$self->type_constraints||[]}; |
285 | my @other_type_constraints = @{$other->type_constraints||[]}; |
179af711 |
286 | |
287 | return unless @self_type_constraints == @other_type_constraints; |
288 | |
16aea7bf |
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; |
179af711 |
292 | my $other_type_constraint = shift @other_type_constraints; |
16aea7bf |
293 | |
179af711 |
294 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
295 | for $self_type_constraint, $other_type_constraint; |
16aea7bf |
296 | |
179af711 |
297 | $self_type_constraint->$op($other_type_constraint) or return; |
16aea7bf |
298 | } |
299 | |
300 | return 1; ##If we get this far, everything is good. |
301 | } |
302 | |
179af711 |
303 | sub _type_constraints_op_any { |
304 | my ($self, $other, $op) = @_; |
305 | |
306 | return unless $other->isa(__PACKAGE__); |
307 | |
308 | my @self_type_constraints = @{$self->type_constraints||[]}; |
309 | my @other_type_constraints = @{$other->type_constraints||[]}; |
310 | |
311 | return unless @self_type_constraints == @other_type_constraints; |
312 | |
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; |
317 | |
318 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
319 | for $self_type_constraint, $other_type_constraint; |
320 | |
321 | return 1 if $self_type_constraint->$op($other_type_constraint); |
322 | } |
323 | |
324 | return 0; |
325 | } |
326 | |
a4a88fef |
327 | =head2 get_message |
328 | |
797510e3 |
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 |
333 | |
334 | =cut |
335 | |
336 | around 'get_message' => sub { |
337 | my ($get_message, $self, $value) = @_; |
7559b71f |
338 | $value = Devel::PartialDump::dump($value) |
339 | if ref $value; |
340 | return $self->$get_message($value); |
797510e3 |
341 | }; |
a4a88fef |
342 | |
a30fa891 |
343 | =head1 SEE ALSO |
344 | |
345 | The following modules or resources may be of interest. |
346 | |
347 | L<Moose>, L<Moose::Meta::TypeConstraint> |
348 | |
349 | =head1 AUTHOR |
350 | |
351 | John Napiorkowski, C<< <jjnapiork@cpan.org> >> |
352 | |
353 | =head1 COPYRIGHT & LICENSE |
354 | |
355 | This program is free software; you can redistribute it and/or modify |
356 | it under the same terms as Perl itself. |
357 | |
358 | =cut |
359 | |
b5deb412 |
360 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); |