Commit | Line | Data |
3b46bd49 |
1 | package Mouse::Util::TypeConstraints; |
d60c78b9 |
2 | use strict; |
3 | use warnings; |
139d92d2 |
4 | use base 'Exporter'; |
9baf5d6b |
5 | |
61a02a3a |
6 | use Carp (); |
6c169c50 |
7 | use Scalar::Util qw/blessed looks_like_number openhandle/; |
d60c78b9 |
8 | |
139d92d2 |
9 | our @EXPORT = qw( |
d44f0d03 |
10 | as where message from via type subtype coerce class_type role_type enum |
ccf44227 |
11 | find_type_constraint |
139d92d2 |
12 | ); |
13 | |
cceb0e25 |
14 | my %TYPE; |
7dbebb1b |
15 | my %TYPE_SOURCE; |
8a7f2a8a |
16 | my %COERCE; |
17 | my %COERCE_KEYS; |
4188b837 |
18 | |
139d92d2 |
19 | sub as ($) { |
61a02a3a |
20 | as => $_[0] |
21 | } |
139d92d2 |
22 | sub where (&) { |
61a02a3a |
23 | where => $_[0] |
24 | } |
0f1dae9a |
25 | sub message (&) { |
61a02a3a |
26 | message => $_[0] |
27 | } |
28 | |
139d92d2 |
29 | sub from { @_ } |
30 | sub via (&) { |
61a02a3a |
31 | $_[0] |
32 | } |
33 | |
321e5271 |
34 | BEGIN { |
381f326a |
35 | no warnings 'uninitialized'; |
cceb0e25 |
36 | %TYPE = ( |
381f326a |
37 | Any => sub { 1 }, |
38 | Item => sub { 1 }, |
39 | Bool => sub { |
c91d12e0 |
40 | !defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0' |
381f326a |
41 | }, |
c91d12e0 |
42 | Undef => sub { !defined($_[0]) }, |
43 | Defined => sub { defined($_[0]) }, |
44 | Value => sub { defined($_[0]) && !ref($_[0]) }, |
45 | Num => sub { !ref($_[0]) && looks_like_number($_[0]) }, |
46 | Int => sub { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }, |
47 | Str => sub { defined($_[0]) && !ref($_[0]) }, |
48 | ClassName => sub { Mouse::is_class_loaded($_[0]) }, |
49 | Ref => sub { ref($_[0]) }, |
50 | |
51 | ScalarRef => sub { ref($_[0]) eq 'SCALAR' }, |
52 | ArrayRef => sub { ref($_[0]) eq 'ARRAY' }, |
53 | HashRef => sub { ref($_[0]) eq 'HASH' }, |
54 | CodeRef => sub { ref($_[0]) eq 'CODE' }, |
55 | RegexpRef => sub { ref($_[0]) eq 'Regexp' }, |
56 | GlobRef => sub { ref($_[0]) eq 'GLOB' }, |
381f326a |
57 | |
58 | FileHandle => sub { |
c91d12e0 |
59 | ref($_[0]) eq 'GLOB' && openhandle($_[0]) |
381f326a |
60 | or |
c91d12e0 |
61 | blessed($_[0]) && $_[0]->isa("IO::Handle") |
abe4e887 |
62 | }, |
381f326a |
63 | |
c91d12e0 |
64 | Object => sub { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }, |
8a7f2a8a |
65 | ); |
78b13827 |
66 | foreach my $code (values %TYPE) { |
67 | bless $code, 'Mouse::Meta::TypeConstraint'; |
68 | } |
d3982c7e |
69 | |
cceb0e25 |
70 | sub optimized_constraints { \%TYPE } |
71 | my @TYPE_KEYS = keys %TYPE; |
72 | sub list_all_builtin_type_constraints { @TYPE_KEYS } |
7dbebb1b |
73 | |
74 | @TYPE_SOURCE{@TYPE_KEYS} = (__PACKAGE__) x @TYPE_KEYS; |
381f326a |
75 | } |
d3982c7e |
76 | |
139d92d2 |
77 | sub type { |
0d9fea22 |
78 | my $pkg = caller(0); |
79 | my($name, %conf) = @_; |
0d062abb |
80 | if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) { |
7dbebb1b |
81 | Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg"; |
0d9fea22 |
82 | }; |
321e5271 |
83 | my $constraint = $conf{where} || do { |
84 | my $as = delete $conf{as} || 'Any'; |
85 | if (! exists $TYPE{$as}) { |
86 | $TYPE{$as} = _build_type_constraint($as); |
87 | } |
88 | $TYPE{$as}; |
89 | }; |
7dbebb1b |
90 | |
91 | $TYPE_SOURCE{$name} = $pkg; |
321e5271 |
92 | $TYPE{$name} = sub { local $_ = $_[0]; $constraint->($_[0]) }; |
0d9fea22 |
93 | } |
94 | |
139d92d2 |
95 | sub subtype { |
4188b837 |
96 | my $pkg = caller(0); |
61a02a3a |
97 | my($name, %conf) = @_; |
0d062abb |
98 | if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) { |
7dbebb1b |
99 | Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg"; |
61a02a3a |
100 | }; |
321e5271 |
101 | my $constraint = $conf{where}; |
102 | my $as_constraint = find_or_create_isa_type_constraint($conf{as} || 'Any'); |
7dbebb1b |
103 | |
104 | $TYPE_SOURCE{$name} = $pkg; |
321e5271 |
105 | $TYPE{$name} = $constraint ? |
106 | sub { |
107 | local $_ = $_[0]; |
108 | $as_constraint->($_[0]) && $constraint->($_[0]) |
109 | } : |
110 | sub { |
111 | local $_ = $_[0]; |
112 | $as_constraint->($_[0]); |
113 | } |
114 | ; |
7dbebb1b |
115 | |
d9f8c878 |
116 | return $name; |
4188b837 |
117 | } |
118 | |
139d92d2 |
119 | sub coerce { |
61a02a3a |
120 | my($name, %conf) = @_; |
121 | |
122 | Carp::croak "Cannot find type '$name', perhaps you forgot to load it." |
cceb0e25 |
123 | unless $TYPE{$name}; |
61a02a3a |
124 | |
8a7f2a8a |
125 | unless ($COERCE{$name}) { |
126 | $COERCE{$name} = {}; |
127 | $COERCE_KEYS{$name} = []; |
128 | } |
61a02a3a |
129 | while (my($type, $code) = each %conf) { |
130 | Carp::croak "A coercion action already exists for '$type'" |
8a7f2a8a |
131 | if $COERCE{$name}->{$type}; |
61a02a3a |
132 | |
310ad28b |
133 | if (! $TYPE{$type}) { |
134 | # looks parameterized |
135 | if ($type =~ /^[^\[]+\[.+\]$/) { |
321e5271 |
136 | _build_type_constraint($type); |
310ad28b |
137 | } else { |
138 | Carp::croak "Could not find the type constraint ($type) to coerce from" |
139 | } |
140 | } |
61a02a3a |
141 | |
8a7f2a8a |
142 | push @{ $COERCE_KEYS{$name} }, $type; |
143 | $COERCE{$name}->{$type} = $code; |
61a02a3a |
144 | } |
4188b837 |
145 | } |
146 | |
139d92d2 |
147 | sub class_type { |
ecc6e3b1 |
148 | my($name, $conf) = @_; |
d9f8c878 |
149 | if ($conf && $conf->{class}) { |
150 | # No, you're using this wrong |
151 | warn "class_type() should be class_type(ClassName). Perhaps you're looking for subtype $name => as '$conf->{class}'?"; |
152 | subtype($name, as => $conf->{class}); |
153 | } else { |
154 | subtype( |
155 | $name => where => sub { $_->isa($name) } |
156 | ); |
157 | } |
ecc6e3b1 |
158 | } |
159 | |
139d92d2 |
160 | sub role_type { |
47f36c05 |
161 | my($name, $conf) = @_; |
162 | my $role = $conf->{role}; |
139d92d2 |
163 | subtype( |
61a02a3a |
164 | $name => where => sub { |
165 | return unless defined $_ && ref($_) && $_->isa('Mouse::Object'); |
166 | $_->meta->does_role($role); |
167 | } |
168 | ); |
47f36c05 |
169 | } |
170 | |
4188b837 |
171 | sub typecast_constraints { |
eec1bb49 |
172 | my($class, $pkg, $type_constraint, $types, $value) = @_; |
eec1bb49 |
173 | |
b3b74cc6 |
174 | local $_; |
eec1bb49 |
175 | for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) { |
8a7f2a8a |
176 | next unless $COERCE{$type}; |
8a7f2a8a |
177 | for my $coerce_type (@{ $COERCE_KEYS{$type}}) { |
b3b74cc6 |
178 | $_ = $value; |
c91d12e0 |
179 | next unless $TYPE{$coerce_type}->($value); |
b3b74cc6 |
180 | $_ = $value; |
c91d12e0 |
181 | $_ = $COERCE{$type}->{$coerce_type}->($value); |
182 | return $_ if $type_constraint->($_); |
4188b837 |
183 | } |
184 | } |
4188b837 |
185 | return $value; |
186 | } |
187 | |
01904723 |
188 | my $serial_enum = 0; |
d44f0d03 |
189 | sub enum { |
01904723 |
190 | # enum ['small', 'medium', 'large'] |
191 | if (ref($_[0]) eq 'ARRAY') { |
192 | my @elements = @{ shift @_ }; |
193 | |
194 | my $name = 'Mouse::Util::TypeConstaints::Enum::Serial::' |
195 | . ++$serial_enum; |
196 | enum($name, @elements); |
197 | return $name; |
198 | } |
199 | |
200 | # enum size => 'small', 'medium', 'large' |
d44f0d03 |
201 | my $name = shift; |
202 | my %is_valid = map { $_ => 1 } @_; |
203 | |
204 | subtype( |
205 | $name => where => sub { $is_valid{$_} } |
206 | ); |
207 | } |
208 | |
321e5271 |
209 | sub _build_type_constraint { |
210 | |
211 | my $spec = shift; |
212 | my $code; |
213 | if ($spec =~ /^([^\[]+)\[(.+)\]$/) { |
214 | # parameterized |
215 | my $constraint = $1; |
216 | my $param = $2; |
217 | my $parent; |
218 | if ($constraint eq 'Maybe') { |
219 | $parent = _build_type_constraint('Undef'); |
220 | } else { |
221 | $parent = _build_type_constraint($constraint); |
222 | } |
223 | my $child = _build_type_constraint($param); |
224 | if ($constraint eq 'ArrayRef') { |
225 | my $code_str = |
226 | "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" . |
227 | "sub {\n" . |
228 | " if (\$parent->(\$_[0])) {\n" . |
229 | " foreach my \$e (\@{\$_[0]}) {\n" . |
230 | " return () unless \$child->(\$e);\n" . |
231 | " }\n" . |
232 | " return 1;\n" . |
233 | " }\n" . |
234 | " return ();\n" . |
235 | "};\n" |
236 | ; |
237 | $code = eval $code_str or Carp::confess("Failed to generate inline type constraint: $@"); |
238 | } elsif ($constraint eq 'HashRef') { |
239 | my $code_str = |
240 | "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" . |
241 | "sub {\n" . |
242 | " if (\$parent->(\$_[0])) {\n" . |
243 | " foreach my \$e (values \%{\$_[0]}) {\n" . |
244 | " return () unless \$child->(\$e);\n" . |
245 | " }\n" . |
246 | " return 1;\n" . |
247 | " }\n" . |
248 | " return ();\n" . |
249 | "};\n" |
250 | ; |
251 | $code = eval $code_str or Carp::confess($@); |
252 | } elsif ($constraint eq 'Maybe') { |
253 | my $code_str = |
254 | "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" . |
255 | "sub {\n" . |
256 | " return \$child->(\$_[0]) || \$parent->(\$_[0]);\n" . |
257 | "};\n" |
258 | ; |
259 | $code = eval $code_str or Carp::confess($@); |
260 | } else { |
261 | Carp::confess("Support for parameterized types other than ArrayRef or HashRef is not implemented yet"); |
262 | } |
263 | $TYPE{$spec} = $code; |
264 | } else { |
265 | $code = $TYPE{ $spec }; |
266 | if (! $code) { |
267 | my $code_str = |
268 | "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" . |
269 | "sub {\n" . |
270 | " Scalar::Util::blessed(\$_[0]) && \$_[0]->isa('$spec');\n" . |
271 | "}" |
272 | ; |
273 | $code = eval $code_str or Carp::confess($@); |
78b13827 |
274 | $TYPE{$spec} = bless $code, 'Mouse::Meta::TypeConstraint'; |
321e5271 |
275 | } |
276 | } |
78b13827 |
277 | return bless $code, 'Mouse::Meta::TypeConstraint'; |
321e5271 |
278 | } |
279 | |
280 | sub find_type_constraint { |
281 | my $type_constraint = shift; |
282 | return $TYPE{$type_constraint}; |
283 | } |
284 | |
285 | sub find_or_create_isa_type_constraint { |
286 | my $type_constraint = shift; |
287 | |
288 | my $code; |
289 | |
290 | $type_constraint =~ s/\s+//g; |
291 | my @type_constraints = split /\|/, $type_constraint; |
292 | if (@type_constraints == 1) { |
293 | $code = $TYPE{$type_constraints[0]} || |
294 | _build_type_constraint($type_constraints[0]); |
295 | } else { |
296 | my @code_list = map { |
297 | $TYPE{$_} || _build_type_constraint($_) |
298 | } @type_constraints; |
78b13827 |
299 | $code = bless sub { |
321e5271 |
300 | my $i = 0; |
301 | for my $code (@code_list) { |
302 | return 1 if $code->($_[0]); |
303 | } |
304 | return 0; |
78b13827 |
305 | }, 'Mouse::Meta::TypeConstraint'; |
321e5271 |
306 | } |
307 | return $code; |
308 | } |
309 | |
78b13827 |
310 | package # Hide from pause |
311 | Mouse::Meta::TypeConstraint; |
312 | |
313 | sub check { |
314 | $_[0]->($_[1]) |
315 | } |
316 | |
317 | |
d60c78b9 |
318 | 1; |
319 | |
6feb83f1 |
320 | __END__ |
321 | |
322 | =head1 NAME |
323 | |
3b46bd49 |
324 | Mouse::Util::TypeConstraints - simple type constraints |
6feb83f1 |
325 | |
326 | =head1 METHODS |
327 | |
328 | =head2 optimized_constraints -> HashRef[CODE] |
329 | |
330 | Returns the simple type constraints that Mouse understands. |
331 | |
c91d12e0 |
332 | =head1 FUNCTIONS |
333 | |
334 | =over 4 |
335 | |
336 | =item B<subtype 'Name' => as 'Parent' => where { } ...> |
337 | |
338 | =item B<subtype as 'Parent' => where { } ...> |
339 | |
340 | =item B<class_type ($class, ?$options)> |
341 | |
342 | =item B<role_type ($role, ?$options)> |
343 | |
344 | =item B<enum (\@values)> |
345 | |
346 | =back |
347 | |
6feb83f1 |
348 | =cut |
349 | |
350 | |