| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MooX::Types::MooseLike; |
|
2
|
14
|
|
|
14
|
|
64727
|
use strict; |
|
|
14
|
|
|
|
|
24
|
|
|
|
14
|
|
|
|
|
512
|
|
|
3
|
14
|
|
|
14
|
|
54
|
use warnings FATAL => 'all'; |
|
|
14
|
|
|
|
|
21
|
|
|
|
14
|
|
|
|
|
567
|
|
|
4
|
14
|
|
|
14
|
|
67
|
use Exporter 5.57 'import'; |
|
|
14
|
|
|
|
|
272
|
|
|
|
14
|
|
|
|
|
748
|
|
|
5
|
|
|
|
|
|
|
our @EXPORT_OK; |
|
6
|
|
|
|
|
|
|
push @EXPORT_OK, qw( exception_message inflate_type ); |
|
7
|
14
|
|
|
14
|
|
3035
|
use Module::Runtime qw(require_module); |
|
|
14
|
|
|
|
|
7833
|
|
|
|
14
|
|
|
|
|
75
|
|
|
8
|
14
|
|
|
14
|
|
692
|
use Carp qw(confess croak); |
|
|
14
|
|
|
|
|
21
|
|
|
|
14
|
|
|
|
|
949
|
|
|
9
|
14
|
|
|
14
|
|
64
|
use List::Util qw(first); |
|
|
14
|
|
|
|
|
18
|
|
|
|
14
|
|
|
|
|
2961
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.28'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub register_types { |
|
14
|
17
|
|
|
17
|
1
|
502
|
my ($type_definitions, $into, $moose_namespace) = @_; |
|
15
|
17
|
|
|
|
|
25
|
foreach my $type_def (@{$type_definitions}) { |
|
|
17
|
|
|
|
|
55
|
|
|
16
|
257
|
|
|
|
|
370
|
my $coderefs = make_type($type_def, $moose_namespace); |
|
17
|
257
|
|
|
|
|
436
|
install_type($type_def->{name}, $coderefs, $into); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
17
|
|
|
|
|
489
|
return; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub install_type { |
|
23
|
257
|
|
|
257
|
0
|
283
|
my ($type_name, $coderefs, $namespace) = @_; |
|
24
|
257
|
|
|
|
|
308
|
my $is_type_name = 'is_' . $type_name; |
|
25
|
257
|
|
|
|
|
282
|
my $type_full_name = $namespace . '::' . $type_name; |
|
26
|
257
|
|
|
|
|
303
|
my $is_type_full_name = $namespace . '::' . $is_type_name; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
{ |
|
29
|
14
|
|
|
14
|
|
75
|
no strict 'refs'; ## no critic qw(TestingAndDebugging::ProhibitNoStrict) |
|
|
14
|
|
|
|
|
18
|
|
|
|
14
|
|
|
|
|
2025
|
|
|
|
257
|
|
|
|
|
196
|
|
|
30
|
257
|
|
|
|
|
236
|
*{$type_full_name} = $coderefs->{type}; |
|
|
257
|
|
|
|
|
1029
|
|
|
31
|
257
|
|
|
|
|
291
|
*{$is_type_full_name} = $coderefs->{is_type}; |
|
|
257
|
|
|
|
|
836
|
|
|
32
|
257
|
|
|
|
|
230
|
push @{"${namespace}::EXPORT_OK"}, $type_name, $is_type_name; |
|
|
257
|
|
|
|
|
762
|
|
|
33
|
|
|
|
|
|
|
} |
|
34
|
257
|
|
|
|
|
536
|
return; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub make_type { |
|
38
|
257
|
|
|
257
|
0
|
245
|
my ($type_definition, $moose_namespace) = @_; |
|
39
|
257
|
|
|
|
|
283
|
my $test = $type_definition->{test}; |
|
40
|
|
|
|
|
|
|
|
|
41
|
257
|
100
|
|
|
|
457
|
if (my $subtype_of = $type_definition->{subtype_of}) { |
|
42
|
3
|
100
|
|
|
|
11
|
if (!ref $subtype_of) { |
|
43
|
1
|
|
33
|
|
|
2
|
my $from = $type_definition->{from} |
|
44
|
|
|
|
|
|
|
|| croak "Must define a 'from' namespace for the parent type: $subtype_of when defining type: $type_definition->{name}"; |
|
45
|
1
|
|
|
|
|
1
|
$subtype_of = do { |
|
46
|
14
|
|
|
14
|
|
64
|
no strict 'refs'; |
|
|
14
|
|
|
|
|
19
|
|
|
|
14
|
|
|
|
|
8659
|
|
|
47
|
1
|
|
|
|
|
1
|
&{$from . '::' . $subtype_of}(); |
|
|
1
|
|
|
|
|
3
|
|
|
48
|
|
|
|
|
|
|
}; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
# Assume a (base) test always exists even if you must write: test => sub {1} |
|
51
|
3
|
|
|
|
|
2
|
my $base_test = $test; |
|
52
|
|
|
|
|
|
|
$test = sub { |
|
53
|
9
|
|
|
9
|
|
9
|
my $value = shift; |
|
54
|
9
|
|
|
|
|
10
|
local $@; |
|
55
|
9
|
100
|
|
|
|
7
|
eval { $subtype_of->($value); 1 } or return; |
|
|
9
|
|
|
|
|
13
|
|
|
|
7
|
|
|
|
|
27
|
|
|
56
|
|
|
|
|
|
|
# TODO implement: eval { $base_test->($value); 1 } paradigm |
|
57
|
7
|
50
|
|
|
|
13
|
if ($base_test) { |
|
58
|
7
|
100
|
|
|
|
11
|
$base_test->($value) or return; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
5
|
|
|
|
|
158
|
return 1; |
|
61
|
3
|
|
|
|
|
9
|
}; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $isa = sub { |
|
65
|
239
|
100
|
|
239
|
|
42295
|
return if $test->(@_); |
|
66
|
101
|
|
|
|
|
863
|
local $Carp::Internal{"MooX::Types::MooseLike"} = 1; ## no critic qw(Variables::ProhibitPackageVars) |
|
67
|
101
|
|
|
|
|
335
|
confess $type_definition->{message}->(@_) ; ## no critic qw(ErrorHandling::RequireUseOfExceptions) |
|
68
|
257
|
|
|
|
|
792
|
}; |
|
69
|
|
|
|
|
|
|
|
|
70
|
257
|
100
|
66
|
|
|
731
|
if (ref $type_definition->{inflate}) { |
|
|
|
100
|
|
|
|
|
|
|
71
|
81
|
|
|
|
|
176
|
$Moo::HandleMoose::TYPE_MAP{$isa} = $type_definition->{inflate}; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
elsif (exists $type_definition->{inflate} and not $type_definition->{inflate}) { |
|
74
|
|
|
|
|
|
|
# no-op |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
else { |
|
77
|
165
|
50
|
|
|
|
277
|
my $full_name = |
|
78
|
|
|
|
|
|
|
defined $moose_namespace |
|
79
|
|
|
|
|
|
|
? "${moose_namespace}::" . $type_definition->{name} |
|
80
|
|
|
|
|
|
|
: $type_definition->{name}; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$Moo::HandleMoose::TYPE_MAP{$isa} = sub { |
|
83
|
0
|
0
|
|
0
|
|
0
|
require_module($moose_namespace) if $moose_namespace; |
|
84
|
0
|
|
|
|
|
0
|
Moose::Util::TypeConstraints::find_type_constraint($full_name); |
|
85
|
165
|
|
|
|
|
628
|
}; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
return { |
|
89
|
|
|
|
|
|
|
type => sub { |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# If we have a parameterized type then we want to check its values |
|
92
|
82
|
100
|
|
82
|
|
73518
|
if (ref($_[0]) eq 'ARRAY') { |
|
93
|
38
|
|
|
|
|
35
|
my @params = @{$_[0]}; |
|
|
38
|
|
|
|
|
74
|
|
|
94
|
|
|
|
|
|
|
my $parameterized_isa = sub { |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Types that take other types as a parameter have a parameterizable |
|
97
|
|
|
|
|
|
|
# part with the one exception: 'AnyOf' |
|
98
|
100
|
100
|
|
100
|
|
94431
|
if (my $parameterizer = $type_definition->{parameterizable}) { |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Can we assume @params is a list of coderefs? |
|
101
|
54
|
50
|
|
|
|
271
|
if(my $culprit = first { (ref($_) ne 'CODE') } @params) { |
|
|
61
|
|
|
|
|
177
|
|
|
102
|
0
|
|
|
|
|
0
|
croak "Expect all parameters to be coderefs, but found: $culprit"; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Check the containing type. We could pass @_, but it is such that: |
|
106
|
|
|
|
|
|
|
# scalar @_ = 1 always in this context. In other words, |
|
107
|
|
|
|
|
|
|
# an $isa only type checks one thing at a time. |
|
108
|
54
|
|
|
|
|
177
|
$isa->($_[0]); |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Run the nested type coderefs on each value |
|
111
|
49
|
|
|
|
|
78
|
foreach my $coderef (@params) { |
|
112
|
52
|
|
|
|
|
125
|
foreach my $value ($parameterizer->($_[0])) { |
|
113
|
60
|
|
|
|
|
117
|
$coderef->($value); |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
else { |
|
118
|
|
|
|
|
|
|
# Note that while $isa only checks on value at a time |
|
119
|
|
|
|
|
|
|
# We can pass it additional parameters as we do here. |
|
120
|
|
|
|
|
|
|
# These additional parameters are then used in the type definition |
|
121
|
|
|
|
|
|
|
# For example, see InstanceOf |
|
122
|
46
|
|
|
|
|
114
|
$isa->($_[0], @params); |
|
123
|
|
|
|
|
|
|
} |
|
124
|
38
|
|
|
|
|
152
|
}; |
|
125
|
|
|
|
|
|
|
|
|
126
|
38
|
100
|
|
|
|
124
|
if (ref $type_definition->{inflate}) { |
|
127
|
33
|
|
|
|
|
39
|
my $inflation = $type_definition->{inflate}; |
|
128
|
33
|
|
|
0
|
|
141
|
$Moo::HandleMoose::TYPE_MAP{$parameterized_isa} = sub { $inflation->(\@params) }; |
|
|
0
|
|
|
|
|
0
|
|
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Remove old $isa, but return the rest of the arguments |
|
132
|
|
|
|
|
|
|
# so any specs defined after 'isa' don't get lost |
|
133
|
38
|
|
|
|
|
38
|
shift; |
|
134
|
38
|
|
|
|
|
148
|
return ($parameterized_isa, @_); |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
else { |
|
137
|
44
|
|
|
|
|
168
|
return $isa; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
}, |
|
140
|
22
|
|
|
22
|
|
1197
|
is_type => sub { $test->(@_) }, |
|
|
|
|
|
22
|
|
|
|
|
141
|
257
|
|
|
|
|
1414
|
}; |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub exception_message { |
|
145
|
81
|
|
|
81
|
1
|
113
|
my ($attribute_value, $type) = @_; |
|
146
|
81
|
100
|
|
|
|
136
|
$attribute_value = defined $attribute_value ? $attribute_value : 'undef'; |
|
147
|
81
|
|
|
|
|
10928
|
return "${attribute_value} is not ${type}!"; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub inflate_type { |
|
151
|
0
|
|
|
0
|
1
|
|
my $coderef = shift; |
|
152
|
0
|
0
|
|
|
|
|
if (my $inflator = $Moo::HandleMoose::TYPE_MAP{$coderef}) { |
|
153
|
0
|
|
|
|
|
|
return $inflator->(); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
return Moose::Meta::TypeConstraint->new( |
|
156
|
0
|
|
|
0
|
|
|
constraint => sub { eval { &$coderef; 1 } } |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |
|
161
|
|
|
|
|
|
|
__END__ |