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