Commit | Line | Data |
59deb858 |
1 | package ## Hide from PAUSE |
2 | MooseX::Meta::TypeConstraint::Structured; |
8dbdca20 |
3 | # ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints. |
a30fa891 |
4 | |
5 | use Moose; |
797510e3 |
6 | use Devel::PartialDump; |
a30fa891 |
7 | use Moose::Util::TypeConstraints (); |
16aea7bf |
8 | use MooseX::Meta::TypeCoercion::Structured; |
a30fa891 |
9 | extends 'Moose::Meta::TypeConstraint'; |
10 | |
a30fa891 |
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 |
8dbdca20 |
20 | |
a30fa891 |
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 | |
8dbdca20 |
24 | =attr type_constraints |
a30fa891 |
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 | |
8dbdca20 |
36 | =attr constraint_generator |
a30fa891 |
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 | |
e327145a |
43 | has 'constraint_generator' => ( |
44 | is=>'ro', |
45 | isa=>'CodeRef', |
46 | predicate=>'has_constraint_generator', |
47 | ); |
a30fa891 |
48 | |
c988e3f2 |
49 | has coercion => ( |
50 | is => 'ro', |
51 | isa => 'Object', |
52 | builder => '_build_coercion', |
53 | ); |
54 | |
c988e3f2 |
55 | sub _build_coercion { |
56 | my ($self) = @_; |
57 | return MooseX::Meta::TypeCoercion::Structured->new( |
16aea7bf |
58 | type_constraint => $self, |
c988e3f2 |
59 | ); |
60 | } |
16aea7bf |
61 | |
8dbdca20 |
62 | =method validate |
7559b71f |
63 | |
64 | Messing with validate so that we can support niced error messages. |
8dbdca20 |
65 | |
7559b71f |
66 | =cut |
67 | |
fbe3dfe7 |
68 | sub _clean_message { |
69 | my $message = shift @_; |
70 | $message =~s/MooseX::Types::Structured:://g; |
71 | return $message; |
72 | } |
73 | |
7559b71f |
74 | override 'validate' => sub { |
9448ea2c |
75 | my ($self, $value, $message_stack) = @_; |
76 | unless ($message_stack) { |
77 | $message_stack = MooseX::Types::Structured::MessageStack->new(); |
78 | } |
7559b71f |
79 | |
9448ea2c |
80 | $message_stack->inc_level; |
21d0e759 |
81 | |
9448ea2c |
82 | if ($self->_compiled_type_constraint->($value, $message_stack)) { |
d716430a |
83 | ## Everything is good, no error message to return |
84 | return undef; |
7559b71f |
85 | } else { |
d716430a |
86 | ## Whoops, need to figure out the right error message |
21d0e759 |
87 | my $args = Devel::PartialDump::dump($value); |
9448ea2c |
88 | $message_stack->dec_level; |
89 | if($message_stack->has_messages) { |
90 | if($message_stack->level) { |
91 | ## we are inside a deeply structured constraint |
21d0e759 |
92 | return $self->get_message($args); |
93 | } else { |
9448ea2c |
94 | my $message_str = $message_stack->as_string; |
fbe3dfe7 |
95 | return _clean_message($self->get_message("$args, Internal Validation Error is: $message_str")); |
21d0e759 |
96 | } |
7559b71f |
97 | } else { |
98 | return $self->get_message($args); |
99 | } |
100 | } |
101 | }; |
102 | |
8dbdca20 |
103 | =method generate_constraint_for ($type_constraints) |
a30fa891 |
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) = @_; |
a4ae4800 |
112 | return $self->constraint_generator->($self, $type_constraints); |
a30fa891 |
113 | } |
114 | |
8dbdca20 |
115 | =method parameterize (@type_constraints) |
a30fa891 |
116 | |
117 | Given a ref of type constraints, create a structured type. |
118 | |
119 | =cut |
120 | |
121 | sub parameterize { |
16aea7bf |
122 | my ($self, @type_constraints) = @_; |
123 | my $class = ref $self; |
a30fa891 |
124 | my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']'; |
e327145a |
125 | my $constraint_generator = $self->__infer_constraint_generator; |
67a8bc04 |
126 | |
16aea7bf |
127 | return $class->new( |
a30fa891 |
128 | name => $name, |
129 | parent => $self, |
130 | type_constraints => \@type_constraints, |
e327145a |
131 | constraint_generator => $constraint_generator, |
132 | ); |
133 | } |
134 | |
8dbdca20 |
135 | =method __infer_constraint_generator |
e327145a |
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 |
67a8bc04 |
149 | my $tc = shift @_; |
150 | my $merged_tc = [@$tc, @{$self->parent->type_constraints}]; |
8dbdca20 |
151 | $self->constraint->($merged_tc, @_); |
e327145a |
152 | }; |
8dbdca20 |
153 | } |
a30fa891 |
154 | } |
155 | |
8dbdca20 |
156 | =method compile_type_constraint |
a30fa891 |
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) = @_; |
8dbdca20 |
164 | |
a30fa891 |
165 | if($self->has_type_constraints) { |
166 | my $type_constraints = $self->type_constraints; |
167 | my $constraint = $self->generate_constraint_for($type_constraints); |
8dbdca20 |
168 | $self->_set_constraint($constraint); |
a30fa891 |
169 | } |
170 | |
171 | return $self->$compile_type_constraint(@args); |
172 | }; |
173 | |
8dbdca20 |
174 | =method create_child_type |
a4a88fef |
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, |
190a34eb |
184 | constraint_generator => $self->__infer_constraint_generator, |
a4a88fef |
185 | ); |
186 | }; |
187 | |
8dbdca20 |
188 | =method is_a_type_of |
a4a88fef |
189 | |
8dbdca20 |
190 | =method is_subtype_of |
a4a88fef |
191 | |
8dbdca20 |
192 | =method equals |
a4a88fef |
193 | |
16aea7bf |
194 | Override the base class behavior. |
195 | |
196 | =cut |
197 | |
198 | sub equals { |
199 | my ( $self, $type_or_name ) = @_; |
8c187148 |
200 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) |
201 | or return; |
16aea7bf |
202 | |
203 | return unless $other->isa(__PACKAGE__); |
8dbdca20 |
204 | |
16aea7bf |
205 | return ( |
179af711 |
206 | $self->parent->equals($other->parent) |
16aea7bf |
207 | and |
179af711 |
208 | $self->type_constraints_equals($other) |
16aea7bf |
209 | ); |
210 | } |
211 | |
179af711 |
212 | sub is_a_type_of { |
213 | my ( $self, $type_or_name ) = @_; |
8c187148 |
214 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) |
215 | or return; |
179af711 |
216 | |
217 | if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) { |
179af711 |
218 | if ( $self->parent->is_a_type_of($other->parent) ) { |
179af711 |
219 | return $self->_type_constraints_op_all($other, "is_a_type_of"); |
220 | } elsif ( $self->parent->is_a_type_of($other) ) { |
221 | return 1; |
222 | # FIXME compare? |
223 | } else { |
224 | return 0; |
225 | } |
226 | } else { |
227 | return $self->SUPER::is_a_type_of($other); |
228 | } |
229 | } |
230 | |
231 | sub is_subtype_of { |
232 | my ( $self, $type_or_name ) = @_; |
8c187148 |
233 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) |
234 | or return; |
179af711 |
235 | if ( $other->isa(__PACKAGE__) ) { |
236 | if ( $other->type_constraints and $self->type_constraints ) { |
237 | if ( $self->parent->is_a_type_of($other->parent) ) { |
238 | return ( |
239 | $self->_type_constraints_op_all($other, "is_a_type_of") |
240 | and |
241 | $self->_type_constraints_op_any($other, "is_subtype_of") |
242 | ); |
243 | } elsif ( $self->parent->is_a_type_of($other) ) { |
244 | return 1; |
245 | # FIXME compare? |
246 | } else { |
247 | return 0; |
248 | } |
249 | } else { |
250 | if ( $self->type_constraints ) { |
251 | if ( $self->SUPER::is_subtype_of($other) ) { |
252 | return 1; |
253 | } else { |
254 | return; |
255 | } |
256 | } else { |
257 | return $self->parent->is_subtype_of($other->parent); |
258 | } |
259 | } |
260 | } else { |
261 | return $self->SUPER::is_subtype_of($other); |
262 | } |
263 | } |
264 | |
8dbdca20 |
265 | =method type_constraints_equals |
16aea7bf |
266 | |
67eec8f7 |
267 | Checks to see if the internal type constraints are equal. |
16aea7bf |
268 | |
269 | =cut |
270 | |
271 | sub type_constraints_equals { |
179af711 |
272 | my ( $self, $other ) = @_; |
273 | $self->_type_constraints_op_all($other, "equals"); |
274 | } |
275 | |
276 | sub _type_constraints_op_all { |
277 | my ($self, $other, $op) = @_; |
278 | |
279 | return unless $other->isa(__PACKAGE__); |
280 | |
16aea7bf |
281 | my @self_type_constraints = @{$self->type_constraints||[]}; |
282 | my @other_type_constraints = @{$other->type_constraints||[]}; |
179af711 |
283 | |
284 | return unless @self_type_constraints == @other_type_constraints; |
285 | |
16aea7bf |
286 | ## Incoming ay be either arrayref or hashref, need top compare both |
287 | while(@self_type_constraints) { |
288 | my $self_type_constraint = shift @self_type_constraints; |
179af711 |
289 | my $other_type_constraint = shift @other_type_constraints; |
8dbdca20 |
290 | |
179af711 |
291 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
292 | for $self_type_constraint, $other_type_constraint; |
16aea7bf |
293 | |
fd29a93c |
294 | my $result = $self_type_constraint->$op($other_type_constraint); |
295 | return unless $result; |
16aea7bf |
296 | } |
8dbdca20 |
297 | |
16aea7bf |
298 | return 1; ##If we get this far, everything is good. |
299 | } |
300 | |
179af711 |
301 | sub _type_constraints_op_any { |
302 | my ($self, $other, $op) = @_; |
303 | |
304 | return unless $other->isa(__PACKAGE__); |
305 | |
306 | my @self_type_constraints = @{$self->type_constraints||[]}; |
307 | my @other_type_constraints = @{$other->type_constraints||[]}; |
308 | |
309 | return unless @self_type_constraints == @other_type_constraints; |
310 | |
311 | ## Incoming ay be either arrayref or hashref, need top compare both |
312 | while(@self_type_constraints) { |
313 | my $self_type_constraint = shift @self_type_constraints; |
314 | my $other_type_constraint = shift @other_type_constraints; |
8dbdca20 |
315 | |
179af711 |
316 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
317 | for $self_type_constraint, $other_type_constraint; |
8dbdca20 |
318 | |
179af711 |
319 | return 1 if $self_type_constraint->$op($other_type_constraint); |
320 | } |
321 | |
322 | return 0; |
323 | } |
324 | |
8dbdca20 |
325 | =method get_message |
a4a88fef |
326 | |
797510e3 |
327 | Give you a better peek into what's causing the error. For now we stringify the |
328 | incoming deep value with L<Devel::PartialDump> and pass that on to either your |
329 | custom error message or the default one. In the future we'll try to provide a |
330 | more complete stack trace of the actual offending elements |
331 | |
332 | =cut |
333 | |
334 | around 'get_message' => sub { |
335 | my ($get_message, $self, $value) = @_; |
7559b71f |
336 | $value = Devel::PartialDump::dump($value) |
337 | if ref $value; |
338 | return $self->$get_message($value); |
797510e3 |
339 | }; |
a4a88fef |
340 | |
a30fa891 |
341 | =head1 SEE ALSO |
342 | |
343 | The following modules or resources may be of interest. |
344 | |
345 | L<Moose>, L<Moose::Meta::TypeConstraint> |
346 | |
a30fa891 |
347 | =cut |
348 | |
b5deb412 |
349 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); |