Commit | Line | Data |
684db121 |
1 | package Mouse::Meta::TypeConstraint; |
bc69ee88 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
9c85e9dc |
3 | |
684db121 |
4 | sub new { |
184f8f53 |
5 | my $class = shift; |
6 | my %args = @_ == 1 ? %{$_[0]} : @_; |
f5ee065f |
7 | |
8 | $args{name} = '__ANON__' if !defined $args{name}; |
684db121 |
9 | |
eed39dff |
10 | my $type_parameter; |
11 | if(defined $args{parent}) { # subtyping |
5a592ad7 |
12 | %args = (%{$args{parent}}, %args); |
eed39dff |
13 | |
5a592ad7 |
14 | # a child type must not inherit 'compiled_type_constraint' |
15 | # and 'hand_optimized_type_constraint' from the parent |
eed39dff |
16 | delete $args{compiled_type_constraint}; # don't inherit it |
17 | delete $args{hand_optimized_type_constraint}; # don't inherit it |
18 | |
19 | $type_parameter = $args{type_parameter}; |
7e030e45 |
20 | if(defined(my $parent_tp = $args{parent}{type_parameter})) { |
eed39dff |
21 | if($parent_tp != $type_parameter) { |
22 | $type_parameter->is_a_type_of($parent_tp) |
23 | or $class->throw_error( |
24 | "$type_parameter is not a subtype of $parent_tp", |
25 | ); |
26 | } |
eaa30ab8 |
27 | else { |
28 | $type_parameter = undef; |
29 | } |
7e030e45 |
30 | } |
5a592ad7 |
31 | } |
32 | |
33 | my $check; |
3b89ea91 |
34 | |
eed39dff |
35 | if($check = delete $args{optimized}) { # likely to be builtins |
3b89ea91 |
36 | $args{hand_optimized_type_constraint} = $check; |
37 | $args{compiled_type_constraint} = $check; |
38 | } |
eed39dff |
39 | elsif(defined $type_parameter) { # parameterizing |
5a592ad7 |
40 | my $generator = $args{constraint_generator} |
eed39dff |
41 | || $class->throw_error( |
42 | "The $args{name} constraint cannot be used," |
43 | . " because $type_parameter doesn't subtype" |
44 | . " from a parameterizable type"); |
45 | |
46 | my $parameterized_check = $generator->($type_parameter); |
47 | if(defined(my $my_check = $args{constraint})) { |
48 | $check = sub { |
49 | return $parameterized_check->($_) && $my_check->($_); |
50 | }; |
51 | } |
52 | else { |
53 | $check = $parameterized_check; |
54 | } |
55 | $args{constraint} = $check; |
5a592ad7 |
56 | } |
eed39dff |
57 | else { # common cases |
5a592ad7 |
58 | $check = $args{constraint}; |
59 | } |
f5ee065f |
60 | |
f5ee065f |
61 | if(defined($check) && ref($check) ne 'CODE'){ |
3a29a080 |
62 | $class->throw_error( |
63 | "Constraint for $args{name} is not a CODE reference"); |
f5ee065f |
64 | } |
65 | |
66 | my $self = bless \%args, $class; |
3a29a080 |
67 | $self->compile_type_constraint() |
5a592ad7 |
68 | if !$args{hand_optimized_type_constraint}; |
f5ee065f |
69 | |
5a592ad7 |
70 | if($args{type_constraints}) { |
71 | $self->_compile_union_type_coercion(); |
72 | } |
f5ee065f |
73 | return $self; |
74 | } |
75 | |
c5940bae |
76 | sub create_child_type { |
f5ee065f |
77 | my $self = shift; |
c5940bae |
78 | return ref($self)->new(@_, parent => $self); |
684db121 |
79 | } |
80 | |
4c99ea95 |
81 | sub name; |
82 | sub parent; |
83 | sub message; |
84 | sub has_coercion; |
2b82d75d |
85 | |
c7576321 |
86 | sub check; |
87 | |
2b82d75d |
88 | sub type_parameter; |
89 | sub __is_parameterized; |
90 | |
4c99ea95 |
91 | sub _compiled_type_constraint; |
92 | sub _compiled_type_coercion; |
93 | |
94 | sub compile_type_constraint; |
95 | |
c7576321 |
96 | |
3a29a080 |
97 | sub _add_type_coercions { # ($self, @pairs) |
ffbbf459 |
98 | my $self = shift; |
99 | |
4b58033d |
100 | if(exists $self->{type_constraints}){ # union type |
101 | $self->throw_error( |
102 | "Cannot add additional type coercions to Union types '$self'"); |
103 | } |
104 | |
cc131889 |
105 | my $coercions = ($self->{coercion_map} ||= []); |
ffbbf459 |
106 | my %has = map{ $_->[0] => undef } @{$coercions}; |
107 | |
108 | for(my $i = 0; $i < @_; $i++){ |
109 | my $from = $_[ $i]; |
110 | my $action = $_[++$i]; |
111 | |
112 | if(exists $has{$from}){ |
c7576321 |
113 | $self->throw_error("A coercion action already exists for '$from'"); |
ffbbf459 |
114 | } |
115 | |
116 | my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from) |
3a29a080 |
117 | or $self->throw_error( |
118 | "Could not find the type constraint ($from) to coerce from"); |
ffbbf459 |
119 | |
120 | push @{$coercions}, [ $type => $action ]; |
121 | } |
122 | |
4b58033d |
123 | $self->_compile_type_coercion(); |
536e3c33 |
124 | return; |
125 | } |
126 | |
127 | sub _compile_type_coercion { |
128 | my($self) = @_; |
129 | |
cc131889 |
130 | my @coercions = @{$self->{coercion_map}}; |
536e3c33 |
131 | |
132 | $self->{_compiled_type_coercion} = sub { |
133 | my($thing) = @_; |
134 | foreach my $pair (@coercions) { |
135 | #my ($constraint, $converter) = @$pair; |
136 | if ($pair->[0]->check($thing)) { |
137 | local $_ = $thing; |
138 | return $pair->[1]->($thing); |
139 | } |
140 | } |
141 | return $thing; |
142 | }; |
143 | return; |
144 | } |
145 | |
146 | sub _compile_union_type_coercion { |
147 | my($self) = @_; |
148 | |
149 | my @coercions; |
150 | foreach my $type(@{$self->{type_constraints}}){ |
151 | if($type->has_coercion){ |
152 | push @coercions, $type; |
153 | } |
154 | } |
155 | if(@coercions){ |
ffbbf459 |
156 | $self->{_compiled_type_coercion} = sub { |
536e3c33 |
157 | my($thing) = @_; |
158 | foreach my $type(@coercions){ |
159 | my $value = $type->coerce($thing); |
160 | return $value if $self->check($value); |
161 | } |
162 | return $thing; |
ffbbf459 |
163 | }; |
164 | } |
165 | return; |
166 | } |
167 | |
ffbbf459 |
168 | sub coerce { |
169 | my $self = shift; |
c7576321 |
170 | return $_[0] if $self->check(@_); |
ffbbf459 |
171 | |
4b58033d |
172 | my $coercion = $self->{_compiled_type_coercion} |
173 | or $self->throw_error("Cannot coerce without a type coercion"); |
de0d4152 |
174 | return $coercion->(@_); |
feb0e21b |
175 | } |
176 | |
177 | sub get_message { |
178 | my ($self, $value) = @_; |
179 | if ( my $msg = $self->message ) { |
180 | local $_ = $value; |
181 | return $msg->($value); |
182 | } |
183 | else { |
6e647cac |
184 | if(not defined $value) { |
185 | $value = 'undef'; |
186 | } |
187 | elsif( ref($value) && defined(&overload::StrVal) ) { |
188 | $value = overload::StrVal($value); |
189 | } |
537873b0 |
190 | return "Validation failed for '$self' with value $value"; |
feb0e21b |
191 | } |
192 | } |
193 | |
4b58033d |
194 | sub is_a_type_of { |
feb0e21b |
195 | my($self, $other) = @_; |
196 | |
197 | # ->is_a_type_of('__ANON__') is always false |
ca352580 |
198 | return 0 if !ref($other) && $other eq '__ANON__'; |
feb0e21b |
199 | |
200 | (my $other_name = $other) =~ s/\s+//g; |
201 | |
202 | return 1 if $self->name eq $other_name; |
203 | |
204 | if(exists $self->{type_constraints}){ # union |
3a29a080 |
205 | foreach my $type(@{$self->{type_constraints}}) { |
feb0e21b |
206 | return 1 if $type->name eq $other_name; |
207 | } |
208 | } |
209 | |
3a29a080 |
210 | for(my $p = $self->parent; defined $p; $p = $p->parent) { |
211 | return 1 if $p->name eq $other_name; |
feb0e21b |
212 | } |
213 | |
214 | return 0; |
215 | } |
216 | |
b4d791ba |
217 | # See also Moose::Meta::TypeConstraint::Parameterizable |
7e030e45 |
218 | sub parameterize { |
b4d791ba |
219 | my($self, $param, $name) = @_; |
220 | |
221 | if(!ref $param){ |
222 | require Mouse::Util::TypeConstraints; |
223 | $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param); |
224 | } |
225 | |
226 | $name ||= sprintf '%s[%s]', $self->name, $param->name; |
b4d791ba |
227 | return Mouse::Meta::TypeConstraint->new( |
fc83f4cf |
228 | name => $name, |
229 | parent => $self, |
230 | type_parameter => $param, |
b4d791ba |
231 | ); |
232 | } |
feb0e21b |
233 | |
78508064 |
234 | sub assert_valid { |
235 | my ($self, $value) = @_; |
236 | |
c7576321 |
237 | if(!$self->check($value)){ |
238 | $self->throw_error($self->get_message($value)); |
78508064 |
239 | } |
240 | return 1; |
241 | } |
242 | |
22be7db2 |
243 | # overloading stuff |
244 | |
245 | sub _as_string { $_[0]->name } # overload "" |
246 | sub _identity; # overload 0+ |
6e647cac |
247 | |
248 | sub _unite { # overload infix:<|> |
249 | my($lhs, $rhs) = @_; |
250 | require Mouse::Util::TypeConstraints; |
251 | return Mouse::Util::TypeConstraints::find_or_parse_type_constraint( |
252 | " $lhs | $rhs", |
253 | ); |
254 | } |
255 | |
684db121 |
256 | 1; |
257 | __END__ |
258 | |
259 | =head1 NAME |
260 | |
1820fffe |
261 | Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass |
684db121 |
262 | |
a25ca8d6 |
263 | =head1 VERSION |
264 | |
999072ab |
265 | This document describes Mouse version 0.82 |
a25ca8d6 |
266 | |
684db121 |
267 | =head1 DESCRIPTION |
268 | |
3a29a080 |
269 | This class represents a type constraint, including built-in |
270 | type constraints, union type constraints, parameterizable/ |
271 | parameterized type constraints, as well as custom type |
272 | constraints |
684db121 |
273 | |
274 | =head1 METHODS |
275 | |
ba05bb84 |
276 | =over |
3a29a080 |
277 | |
278 | =item C<< Mouse::Meta::TypeConstraint->new(%options) >> |
279 | |
280 | =item C<< $constraint->name >> |
281 | |
282 | =item C<< $constraint->parent >> |
283 | |
284 | =item C<< $constraint->constraint >> |
285 | |
286 | =item C<< $constraint->has_coercion >> |
287 | |
288 | =item C<< $constraint->message >> |
289 | |
be06d3b0 |
290 | =item C<< $constraint->is_a_type_of($name or $object) >> |
3a29a080 |
291 | |
292 | =item C<< $constraint->coerce($value) >> |
293 | |
294 | =item C<< $constraint->check($value) >> |
295 | |
296 | =item C<< $constraint->assert_valid($value) >> |
684db121 |
297 | |
3a29a080 |
298 | =item C<< $constraint->get_message($value) >> |
684db121 |
299 | |
3a29a080 |
300 | =item C<< $constraint->create_child_type(%options) >> |
684db121 |
301 | |
302 | =back |
303 | |
1820fffe |
304 | =head1 SEE ALSO |
305 | |
306 | L<Moose::Meta::TypeConstraint> |
307 | |
684db121 |
308 | =cut |
309 | |