File Coverage

blib/lib/Specio/Library/Structured/Dict.pm
Criterion Covered Total %
statement 69 70 98.5
branch 14 18 77.7
condition 3 6 50.0
subroutine 13 13 100.0
pod 0 1 0.0
total 99 108 91.6


line stmt bran cond sub pod time code
1             package Specio::Library::Structured::Dict;
2              
3 4     4   31 use strict;
  4         9  
  4         126  
4 4     4   20 use warnings;
  4         10  
  4         204  
5              
6             our $VERSION = '0.47';
7              
8 4     4   25 use Carp qw( confess );
  4         8  
  4         232  
9 4     4   28 use List::Util ();
  4         7  
  4         111  
10 4     4   23 use Scalar::Util qw( blessed );
  4         9  
  4         205  
11 4     4   30 use Specio::Helpers qw( perlstring );
  4         11  
  4         192  
12 4     4   24 use Specio::Library::Builtins;
  4         15  
  4         32  
13 4     4   27 use Specio::TypeChecks qw( does_role );
  4         10  
  4         3426  
14              
15             my $hashref = t('HashRef');
16              
17 4     4 0 37 sub parent {$hashref}
18              
19             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
20             sub _inline {
21 1     1   11 $hashref->inline_check( $_[1] );
22             }
23              
24             sub _parameterization_args_builder {
25 4     4   56 shift;
26 4         9 my $args = shift;
27              
28 4   66     56 for my $p ( ( $args->{slurpy} || () ), values %{ $args->{kv} } ) {
  4         20  
29 11         47 my $type;
30 11 100       35 if ( blessed($p) ) {
31 9         14 $type = $p;
32             }
33             else {
34 2 50 33     13 if ( ref $p eq 'HASH' && $p->{optional} ) {
35 2         4 $type = $p->{optional};
36             }
37             else {
38 0         0 confess
39             'Can only pass types, optional types, and slurpy types when defining a Dict';
40             }
41             }
42              
43 11 50       88 does_role( $type, 'Specio::Constraint::Role::Interface' )
44             or confess
45             'All parameters passed to ->parameterize must be objects which do the Specio::Constraint::Role::Interface role';
46              
47 11 50       162 confess
48             'All parameters passed to ->parameterize must be inlinable constraints'
49             unless $type->can_be_inlined;
50             }
51              
52 4         27 return %{$args};
  4         26  
53             }
54              
55             sub _name_builder {
56 4     4   21 my $self = shift;
57 4         8 my $p = shift;
58              
59             ## no critic (Subroutines::ProtectPrivateSubs)
60 4         7 my @kv;
61 4         9 for my $k ( sort keys %{ $p->{kv} } ) {
  4         26  
62 10         62 my $v = $p->{kv}{$k};
63 10 100       33 if ( blessed($v) ) {
    50          
64 8         28 push @kv, "$k => " . $self->_name_or_anon($v);
65             }
66             elsif ( $v->{optional} ) {
67             push @kv,
68 2         8 "$k => " . $self->_name_or_anon( $v->{optional} ) . '?';
69             }
70             }
71              
72 4 100       41 if ( $p->{slurpy} ) {
73 1         4 push @kv, $self->_name_or_anon( $p->{slurpy} ) . '...';
74             }
75              
76 4         40 return 'Dict{ ' . ( join ', ', @kv ) . ' }';
77             }
78              
79             sub _structured_inline_generator {
80 9     9   14 shift;
81 9         20 my $val = shift;
82 9         27 my %args = @_;
83              
84 9         39 my @code = sprintf( '( %s )', $hashref->_inline_check($val) );
85              
86 9         20 for my $k ( sort keys %{ $args{kv} } ) {
  9         58  
87 24         55 my $p = $args{kv}{$k};
88 24         94 my $access = sprintf( '%s->{%s}', $val, perlstring($k) );
89              
90 24 100       83 if ( !blessed($p) ) {
91 6         14 my $type = $p->{optional};
92              
93 6         18 push @code,
94             sprintf(
95             '( exists %s ? ( %s ) : 1 )',
96             $access, $type->_inline_check($access)
97             );
98             }
99             else {
100 18         55 push @code, sprintf( '( %s )', $p->_inline_check($access) );
101             }
102             }
103              
104 9 100       33 if ( $args{slurpy} ) {
105 3         6 my $check
106             = '( do { my %%_____known_____ = map { $_ => 1 } ( %s ); List::Util::all { %s } grep { ! $_____known_____{$_} } sort keys %%{ %s } } )';
107             push @code,
108             sprintf(
109             $check,
110 9         35 ( join ', ', map { perlstring($_) } keys %{ $args{kv} } ),
  3         7  
111 3         8 $args{slurpy}->_inline_check( sprintf( '%s->{$_}', $val ) ),
112             $val,
113             );
114             }
115              
116 9         242 return '( ' . ( join ' && ', @code ) . ' )';
117             }
118              
119             1;
120              
121             # ABSTRACT: Guts of Dict structured type
122              
123             __END__
124              
125             =pod
126              
127             =encoding UTF-8
128              
129             =head1 NAME
130              
131             Specio::Library::Structured::Dict - Guts of Dict structured type
132              
133             =head1 VERSION
134              
135             version 0.47
136              
137             =head1 DESCRIPTION
138              
139             There are no user facing parts here.
140              
141             =for Pod::Coverage .*
142              
143             =head1 SUPPORT
144              
145             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
146              
147             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
148              
149             =head1 SOURCE
150              
151             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
152              
153             =head1 AUTHOR
154              
155             Dave Rolsky <autarch@urth.org>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
160              
161             This is free software, licensed under:
162              
163             The Artistic License 2.0 (GPL Compatible)
164              
165             The full text of the license can be found in the
166             F<LICENSE> file included with this distribution.
167              
168             =cut