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 3     3   18 use strict;
  3         6  
  3         121  
4 3     3   15 use warnings;
  3         6  
  3         161  
5              
6             our $VERSION = '0.46';
7              
8 3     3   18 use Carp qw( confess );
  3         13  
  3         147  
9 3     3   16 use List::Util ();
  3         6  
  3         57  
10 3     3   23 use Scalar::Util qw( blessed );
  3         6  
  3         137  
11 3     3   27 use Specio::Helpers qw( perlstring );
  3         6  
  3         132  
12 3     3   19 use Specio::Library::Builtins;
  3         7  
  3         14  
13 3     3   18 use Specio::TypeChecks qw( does_role );
  3         5  
  3         1985  
14              
15             my $hashref = t('HashRef');
16              
17 3     3 0 26 sub parent {$hashref}
18              
19             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
20             sub _inline {
21 1     1   7 $hashref->inline_check( $_[1] );
22             }
23              
24             sub _parameterization_args_builder {
25 3     3   38 my $self = shift;
26 3         7 my $args = shift;
27              
28 3   66     39 for my $p ( ( $args->{slurpy} || () ), values %{ $args->{kv} } ) {
  3         11  
29 9         44 my $type;
30 9 100       26 if ( blessed($p) ) {
31 7         14 $type = $p;
32             }
33             else {
34 2 50 33     11 if ( ref $p eq 'HASH' && $p->{optional} ) {
35 2         5 $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 9 50       20 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 9 50       126 confess
48             'All parameters passed to ->parameterize must be inlinable constraints'
49             unless $type->can_be_inlined;
50             }
51              
52 3         19 return %{$args};
  3         14  
53             }
54              
55             sub _name_builder {
56 3     3   13 my $self = shift;
57 3         5 my $p = shift;
58              
59             ## no critic (Subroutines::ProtectPrivateSubs)
60 3         5 my @kv;
61 3         6 for my $k ( sort keys %{ $p->{kv} } ) {
  3         15  
62 8         73 my $v = $p->{kv}{$k};
63 8 100       24 if ( blessed($v) ) {
    50          
64 6         21 push @kv, "$k => " . $self->_name_or_anon($v);
65             }
66             elsif ( $v->{optional} ) {
67             push @kv,
68 2         9 "$k => " . $self->_name_or_anon( $v->{optional} ) . '?';
69             }
70             }
71              
72 3 100       24 if ( $p->{slurpy} ) {
73 1         5 push @kv, $self->_name_or_anon( $p->{slurpy} ) . '...';
74             }
75              
76 3         30 return 'Dict{ ' . ( join ', ', @kv ) . ' }';
77             }
78              
79             sub _structured_inline_generator {
80 9     9   19 my $self = shift;
81 9         16 my $val = shift;
82 9         28 my %args = @_;
83              
84 9         36 my @code = sprintf( '( %s )', $hashref->_inline_check($val) );
85              
86 9         23 for my $k ( sort keys %{ $args{kv} } ) {
  9         54  
87 24         55 my $p = $args{kv}{$k};
88 24         91 my $access = sprintf( '%s->{%s}', $val, perlstring($k) );
89              
90 24 100       88 if ( !blessed($p) ) {
91 6         15 my $type = $p->{optional};
92              
93 6         16 push @code,
94             sprintf(
95             '( exists %s ? ( %s ) : 1 )',
96             $access, $type->_inline_check($access)
97             );
98             }
99             else {
100 18         51 push @code, sprintf( '( %s )', $p->_inline_check($access) );
101             }
102             }
103              
104 9 100       34 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         34 ( join ', ', map { perlstring($_) } keys %{ $args{kv} } ),
  3         11  
111 3         7 $args{slurpy}->_inline_check( sprintf( '%s->{$_}', $val ) ),
112             $val,
113             );
114             }
115              
116 9         409 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.46
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 - 2020 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