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