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 ) = @_; |
200 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
201 | |
202 | return unless $other->isa(__PACKAGE__); |
8dbdca20 |
203 | |
16aea7bf |
204 | return ( |
179af711 |
205 | $self->parent->equals($other->parent) |
16aea7bf |
206 | and |
179af711 |
207 | $self->type_constraints_equals($other) |
16aea7bf |
208 | ); |
209 | } |
210 | |
179af711 |
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 || [] }) { |
179af711 |
216 | if ( $self->parent->is_a_type_of($other->parent) ) { |
179af711 |
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 ) = @_; |
179af711 |
231 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
179af711 |
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 | |
8dbdca20 |
262 | =method type_constraints_equals |
16aea7bf |
263 | |
67eec8f7 |
264 | Checks to see if the internal type constraints are equal. |
16aea7bf |
265 | |
266 | =cut |
267 | |
268 | sub type_constraints_equals { |
179af711 |
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 | |
16aea7bf |
278 | my @self_type_constraints = @{$self->type_constraints||[]}; |
279 | my @other_type_constraints = @{$other->type_constraints||[]}; |
179af711 |
280 | |
281 | return unless @self_type_constraints == @other_type_constraints; |
282 | |
16aea7bf |
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; |
179af711 |
286 | my $other_type_constraint = shift @other_type_constraints; |
8dbdca20 |
287 | |
179af711 |
288 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
289 | for $self_type_constraint, $other_type_constraint; |
16aea7bf |
290 | |
fd29a93c |
291 | my $result = $self_type_constraint->$op($other_type_constraint); |
292 | return unless $result; |
16aea7bf |
293 | } |
8dbdca20 |
294 | |
16aea7bf |
295 | return 1; ##If we get this far, everything is good. |
296 | } |
297 | |
179af711 |
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; |
8dbdca20 |
312 | |
179af711 |
313 | $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) |
314 | for $self_type_constraint, $other_type_constraint; |
8dbdca20 |
315 | |
179af711 |
316 | return 1 if $self_type_constraint->$op($other_type_constraint); |
317 | } |
318 | |
319 | return 0; |
320 | } |
321 | |
8dbdca20 |
322 | =method get_message |
a4a88fef |
323 | |
797510e3 |
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) = @_; |
7559b71f |
333 | $value = Devel::PartialDump::dump($value) |
334 | if ref $value; |
335 | return $self->$get_message($value); |
797510e3 |
336 | }; |
a4a88fef |
337 | |
a30fa891 |
338 | =head1 SEE ALSO |
339 | |
340 | The following modules or resources may be of interest. |
341 | |
342 | L<Moose>, L<Moose::Meta::TypeConstraint> |
343 | |
a30fa891 |
344 | =cut |
345 | |
b5deb412 |
346 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); |