lib/Types/Standard/Dict.pm

filter: Statementstmt 96.7% 263 / 273 (1 uncoverable) Branchbran 83.5% 142 / 170 Conditioncond 71.9% 77 / 107 MC/DCmcdc 50.9% 28 / 55 Subroutinesub 100% 28 / 28 Podpod 50.0% 1 / 2 totaltotal 85.0% 539 / 635 (1 uncoverable) CC 132 SCAR 24.5
CC
132
Cov
88%
__deep_explanation
20.5
__coercion_generator
18.1
_exporter_fail
14.6
CRAP 11.6
« blib/lib/Type/Tiny.pm ↑ Summary lib/Type/Parser.pm »
1 # INTERNAL MODULE: guts for Dict type from Types::Standard.
2  
3 package Types::Standard::Dict;
4  
5 22 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 $Types::Standard::Dict::AUTHORITY = 'cpan:TOBYINK';
11 22 $Types::Standard::Dict::VERSION = '2.010001';
12 }
13  
14 22 $Types::Standard::Dict::VERSION =~ tr/_//d;
15  
16 22 ▶use Types::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶sub _croak ($;@) {
Subroutine _croak: called CC=1 SCAR=1.2
20 3 require Carp;
21 3 goto \&Carp::confess;
22 0 require Error::TypeTiny;
23 0 goto \&Error::TypeTiny::croak;
24 }
25  
26 22 my $_Slurpy = Types::Standard::Slurpy;
27 22 my $_optional = Types::Standard::Optional;
28 22 my $_hash = Types::Standard::HashRef;
29 22 my $_map = Types::Standard::Map;
30 22 my $_any = Types::Standard::Any;
31  
32 22 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 22 our @ISA = qw( Exporter::Tiny );
34 22 our @EXPORT_OK = qw( combine );
35  
36 3 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=11 SCAR=26.8
37 3 my ( $class, $type_name, $values, $globals ) = @_;
38 3 my $caller = $globals->{into};
39
40 3 my @final;
41 {
42 3 ▶ my $to_type = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
43 9 ▶ return $_[0] if Types::TypeTiny::is_TypeTiny($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Types::TypeTiny::is_TypeTiny($_[0]) 9 0 9
44 0 require Type::Utils;
45 0 Type::Utils::dwim_type( $_[0], for => 'caller' );
46 3 };
47 3 my $of = $values->{of};
48 3 ▶ Types::TypeTiny::is_ArrayLike($of)
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_ArrayLike($of) or &_croak(qq[Expected arrayref option "of" for type "$type_name"]) 3 0 3
49 or _croak( qq{Expected arrayref option "of" for type "$type_name"} );
50 3 my @of_copy = @$of;
51 3 ▶ my $slurpy = @of_copy % 2 ? pop( @of_copy ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@of_copy % 2 ? : 0 3 3
52 3 my $iter = pair_iterator( @of_copy );
53 3 ▶ while ( my ( $name, $type ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 9 3 12
54 9 push @final, $name, $to_type->( $type );
55 }
56 3 ▶ push @final, $to_type->( $slurpy ) if defined $slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if defined $slurpy 0 3 3
57 }
58
59 3 my $type = Types::Standard::Dict->of( @final );
60 $type = $type->create_child_type(
61 name => $type_name,
62 $type->has_coercion ? ( coercion => 1 ) : (),
63 3 ▶ exists( $values->{where} ) ? ( constraint => $values->{where} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$type->has_coercion ? : 0 3 3
exists $values->{'where'} ? : 0 3 3
64 );
65
66 $INC{'Type/Registry.pm'}
67 ? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
68 : ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
69 3 ▶ unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $caller or $caller eq "-lexical" or $globals->{"lexical"} 3 0 3
$INC{'Type/Registry.pm'} ? : 0 3 3
Condition33%2/6
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
l!l&&r!l&&!r
003
ref $caller or $caller eq "-lexical"
l!l&&r!l&&!r
003
MC/DC0%0/3
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
ref $caller $caller eq "-lexical" $globals->{"lexical"}
Truth table25%1/4
A or B or C
A= ref $callerB= $caller eq "-lexical"C= $globals->{"lexical"}
ABCresult
1XX1
01X1
0011
0000
70 3 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
71 }
72  
73 22 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 142 ▶sub pair_iterator {
Subroutine pair_iterator: called CC=2 SCAR=6.9
Pod undocumented
76 142 ▶ _croak( "Expected even-sized list" ) if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 141 142
77 141 my @array = @_;
78 372 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
79 372 ▶ return unless @array;
Branch: 1 branch
BranchTrueFalseTotal
unless @array 136 236 372
80 236 splice( @array, 0, 2 );
81 141 };
82 }
83  
84 67 ▶sub __constraint_generator {
Subroutine __constraint_generator: called CC=7 SCAR=19.5
85 67 ▶ my $slurpy =
Branch: 1 branch
BranchTrueFalseTotal
@_ && Types::TypeTiny::is_TypeTiny($_[-1]) && $_[-1]->is_strictly_a_type_of($_Slurpy) ? : 21 46 67
Condition100%6/6
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
!ll&&!rl&&r
44221
@_ and Types::TypeTiny::is_TypeTiny($_[-1])
!ll&&!rl&&r
2263
MC/DC100%3/3
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
@_ Types::TypeTiny::is_TypeTiny($_[-1]) $_[-1]->is_strictly_a_type_of($_Slurpy)
Truth table100%4/4
A and B and C
A= @_B= Types::TypeTiny::is_TypeTiny($_[-1])C= $_[-1]->is_strictly_a_type_of($_Slurpy)
ABCresult
0XX0
10X0
1100
1111
86 @_
87 && Types::TypeTiny::is_TypeTiny( $_[-1] )
88 && $_[-1]->is_strictly_a_type_of( $_Slurpy )
89 ? pop->my_unslurpy
90 : undef;
91 67 my $iterator = pair_iterator @_;
92 66 my %constraints;
93 my %is_optional;
94 66 my @keys;
95
96 66 ▶ while ( my ( $k, $v ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 107 64 171
97 107 $constraints{$k} = $v;
98 107 ▶ Types::TypeTiny::is_TypeTiny( $v )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($v) or &_croak("Parameter for Dict[...] with key '${k}' expected to be a type constraint; got $v") 106 1 107
99 or _croak(
100 "Parameter for Dict[...] with key '$k' expected to be a type constraint; got $v"
101 );
102 106 ▶ Types::TypeTiny::is_StringLike( $k )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_StringLike($k) or &_croak("Key for Dict[...] expected to be string; got $k") 105 1 106
103 or _croak( "Key for Dict[...] expected to be string; got $k" );
104 105 push @keys, $k;
105 105 $is_optional{$k} = !!$constraints{$k}->is_strictly_a_type_of( $_optional );
106 } #/ while ( my ( $k, $v ) = $iterator...)
107
108 124 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
109 124 my $value = $_[0];
110 124 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 86 38 124
111 86 ▶ my %tmp = map +( exists( $constraints{$_} ) ? () : ( $_ => $value->{$_} ) ),
Branch: 1 branch
BranchTrueFalseTotal
exists $constraints{$_} ? : 136 57 193
112 keys %$value;
113 86 ▶ return unless $slurpy->check( \%tmp );
Branch: 1 branch
BranchTrueFalseTotal
unless $slurpy->check(\%tmp) 22 64 86
114 }
115 else {
116 38 ▶ exists( $constraints{$_} ) || return for sort keys %$value;
Branch: 1 branch
BranchTrueFalseTotal
exists $constraints{$_} or return 60 8 68
117 }
118 94 for my $k ( @keys ) {
119 153 ▶ exists( $value->{$k} ) or ( $is_optional{$k} ? next : return );
Branch: 2 branches
BranchTrueFalseTotal
exists $value->{$k} or $is_optional{$k} ? next : (return) 126 27 153
$is_optional{$k} ? : 10 17 27
120 126 ▶ $constraints{$k}->check( $value->{$k} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$constraints{$k}->check($value->{$k}) or return 87 39 126
121 }
122 38 return !!1;
123 64 };
124 } #/ sub __constraint_generator
125  
126 64 ▶sub __inline_generator {
Subroutine __inline_generator: called CC=14 SCAR=26.6
127  
128 # We can only inline a parameterized Dict if all the
129 # constraints inside can be inlined.
130
131 64 ▶ my $slurpy =
Branch: 1 branch
BranchTrueFalseTotal
@_ && Types::TypeTiny::is_TypeTiny($_[-1]) && $_[-1]->is_strictly_a_type_of($_Slurpy) ? : 21 43 64
Condition83%5/6
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
!ll&&!rl&&r
24121
@_ and Types::TypeTiny::is_TypeTiny($_[-1])
!ll&&!rl&&r
2062
MC/DC66%2/3
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
@_ Types::TypeTiny::is_TypeTiny($_[-1]) $_[-1]->is_strictly_a_type_of($_Slurpy)
Truth table75%3/4
A and B and C
A= @_B= Types::TypeTiny::is_TypeTiny($_[-1])C= $_[-1]->is_strictly_a_type_of($_Slurpy)
ABCresult
0XX0
10X0
1100
1111
132 @_
133 && Types::TypeTiny::is_TypeTiny( $_[-1] )
134 && $_[-1]->is_strictly_a_type_of( $_Slurpy )
135 ? pop->my_unslurpy
136 : undef;
137 64 ▶ return if $slurpy && !$slurpy->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy and not $slurpy->can_be_inlined 0 64 64
Condition66%2/3
$slurpy and not $slurpy->can_be_inlined
!ll&&!rl&&r
43210
MC/DC0%0/2
$slurpy and not $slurpy->can_be_inlined
$slurpy not $slurpy->can_be_inlined
Truth table66%2/3
A and not(B)
A= $slurpyB= not $slurpy->can_be_inlined
ABresult
0X0
100
111
138
139 # Is slurpy a very loose type constraint?
140 # i.e. Any, Item, Defined, Ref, or HashRef
141 64 ▶ my $slurpy_is_any = $slurpy && $_hash->is_a_type_of( $slurpy );
Condition100%3/3
$slurpy && $_hash->is_a_type_of($slurpy)
!ll&&!rl&&r
43183
MC/DC100%2/2
$slurpy && $_hash->is_a_type_of($slurpy)
$slurpy $_hash->is_a_type_of($slurpy)
Truth table100%3/3
A && B
A= $slurpyB= $_hash->is_a_type_of($slurpy)
ABresult
0X0
100
111
142
143 # Is slurpy a parameterized Map, or expressible as a parameterized Map?
144 64 ▶ my $slurpy_is_map =
Condition71%10/14
$slurpy && $slurpy->is_parameterized && ($slurpy->parent->strictly_equals($_map) && $slurpy->parameters || $slurpy->parent->strictly_equals($_hash) && [$_any, $slurpy->parameters->[0]])
!ll&&!rl&&r
51013
$slurpy and $slurpy->is_parameterized
!ll&&!rl&&r
43813
$slurpy->parent->strictly_equals($_map) and $slurpy->parameters or $slurpy->parent->strictly_equals($_hash) and [$_any, $slurpy->parameters->[0]]
l!l&&r!l&&!r
760
$slurpy->parent->strictly_equals($_map) and $slurpy->parameters
!ll&&!rl&&r
607
$slurpy->parent->strictly_equals($_hash) and [$_any, $slurpy->parameters->[0]]
!ll
06
MC/DC40%2/5
$slurpy && $slurpy->is_parameterized && ($slurpy->parent->strictly_equals($_map) && $slurpy->parameters || $slurpy->parent->strictly_equals($_hash) && [$_any, $slurpy->parameters->[0]])
$slurpy $slurpy->is_parameterized $slurpy->parent->strictly_equals($_map) $slurpy->parameters $slurpy->parent->strictly_equals($_hash)
Truth table57%4/7
A and B && C and D or E
A= $slurpyB= $slurpy->is_parameterizedC= $slurpy->parent->strictly_equals($_map)D= $slurpy->parametersE= $slurpy->parent->strictly_equals($_hash)
ABCDEresult
0XXXX0
10XXX0
110X00
111000
1111X1
110X11
111011
145 $slurpy
146 && $slurpy->is_parameterized
147 && (
148 ( $slurpy->parent->strictly_equals( $_map ) && $slurpy->parameters )
149 || ( $slurpy->parent->strictly_equals( $_hash )
150 && [ $_any, $slurpy->parameters->[0] ] )
151 );
152
153 64 my $iterator = pair_iterator @_;
154 64 my %constraints;
155 my @keys;
156
157 64 ▶ while ( my ( $k, $c ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 103 61 164
158 103 ▶ return unless $c->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->can_be_inlined 3 100 103
159 100 $constraints{$k} = $c;
160 100 push @keys, $k;
161 }
162
163 61 my $regexp = join "|", map quotemeta, @keys;
164 518 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
165 518 require B;
166 518 my $h = $_[1];
167 join " and ",
168 Types::Standard::HashRef->inline_check( $h ),
169 (
170 $slurpy_is_any
171 ? ()
172 : $slurpy_is_map ? do {
173 171 ▶ '(not grep {' . "my \$v = ($h)->{\$_};" . sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$slurpy ? : 30 272 302
174 'not((/\\A(?:%s)\\z/) or ((%s) and (%s)))',
175 $regexp,
176 $slurpy_is_map->[0]->inline_check( '$_' ),
177 $slurpy_is_map->[1]->inline_check( '$v' ),
178 ) . "} keys \%{$h})";
179 }
180 : $slurpy ? do {
181 30 'do {'
182 . "my \$slurpy_tmp = +{ map /\\A(?:$regexp)\\z/ ? () : (\$_ => ($h)->{\$_}), keys \%{$h} };"
183 . $slurpy->inline_check( '$slurpy_tmp' ) . '}';
184 }
185 : "not(grep !/\\A(?:$regexp)\\z/, keys \%{$h})"
186 ),
187 (
188 map {
189 518 ▶ my $k = B::perlstring( $_ );
Branch: 2 branches
BranchTrueFalseTotal
$slurpy_is_any ? : 45 473 518
$slurpy_is_map ? : 171 302 473
190 $constraints{$_}->is_strictly_a_type_of( $_optional )
191 ? sprintf(
192 '(!exists %s->{%s} or %s)', $h, $k,
193 $constraints{$_}->inline_check( "$h\->{$k}" )
194 )
195 : (
196 "exists($h\->{$k})",
197 858 ▶ $constraints{$_}->inline_check( "$h\->{$k}" )
Branch: 1 branch
BranchTrueFalseTotal
$constraints{$_}->is_strictly_a_type_of($_optional) ? : 218 640 858
198 )
199 } @keys
200 ),
201 ;
202 }
203 61 } #/ sub __inline_generator
204  
205 8 ▶sub __deep_explanation {
Subroutine __deep_explanation: called CC=16 SCAR=30.2
206 8 require B;
207 8 my ( $type, $value, $varname ) = @_;
208 8 my @params = @{ $type->parameters };
209
210 8 ▶ my $slurpy =
Branch: 1 branch
BranchTrueFalseTotal
@params && Types::TypeTiny::is_TypeTiny($params[-1]) && $params[-1]->is_strictly_a_type_of($_Slurpy) ? : 0 8 8
Condition33%2/6
@params and Types::TypeTiny::is_TypeTiny($params[-1]) and $params[-1]->is_strictly_a_type_of($_Slurpy)
!ll&&!rl&&r
080
@params and Types::TypeTiny::is_TypeTiny($params[-1])
!ll&&!rl&&r
008
MC/DC0%0/3
@params and Types::TypeTiny::is_TypeTiny($params[-1]) and $params[-1]->is_strictly_a_type_of($_Slurpy)
@params Types::TypeTiny::is_TypeTiny($params[-1]) $params[-1]->is_strictly_a_type_of($_Slurpy)
Truth table25%1/4
A and B and C
A= @paramsB= Types::TypeTiny::is_TypeTiny($params[-1])C= $params[-1]->is_strictly_a_type_of($_Slurpy)
ABCresult
0XX0
10X0
1100
1111
211 @params
212 && Types::TypeTiny::is_TypeTiny( $params[-1] )
213 && $params[-1]->is_strictly_a_type_of( $_Slurpy )
214 ? pop( @params )->my_unslurpy
215 : undef;
216 8 my $iterator = pair_iterator @params;
217 8 my %constraints;
218 my @keys;
219
220 8 ▶ while ( my ( $k, $c ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 17 8 25
221 17 push @keys, $k;
222 17 $constraints{$k} = $c;
223 }
224
225 8 for my $k ( @keys ) {
226 next
227 if $constraints{$k}->has_parent
228 && ( $constraints{$k}->parent == Types::Standard::Optional )
229 16 ▶ && ( !exists $value->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
if $constraints{$k}->has_parent and $constraints{$k}->parent == &Types::Standard::Optional() and not exists $value->{$k} 2 14 16
Condition100%6/6
$constraints{$k}->has_parent and $constraints{$k}->parent == &Types::Standard::Optional() and not exists $value->{$k}
!ll&&!rl&&r
1222
$constraints{$k}->has_parent and $constraints{$k}->parent == &Types::Standard::Optional()
!ll&&!rl&&r
1114
MC/DC100%3/3
$constraints{$k}->has_parent and $constraints{$k}->parent == &Types::Standard::Optional() and not exists $value->{$k}
$constraints{$k}->has_parent $constraints{$k}->parent == &Types::Standard::Optional() not exists $value->{$k}
Truth table100%4/4
A and B and not(C)
A= $constraints{$k}->has_parentB= $constraints{$k}->parent == &Types::Standard::Optional()C= not exists $value->{$k}
ABCresult
0XX0
10X0
1100
1111
230 14 ▶ next if $constraints{$k}->check( $value->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
if $constraints{$k}->check($value->{$k}) 9 5 14
231
232 return [
233 sprintf( '"%s" requires key %s to appear in hash', $type, B::perlstring( $k ) )
234 ]
235 5 ▶ unless exists $value->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $value->{$k} 3 2 5
236
237 return [
238 sprintf(
239 '"%s" constrains value at key %s of hash with "%s"',
240 $type,
241 B::perlstring( $k ),
242 $constraints{$k},
243 ),
244 @{
245 2 $constraints{$k}->validate_explain(
246 2 $value->{$k},
247 sprintf( '%s->{%s}', $varname, B::perlstring( $k ) ),
248 )
249 },
250 ];
251 } #/ for my $k ( @keys )
252
253 3 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 0 3 3
254 0 ▶ my %tmp = map { exists( $constraints{$_} ) ? () : ( $_ => $value->{$_} ) }
Branch: 1 branch
BranchTrueFalseTotal
exists $constraints{$_} ? : 0 0 0
255 keys %$value;
256
257 0 my $explain = $slurpy->validate_explain( \%tmp, '$slurpy' );
258 return [
259 0 ▶ sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $explain 0 0 0
260 '"%s" requires the hashref of additional key/value pairs to conform to "%s"',
261 $type, $slurpy
262 ),
263 @$explain,
264 ] if $explain;
265 } #/ if ( $slurpy )
266 else {
267 3 for my $k ( sort keys %$value ) {
268 return [
269 sprintf(
270 '"%s" does not allow key %s to appear in hash', $type, B::perlstring( $k )
271 )
272 ]
273 6 ▶ unless exists $constraints{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $constraints{$k} 3 3 6
274 }
275 } #/ else [ if ( $slurpy ) ]
276
277 # This should never happen...
278 0 return; # uncoverable statement
279 } #/ sub __deep_explanation
280  
281 22 my $label_counter = 0;
282 22 our ( $keycheck_counter, @KEYCHECK ) = -1;
283  
284 32 ▶sub __coercion_generator {
Subroutine __coercion_generator: called CC=18 SCAR=29.0
285 32 ▶ my $slurpy =
Branch: 1 branch
BranchTrueFalseTotal
@_ && Types::TypeTiny::is_TypeTiny($_[-1]) && $_[-1]->is_strictly_a_type_of($_Slurpy) ? : 6 26 32
Condition50%3/6
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
!ll&&!rl&&r
0266
@_ and Types::TypeTiny::is_TypeTiny($_[-1])
!ll&&!rl&&r
0032
MC/DC33%1/3
@_ and Types::TypeTiny::is_TypeTiny($_[-1]) and $_[-1]->is_strictly_a_type_of($_Slurpy)
@_ Types::TypeTiny::is_TypeTiny($_[-1]) $_[-1]->is_strictly_a_type_of($_Slurpy)
Truth table50%2/4
A and B and C
A= @_B= Types::TypeTiny::is_TypeTiny($_[-1])C= $_[-1]->is_strictly_a_type_of($_Slurpy)
ABCresult
0XX0
10X0
1100
1111
286 @_
287 && Types::TypeTiny::is_TypeTiny( $_[-1] )
288 && $_[-1]->is_strictly_a_type_of( $_Slurpy )
289 ? pop->my_unslurpy
290 : undef;
291 32 my ( $parent, $child, %dict ) = @_;
292 32 my $C = "Type::Coercion"->new( type_constraint => $child );
293
294 32 my $all_inlinable = 1;
295 32 my $child_coercions_exist = 0;
296 32 for my $tc ( values %dict ) {
297 59 ▶ $all_inlinable = 0 if !$tc->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->can_be_inlined 1 58 59
298 59 ▶ $all_inlinable = 0 if $tc->has_coercion && !$tc->coercion->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
if $tc->has_coercion and not $tc->coercion->can_be_inlined 6 53 59
Condition100%3/3
$tc->has_coercion and not $tc->coercion->can_be_inlined
!ll&&!rl&&r
32216
MC/DC100%2/2
$tc->has_coercion and not $tc->coercion->can_be_inlined
$tc->has_coercion not $tc->coercion->can_be_inlined
Truth table100%3/3
A and not(B)
A= $tc->has_coercionB= not $tc->coercion->can_be_inlined
ABresult
0X0
100
111
299 59 ▶ $child_coercions_exist++ if $tc->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
if $tc->has_coercion 27 32 59
300 }
301 32 ▶ $all_inlinable = 0 if $slurpy && !$slurpy->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy and not $slurpy->can_be_inlined 0 32 32
Condition66%2/3
$slurpy and not $slurpy->can_be_inlined
!ll&&!rl&&r
2660
MC/DC0%0/2
$slurpy and not $slurpy->can_be_inlined
$slurpy not $slurpy->can_be_inlined
Truth table66%2/3
A and not(B)
A= $slurpyB= not $slurpy->can_be_inlined
ABresult
0X0
100
111
302 32 ▶ $all_inlinable = 0
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy and $slurpy->has_coercion and not $slurpy->coercion->can_be_inlined 1 31 32
Condition100%6/6
$slurpy and $slurpy->has_coercion and not $slurpy->coercion->can_be_inlined
!ll&&!rl&&r
2831
$slurpy and $slurpy->has_coercion
!ll&&!rl&&r
2624
MC/DC100%3/3
$slurpy and $slurpy->has_coercion and not $slurpy->coercion->can_be_inlined
$slurpy $slurpy->has_coercion not $slurpy->coercion->can_be_inlined
Truth table100%4/4
A and B and not(C)
A= $slurpyB= $slurpy->has_coercionC= not $slurpy->coercion->can_be_inlined
ABCresult
0XX0
10X0
1100
1111
303 if $slurpy
304 && $slurpy->has_coercion
305 && !$slurpy->coercion->can_be_inlined;
306
307 32 ▶ $child_coercions_exist++ if $slurpy && $slurpy->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy and $slurpy->has_coercion 4 28 32
Condition100%3/3
$slurpy and $slurpy->has_coercion
!ll&&!rl&&r
2624
MC/DC100%2/2
$slurpy and $slurpy->has_coercion
$slurpy $slurpy->has_coercion
Truth table100%3/3
A and B
A= $slurpyB= $slurpy->has_coercion
ABresult
0X0
100
111
308 32 ▶ return unless $child_coercions_exist;
Branch: 1 branch
BranchTrueFalseTotal
unless $child_coercions_exist 14 18 32
309
310 18 ▶ if ( $all_inlinable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_inlinable) { } 12 6 18
311 $C->add_type_coercions(
312 11 ▶ $parent => Types::Standard::Stringable {
Subroutine __ANON__: called CC=6 SCAR=17.9
313 11 require B;
314
315 my $keycheck = join "|", map quotemeta,
316 11 ▶ sort { length( $b ) <=> length( $a ) or $a cmp $b } keys %dict;
Condition66%2/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
560
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table66%2/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
317 11 $keycheck = $KEYCHECK[ ++$keycheck_counter ] = qr{^($keycheck)$}ms; # regexp for legal keys
318
319 11 my $label = sprintf( "DICTLABEL%d", ++$label_counter );
320 11 my @code;
321 11 push @code, 'do { my ($orig, $return_orig, $tmp, %new) = ($_, 0);';
322 11 push @code, "$label: {";
323 11 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 4 7 11
324 4 push @code,
325 sprintf(
326 'my $slurped = +{ map +($_=~$%s::KEYCHECK[%d])?():($_=>$orig->{$_}), keys %%$orig };',
327 __PACKAGE__, $keycheck_counter
328 );
329 4 ▶ if ( $slurpy->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy->has_coercion) { } 3 1 4
330 3 push @code,
331 sprintf(
332 'my $coerced = %s;',
333 $slurpy->coercion->inline_coercion( '$slurped' )
334 );
335 3 push @code,
336 sprintf(
337 '((%s)&&(%s))?(%%new=%%$coerced):(($return_orig = 1), last %s);',
338 $_hash->inline_check( '$coerced' ), $slurpy->inline_check( '$coerced' ),
339 $label
340 );
341 } #/ if ( $slurpy->has_coercion)
342 else {
343 1 push @code,
344 sprintf(
345 '(%s)?(%%new=%%$slurped):(($return_orig = 1), last %s);',
346 $slurpy->inline_check( '$slurped' ), $label
347 );
348 }
349 } #/ if ( $slurpy )
350 else {
351 7 push @code,
352 sprintf(
353 '($_ =~ $%s::KEYCHECK[%d])||(($return_orig = 1), last %s) for sort keys %%$orig;',
354 __PACKAGE__, $keycheck_counter, $label
355 );
356 }
357 11 for my $k ( keys %dict ) {
358 20 my $ct = $dict{$k};
359 20 my $ct_coerce = $ct->has_coercion;
360 20 my $ct_optional = $ct->is_a_type_of( $_optional );
361 20 my $K = B::perlstring( $k );
362
363 20 ▶ push @code, sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$ct_coerce ? : 18 2 20
364 'if (exists $orig->{%s}) { $tmp = %s; (%s) ? ($new{%s}=$tmp) : (($return_orig=1), last %s) }',
365 $K,
366 $ct_coerce
367 ? $ct->coercion->inline_coercion( "\$orig->{$K}" )
368 : "\$orig->{$K}",
369 $ct->inline_check( '$tmp' ),
370 $K,
371 $label,
372 );
373 } #/ for my $k ( keys %dict )
374 11 push @code, '}';
375 11 push @code, '$return_orig ? $orig : \\%new';
376 11 push @code, '}';
377
378 #warn "CODE:: @code";
379 11 "@code";
380 }
381 12 );
382 } #/ if ( $all_inlinable )
383
384 else {
385 my %is_optional = map {
386 6 ;
387 12 $_ => !!$dict{$_}->is_strictly_a_type_of( $_optional )
388 } sort keys %dict;
389 $C->add_type_coercions(
390 7 ▶ $parent => sub {
Subroutine __ANON__: called CC=14 SCAR=26.5
391 7 ▶ my $value = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 7 0 7
392 7 my %new;
393
394 7 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 3 4 7
395 3 ▶ my %slurped = map exists( $dict{$_} ) ? () : ( $_ => $value->{$_} ),
Branch: 1 branch
BranchTrueFalseTotal
exists $dict{$_} ? : 3 4 7
396 keys %$value;
397
398 3 ▶ if ( $slurpy->check( \%slurped ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slurpy->check(\%slurped)) { } 1 2 3
elsif ($slurpy->has_coercion) { } 1 1 2
399 1 %new = %slurped;
400 }
401 elsif ( $slurpy->has_coercion ) {
402 1 my $coerced = $slurpy->coerce( \%slurped );
403 1 ▶ $slurpy->check( $coerced ) ? ( %new = %$coerced ) : ( return $value );
Branch: 1 branch
BranchTrueFalseTotal
$slurpy->check($coerced) ? : 1 0 1
404 }
405 else {
406 1 return $value;
407 }
408 } #/ if ( $slurpy )
409 else {
410 4 for my $k ( keys %$value ) {
411 8 ▶ return $value unless exists $dict{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dict{$k} 0 8 8
412 }
413 }
414
415 6 for my $k ( keys %dict ) {
416 12 ▶ next if $is_optional{$k} and not exists $value->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $is_optional{$k} and not exists $value->{$k} 2 10 12
Condition100%3/3
$is_optional{$k} and not exists $value->{$k}
!ll&&!rl&&r
552
MC/DC100%2/2
$is_optional{$k} and not exists $value->{$k}
$is_optional{$k} not exists $value->{$k}
Truth table100%3/3
A and not(B)
A= $is_optional{$k}B= not exists $value->{$k}
ABresult
0X0
100
111
417
418 10 my $ct = $dict{$k};
419 10 ▶ my $x = $ct->has_coercion ? $ct->coerce( $value->{$k} ) : $value->{$k};
Branch: 1 branch
BranchTrueFalseTotal
$ct->has_coercion ? : 7 3 10
420
421 10 ▶ return $value unless $ct->check( $x );
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->check($x) 0 10 10
422
423 10 $new{$k} = $x;
424 } #/ for my $k ( keys %dict )
425
426 6 return \%new;
427 },
428 6 );
429 } #/ else [ if ( $all_inlinable ) ]
430
431 18 return $C;
432 } #/ sub __coercion_generator
433  
434 97 ▶sub __dict_is_slurpy {
Subroutine my_dict_is_slurpy: called CC=5 SCAR=16.3
435 97 my $self = shift;
436
437 97 ▶ return !!0 if $self == Types::Standard::Dict();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::Dict() 0 97 97
438
439 my $dict = $self->find_parent(
440 97 ▶ sub { $_->has_parent && $_->parent == Types::Standard::Dict() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::Dict() 99 0 99
441 my $slurpy =
442 97 ▶ @{ $dict->parameters }
Branch: 1 branch
BranchTrueFalseTotal
@{$dict->parameters;} && Types::TypeTiny::is_TypeTiny($dict->parameters->[-1]) && $dict->parameters->[-1]->is_strictly_a_type_of($_Slurpy) ? : 63 34 97
Condition83%5/6
@{$dict->parameters;} and Types::TypeTiny::is_TypeTiny($dict->parameters->[-1]) and $dict->parameters->[-1]->is_strictly_a_type_of($_Slurpy)
!ll&&!rl&&r
72763
@{$dict->parameters;} and Types::TypeTiny::is_TypeTiny($dict->parameters->[-1])
!ll&&!rl&&r
7090
MC/DC66%2/3
@{$dict->parameters;} and Types::TypeTiny::is_TypeTiny($dict->parameters->[-1]) and $dict->parameters->[-1]->is_strictly_a_type_of($_Slurpy)
@{$dict->parameters;} Types::TypeTiny::is_TypeTiny($dict->parameters->[-1]) $dict->parameters->[-1]->is_strictly_a_type_of($_Slurpy)
Truth table75%3/4
A and B and C
A= @{$dict->parameters;}B= Types::TypeTiny::is_TypeTiny($dict->parameters->[-1])C= $dict->parameters->[-1]->is_strictly_a_type_of($_Slurpy)
ABCresult
0XX0
10X0
1100
1111
443 && Types::TypeTiny::is_TypeTiny( $dict->parameters->[-1] )
444 && $dict->parameters->[-1]->is_strictly_a_type_of( $_Slurpy )
445 ? $dict->parameters->[-1]
446 : undef;
447 } #/ sub __dict_is_slurpy
448  
449 75 ▶sub __hashref_allows_key {
Subroutine my_hashref_allows_key: called CC=10 SCAR=23.1
450 75 my $self = shift;
451 75 my ( $key ) = @_;
452
453 75 ▶ return Types::Standard::is_Str( $key ) if $self == Types::Standard::Dict();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::Dict() 6 69 75
454
455 my $dict = $self->find_parent(
456 69 ▶ sub { $_->has_parent && $_->parent == Types::Standard::Dict() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::Dict() 92 0 92
457 69 my %params;
458 69 my $slurpy = $dict->my_dict_is_slurpy;
459 69 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 47 22 69
460 47 my @args = @{ $dict->parameters };
461 47 pop @args;
462 47 %params = @args;
463 47 $slurpy = $slurpy->my_unslurpy;
464 }
465 else {
466 22 %params = @{ $dict->parameters };
467 }
468
469 return !!1
470 69 ▶ if exists( $params{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 23 46 69
471 46 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
unless $slurpy 14 32 46
472 if !$slurpy;
473 32 ▶ return Types::Standard::is_Str( $key )
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref() 12 20 32
Condition77%7/9
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref()
l!l&&r!l&&!r
6620
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined()
l!l&&r!l&&!r
6026
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item()
l!l&&r!l&&!r
6026
MC/DC50%2/4
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref()
$slurpy == &Types::Standard::Any() $slurpy == &Types::Standard::Item() $slurpy == &Types::Standard::Defined() $slurpy == &Types::Standard::Ref()
Truth table60%3/5
A or B or C or D
A= $slurpy == &Types::Standard::Any()B= $slurpy == &Types::Standard::Item()C= $slurpy == &Types::Standard::Defined()D= $slurpy == &Types::Standard::Ref()
ABCDresult
1XXX1
01XX1
001X1
00011
00000
474 if $slurpy == Types::Standard::Any()
475 || $slurpy == Types::Standard::Item()
476 || $slurpy == Types::Standard::Defined()
477 || $slurpy == Types::Standard::Ref();
478 20 ▶ return $slurpy->my_hashref_allows_key( $key )
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy->is_a_type_of(&Types::Standard::HashRef()) 18 2 20
479 if $slurpy->is_a_type_of( Types::Standard::HashRef() );
480 2 return !!0;
481 } #/ sub __hashref_allows_key
482  
483 18 ▶sub __hashref_allows_value {
Subroutine my_hashref_allows_value: called CC=12 SCAR=26.1
484 18 my $self = shift;
485 18 my ( $key, $value ) = @_;
486
487 18 ▶ return !!0 unless $self->my_hashref_allows_key( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->my_hashref_allows_key($key) 2 16 18
488 16 ▶ return !!1 if $self == Types::Standard::Dict();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::Dict() 0 16 16
489
490 my $dict = $self->find_parent(
491 16 ▶ sub { $_->has_parent && $_->parent == Types::Standard::Dict() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::Dict() 18 0 18
492 16 my %params;
493 16 my $slurpy = $dict->my_dict_is_slurpy;
494 16 ▶ if ( $slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy) { } 12 4 16
495 12 my @args = @{ $dict->parameters };
496 12 pop @args;
497 12 %params = @args;
498 12 $slurpy = $slurpy->my_unslurpy;
499 }
500 else {
501 4 %params = @{ $dict->parameters };
502 }
503
504 return !!1
505 16 ▶ if exists( $params{$key} ) && $params{$key}->check( $value );
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} and $params{$key}->check($value) 6 10 16
Condition100%3/3
exists $params{$key} and $params{$key}->check($value)
!ll&&!rl&&r
556
MC/DC100%2/2
exists $params{$key} and $params{$key}->check($value)
exists $params{$key} $params{$key}->check($value)
Truth table100%3/3
A and B
A= exists $params{$key}B= $params{$key}->check($value)
ABresult
0X0
100
111
506 10 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
unless $slurpy 2 8 10
507 if !$slurpy;
508 8 ▶ return !!1
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref() 0 8 8
Condition33%3/9
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref()
l!l&&r!l&&!r
008
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined()
l!l&&r!l&&!r
008
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item()
l!l&&r!l&&!r
008
MC/DC0%0/4
$slurpy == &Types::Standard::Any() or $slurpy == &Types::Standard::Item() or $slurpy == &Types::Standard::Defined() or $slurpy == &Types::Standard::Ref()
$slurpy == &Types::Standard::Any() $slurpy == &Types::Standard::Item() $slurpy == &Types::Standard::Defined() $slurpy == &Types::Standard::Ref()
Truth table20%1/5
A or B or C or D
A= $slurpy == &Types::Standard::Any()B= $slurpy == &Types::Standard::Item()C= $slurpy == &Types::Standard::Defined()D= $slurpy == &Types::Standard::Ref()
ABCDresult
1XXX1
01XX1
001X1
00011
00000
509 if $slurpy == Types::Standard::Any()
510 || $slurpy == Types::Standard::Item()
511 || $slurpy == Types::Standard::Defined()
512 || $slurpy == Types::Standard::Ref();
513 8 ▶ return $slurpy->my_hashref_allows_value( $key, $value )
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy->is_a_type_of(&Types::Standard::HashRef()) 8 0 8
514 if $slurpy->is_a_type_of( Types::Standard::HashRef() );
515 0 return !!0;
516 } #/ sub __hashref_allows_value
517  
518 3 ▶sub combine {
Subroutine combine: called CC=10 SCAR=23.0
Pod documented
519 3 require Type::Tiny::Union;
520 3 my @key_order;
521 my %keys;
522 3 my @slurpy;
523
524 3 for my $dict ( @_ ) {
525 8 ▶ Types::TypeTiny::is_TypeTiny( $dict ) && $dict->is_a_type_of( Types::Standard::Dict() )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($dict) and $dict->is_a_type_of(&Types::Standard::Dict()) or &_croak("Unexpected non-Dict argument: $dict") 8 0 8
Condition33%1/3
Types::TypeTiny::is_TypeTiny($dict) and $dict->is_a_type_of(&Types::Standard::Dict())
!ll&&!rl&&r
008
MC/DC0%0/2
Types::TypeTiny::is_TypeTiny($dict) and $dict->is_a_type_of(&Types::Standard::Dict())
Types::TypeTiny::is_TypeTiny($dict) $dict->is_a_type_of(&Types::Standard::Dict())
Truth table33%1/3
A and B
A= Types::TypeTiny::is_TypeTiny($dict)B= $dict->is_a_type_of(&Types::Standard::Dict())
ABresult
0X0
100
111
526 or _croak "Unexpected non-Dict argument: $dict";
527
528 8 my @args;
529 8 ▶ if ( my $s = $dict->my_dict_is_slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $dict->my_dict_is_slurpy) { } 2 6 8
530 2 @args = @{ $dict->parameters };
531 2 pop @args;
532 2 push @slurpy, $s->my_unslurpy;
533 }
534 else {
535 6 @args = @{ $dict->parameters };
536 }
537
538 8 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 8 16
539 8 my ( $key, $type ) = splice @args, 0, 2;
540 8 ▶ if ( not exists $keys{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $keys{$key}) 5 3 8
541 5 push @key_order, $key;
542 5 $keys{$key} = [];
543 }
544 8 push @{ $keys{$key} }, $type;
545 }
546 }
547
548 3 my @args;
549 3 for my $key ( @key_order ) {
550 5 ▶ if ( @{ $keys{$key} } == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$keys{$key};} == 1) { } 2 3 5
551 2 push @args, $key => $keys{$key}[0];
552 }
553 else {
554 3 my %seen;
555 3 my @uniq = grep { not $seen{$_->{uniq}}++ } @{ $keys{$key} };
556 3 my $union = 'Type::Tiny::Union'->new( type_constraints => \@uniq );
557 3 push @args, $key => $union;
558 }
559 }
560
561 3 ▶ if ( @slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@slurpy) 1 2 3
562 1 my %seen;
563 1 my @uniq = grep { not $seen{$_->{uniq}}++ } @slurpy;
564 1 my $union = 'Type::Tiny::Union'->new( type_constraints => \@uniq );
565 1 push @args, Types::Standard::Slurpy->of( $union );
566 }
567
568 3 return Types::Standard::Dict->of( @args );
569 }
570  
571 22 1;
572  
573 __END__
« blib/lib/Type/Tiny.pm ↑ Summary lib/Type/Parser.pm »