line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Translator::Types; |
2
|
|
|
|
|
|
|
|
3
|
74
|
|
|
74
|
|
479
|
use warnings; |
|
74
|
|
|
|
|
219
|
|
|
74
|
|
|
|
|
2199
|
|
4
|
74
|
|
|
74
|
|
376
|
use strict; |
|
74
|
|
|
|
|
135
|
|
|
74
|
|
|
|
|
1917
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
SQL::Translator::Types - Type checking functions |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package Foo; |
13
|
|
|
|
|
|
|
use Moo; |
14
|
|
|
|
|
|
|
use SQL::Translator::Types qw(schema_obj enum); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has foo => ( is => 'rw', isa => schema_obj('Trigger') ); |
17
|
|
|
|
|
|
|
has bar => ( is => 'rw', isa => enum([qw(baz quux quuz)], { |
18
|
|
|
|
|
|
|
msg => "Invalid value for bar: '%s'", icase => 1, |
19
|
|
|
|
|
|
|
}); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTIONS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This module exports functions that return coderefs suitable for L |
24
|
|
|
|
|
|
|
C type checks. |
25
|
|
|
|
|
|
|
Errors are reported using L. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
74
|
|
|
74
|
|
349
|
use SQL::Translator::Utils qw(throw); |
|
74
|
|
|
|
|
184
|
|
|
74
|
|
|
|
|
3171
|
|
30
|
74
|
|
|
74
|
|
447
|
use Scalar::Util qw(blessed); |
|
74
|
|
|
|
|
171
|
|
|
74
|
|
|
|
|
3300
|
|
31
|
|
|
|
|
|
|
|
32
|
74
|
|
|
74
|
|
505
|
use Exporter qw(import); |
|
74
|
|
|
|
|
193
|
|
|
74
|
|
|
|
|
25754
|
|
33
|
|
|
|
|
|
|
our @EXPORT_OK = qw(schema_obj enum); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 FUNCTIONS |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 schema_obj($type) |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Returns a coderef that checks that its arguments is an object of the |
40
|
|
|
|
|
|
|
class C<< SQL::Translator::Schema::I<$type> >>. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub schema_obj { |
45
|
647
|
|
|
647
|
1
|
1952
|
my ($class) = @_; |
46
|
647
|
|
|
|
|
1677
|
my $name = lc $class; |
47
|
647
|
100
|
|
|
|
2397
|
$class = 'SQL::Translator::Schema' . ($class eq 'Schema' ? '' : "::$class"); |
48
|
|
|
|
|
|
|
return sub { |
49
|
2731
|
50
|
33
|
2731
|
|
101860
|
throw("Not a $name object") |
50
|
|
|
|
|
|
|
unless blessed($_[0]) and $_[0]->isa($class); |
51
|
647
|
|
|
|
|
5215
|
}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 enum(\@strings, [$msg | \%parameters]) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Returns a coderef that checks that the argument is one of the provided |
57
|
|
|
|
|
|
|
C<@strings>. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head3 Parameters |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item msg |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
L string for the error message. |
66
|
|
|
|
|
|
|
If no other parameters are needed, this can be provided on its own, |
67
|
|
|
|
|
|
|
instead of the C<%parameters> hashref. |
68
|
|
|
|
|
|
|
The invalid value is passed as the only argument. |
69
|
|
|
|
|
|
|
Defaults to C. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item icase |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
If true, folds the values to lower case before checking for equality. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item allow_undef |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
If true, allow C in addition to the specified strings. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item allow_false |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
If true, allow any false value in addition to the specified strings. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=back |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub enum { |
88
|
359
|
|
|
359
|
1
|
35431
|
my ($values, $args) = @_; |
89
|
359
|
|
50
|
|
|
1151
|
$args ||= {}; |
90
|
359
|
50
|
|
|
|
1236
|
$args = { msg => $args } unless ref($args) eq 'HASH'; |
91
|
359
|
|
|
|
|
899
|
my $icase = !!$args->{icase}; |
92
|
359
|
100
|
|
|
|
570
|
my %values = map { ($icase ? lc : $_) => undef } @{$values}; |
|
1229
|
|
|
|
|
3417
|
|
|
359
|
|
|
|
|
768
|
|
93
|
359
|
|
50
|
|
|
1274
|
my $msg = $args->{msg} || "Invalid value: '%s'"; |
94
|
|
|
|
|
|
|
my $extra_test = |
95
|
104
|
|
|
104
|
|
1791
|
$args->{allow_undef} ? sub { defined $_[0] } : |
96
|
359
|
50
|
|
1307
|
|
1871
|
$args->{allow_false} ? sub { !!$_[0] } : undef; |
|
1307
|
100
|
|
|
|
22329
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
return sub { |
99
|
1411
|
100
|
|
1411
|
|
46866
|
my $val = $icase ? lc $_[0] : $_[0]; |
100
|
|
|
|
|
|
|
throw(sprintf($msg, $val)) |
101
|
|
|
|
|
|
|
if (!defined($extra_test) || $extra_test->($val)) |
102
|
1411
|
100
|
66
|
|
|
4271
|
&& !exists $values{$val}; |
|
|
|
100
|
|
|
|
|
103
|
359
|
|
|
|
|
2513
|
}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |