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