| 1 | package Dist::Zilla::Plugin::Rinci::Validate; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2017-06-24'; # DATE | |||||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.24'; # VERSION | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dmp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Sah; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Perinci::Sub::Normalize qw(normalize_function_metadata); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | my $sah = Data::Sah->new(); | |||||||||||||||||||||||||||||||
| 15 | 1 | my $plc = $sah->get_compiler("perl"); | |||||||||||||||||||||||||||||||
| 16 | 1 | $plc->indent_character(''); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use experimental 'smartmatch'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 1 | with ( | |||||||||||||||||||||||||||||||
| 23 | 'Dist::Zilla::Role::FileMunger', | ||||||||||||||||||||||||||||||||
| 24 | 'Dist::Zilla::Role::FileFinderUser' => { | ||||||||||||||||||||||||||||||||
| 25 | default_finders => [':InstallModules'], | ||||||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub __squish_code { | ||||||||||||||||||||||||||||||
|
Subroutine
__squish_code: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 30 | 0 | my $code = shift; | |||||||||||||||||||||||||||||||
| 31 | 0 | for ($code) { | |||||||||||||||||||||||||||||||
| 32 | 0 | s/^\s*#.+//mg; # comment line | |||||||||||||||||||||||||||||||
| 33 | 0 | s/^\s+//mg; # indentation | |||||||||||||||||||||||||||||||
| 34 | 0 | s/\n+/ /g; # newline | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | 0 | $code; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub munge_files { | ||||||||||||||||||||||||||||||
|
Subroutine
munge_files: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($self->zilla->plugin_named('Rinci::Wrap')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | $self->log('Rinci::Wrap plugin detected, will skip running'); | |||||||||||||||||||||||||||||||
| 44 | 0 | return; | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | $self->munge_file($_) for @{ $self->found_files }; | |||||||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub munge_file { | ||||||||||||||||||||||||||||||
|
Subroutine
munge_file: not called CC=38 SCAR=72.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 52 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | my $fname = $file->name; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | unless ($file->isa("Dist::Zilla::File::OnDisk")) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | 0 | $self->log_debug(["skipping %s: not an ondisk file, currently only ondisk files are processed", $fname]); | |||||||||||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | $self->log_debug("Processing file $fname ..."); | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | unless ($fname =~ m!lib/(.+\.pm)$!) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | #$self->log_debug("Skipping: '$fname' not a module"); | ||||||||||||||||||||||||||||||||
| 65 | 0 | return; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | 0 | my $reqname = $1; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | # i do it this way (unshift @INC, "lib" + require "Foo/Bar.pm" instead of | ||||||||||||||||||||||||||||||||
| 70 | # unshift @INC, "." + require "lib/Foo/Bar.pm") in my all other Dist::Zilla | ||||||||||||||||||||||||||||||||
| 71 | # and Pod::Weaver plugin, so they can work together (require "Foo/Bar.pm" | ||||||||||||||||||||||||||||||||
| 72 | # and require "lib/Foo/Bar.pm" would cause Perl to load the same file twice | ||||||||||||||||||||||||||||||||
| 73 | # and generate redefine warnings). | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | local @INC = ("lib", @INC); | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | eval { require $reqname }; | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | $self->log_fatal("$fname: has compile errors: $@"); | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | my @content = split /^/, $file->content; | |||||||||||||||||||||||||||||||
| 83 | 0 | my $munged; | |||||||||||||||||||||||||||||||
| 84 | my $in_pod; | ||||||||||||||||||||||||||||||||
| 85 | 0 | my ($pkg_name, $sub_name, $metas, $meta, $arg, $var); | |||||||||||||||||||||||||||||||
| 86 | 0 | my $sub_has_vargs; # VALIDATE_ARGS has been declared for current sub | |||||||||||||||||||||||||||||||
| 87 | 0 | my %vargs; # list of validated args for current sub, val 2=skipped | |||||||||||||||||||||||||||||||
| 88 | 0 | my %vsubs; # list of subs | |||||||||||||||||||||||||||||||
| 89 | 0 | my %vars; # list of variables that the generated validator needs | |||||||||||||||||||||||||||||||
| 90 | 0 | my @modules; # list of modules (records) that the generated validator needs | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | my $i = 0; # line number | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $check_prev_sub = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return unless $sub_name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return unless $meta; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 0 | my %unvalidated; | |||||||||||||||||||||||||||||||
| 98 | 0 | for (keys %{ $meta->{args} }) { | |||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | next unless $meta->{args}{$_}{schema}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $unvalidated{$_}++ unless $vargs{$_}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (keys %unvalidated) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 103 | 0 | $self->log("NOTICE: $fname: Some argument(s) not validated ". | |||||||||||||||||||||||||||||||
| 104 | "for sub $sub_name: ". | ||||||||||||||||||||||||||||||||
| 105 | join(", ", sort keys %unvalidated)); | ||||||||||||||||||||||||||||||||
| 106 | } elsif (!$meta->{"x.perinci.sub.wrapper.disable_validate_args"}) { | ||||||||||||||||||||||||||||||||
| 107 | 0 | $self->log( | |||||||||||||||||||||||||||||||
| 108 | "NOTICE: $fname: You might want to set ". | ||||||||||||||||||||||||||||||||
| 109 | "x.perinci.sub.wrapper.disable_validate_args => 1 in metadata ". | ||||||||||||||||||||||||||||||||
| 110 | "for sub $sub_name"); | ||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | 0 | }; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $gen_err = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 115 | 0 | my ($status, $msg, $cond) = @_; | |||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($meta->{result_naked}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 0 | return qq[if ($cond) { die "$sub_name(): " . $msg } ]; | |||||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||||||
| 119 | 0 | return qq|if ($cond) { return [$status, $msg] } |; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 0 | }; | |||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $gen_merr = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 123 | 0 | my ($cond, $arg) = @_; | |||||||||||||||||||||||||||||||
| 124 | 0 | $gen_err->(400, qq["Missing argument: $arg"], $cond); | |||||||||||||||||||||||||||||||
| 125 | 0 | }; | |||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $gen_verr = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 127 | 0 | my ($cond, $arg) = @_; | |||||||||||||||||||||||||||||||
| 128 | 0 | $gen_err->(400, qq["Invalid argument value for $arg: \$arg_err"], | |||||||||||||||||||||||||||||||
| 129 | $cond); | ||||||||||||||||||||||||||||||||
| 130 | 0 | }; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $gen_arg = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 133 | 0 | my $meta = $metas->{$sub_name}; | |||||||||||||||||||||||||||||||
| 134 | 0 | my $dn = $arg; $dn =~ s/\W+/_/g; | |||||||||||||||||||||||||||||||
| 135 | my $cd = $plc->compile( | ||||||||||||||||||||||||||||||||
| 136 | schema => $meta->{args}{$arg}{schema}, | ||||||||||||||||||||||||||||||||
| 137 | 0 | err_term => '$arg_err', | |||||||||||||||||||||||||||||||
| 138 | data_name => $dn, | ||||||||||||||||||||||||||||||||
| 139 | data_term => $var, | ||||||||||||||||||||||||||||||||
| 140 | return_type => 'str', | ||||||||||||||||||||||||||||||||
| 141 | comment => 0, | ||||||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 0 | my @code; | |||||||||||||||||||||||||||||||
| 145 | 0 | for my $mod_rec (@{$cd->{modules}}) { | |||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | next unless $mod_rec->{phase} eq 'runtime'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | push @code, $plc->stmt_require_module($mod_rec) unless | ||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | grep { $_->{name} eq $mod_rec->{name} && !$mod_rec->{use_statement} } @modules; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_->{'name'} eq $mod_rec->{'name'} && !$mod_rec->{'use_statement'}
MC/DC0%0/2
$_->{'name'} eq $mod_rec->{'name'} && !$mod_rec->{'use_statement'}
$_->{'name'} eq $mod_rec->{'name'}
!$mod_rec->{'use_statement'}
Truth table0%0/3
A && not(B)
A= $_->{'name'} eq $mod_rec->{'name'}B= !$mod_rec->{'use_statement'}
| |||||||||||||||||||||||||||||||||
| 149 | 0 | push @modules, $mod_rec; | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | 0 | for (sort keys %{$cd->{vars}}) { | |||||||||||||||||||||||||||||||
| 152 | push @code, "my \$$_ = ".$plc->literal($cd->{vars}{$_})."; " | ||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | unless exists($vars{$_}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | 0 | $vars{$_}++; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | push @code, 'my $arg_err; ' unless keys %vargs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | 0 | push @code, __squish_code($cd->{result}), "; "; | |||||||||||||||||||||||||||||||
| 158 | 0 | push @code, $gen_verr->('$arg_err', $arg); | |||||||||||||||||||||||||||||||
| 159 | 0 | $vargs{$arg} = 1; | |||||||||||||||||||||||||||||||
| 160 | 0 | join "", @code; | |||||||||||||||||||||||||||||||
| 161 | 0 | }; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my $gen_args = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||
| 164 | 0 | my @code; | |||||||||||||||||||||||||||||||
| 165 | 0 | for my $arg (sort keys %{ $meta->{args} }) { | |||||||||||||||||||||||||||||||
| 166 | 0 | my $as = $meta->{args}{$arg}; | |||||||||||||||||||||||||||||||
| 167 | 0 | my $has_default_prop = exists($as->{default}); | |||||||||||||||||||||||||||||||
| 168 | 0 | my $sn = $meta->{args}{$arg}{schema}; # already normalized by normalize_function_metadata() | |||||||||||||||||||||||||||||||
| 169 | 0 | my $kvar; # var to access a hash key | |||||||||||||||||||||||||||||||
| 170 | 0 | $kvar = $var; $kvar =~ s/.//; | |||||||||||||||||||||||||||||||
| 171 | $kvar = join( | ||||||||||||||||||||||||||||||||
| 172 | "", | ||||||||||||||||||||||||||||||||
| 173 | "\$$kvar", | ||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | (($meta->{args_as} // "hash") eq "hashref" ? "->" : ""), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$meta->{"args_as"} // "hash"
MC/DC0%0/1
$meta->{"args_as"} // "hash"
$meta->{"args_as"}
Truth table0%0/2
A
A= $meta->{"args_as"}
| |||||||||||||||||||||||||||||||||
| 175 | "{'$arg'}", | ||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($sn) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 178 | 0 | my $has_sch_default = exists($sn->[1]{default}); | |||||||||||||||||||||||||||||||
| 179 | 0 | my $dn = $arg; $dn =~ s/\W+/_/g; | |||||||||||||||||||||||||||||||
| 180 | 0 | my $cd = $plc->compile( | |||||||||||||||||||||||||||||||
| 181 | schema => $sn, | ||||||||||||||||||||||||||||||||
| 182 | schema_is_normalized => 1, | ||||||||||||||||||||||||||||||||
| 183 | err_term => '$arg_err', | ||||||||||||||||||||||||||||||||
| 184 | data_name => $dn, | ||||||||||||||||||||||||||||||||
| 185 | data_term => $kvar, | ||||||||||||||||||||||||||||||||
| 186 | return_type => 'str', | ||||||||||||||||||||||||||||||||
| 187 | comment => 0, | ||||||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | 0 | for my $mod_rec (@{$cd->{modules}}) { | |||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | next unless $mod_rec->{phase} eq 'runtime'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | push @code, $plc->stmt_require_module($mod_rec) unless | ||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | grep { $_->{name} eq $mod_rec->{name} && !$mod_rec->{use_statement} } @modules; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_->{'name'} eq $mod_rec->{'name'} && !$mod_rec->{'use_statement'}
MC/DC0%0/2
$_->{'name'} eq $mod_rec->{'name'} && !$mod_rec->{'use_statement'}
$_->{'name'} eq $mod_rec->{'name'}
!$mod_rec->{'use_statement'}
Truth table0%0/3
A && not(B)
A= $_->{'name'} eq $mod_rec->{'name'}B= !$mod_rec->{'use_statement'}
| |||||||||||||||||||||||||||||||||
| 194 | 0 | push @modules, $mod_rec; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | 0 | for (sort keys %{$cd->{vars}}) { | |||||||||||||||||||||||||||||||
| 197 | push @code, "my \$$_ = ".$plc->literal($cd->{vars}{$_})."; " | ||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | unless exists($vars{$_}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 0 | $vars{$_}++; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | push @code, 'my $arg_err; ' unless keys %vargs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 0 | $vargs{$arg} = 1; | |||||||||||||||||||||||||||||||
| 203 | 0 | push @code, "if (exists($kvar)) { "; | |||||||||||||||||||||||||||||||
| 204 | 0 | push @code, __squish_code($cd->{result}), "; "; | |||||||||||||||||||||||||||||||
| 205 | 0 | push @code, $gen_verr->('$arg_err', $arg); | |||||||||||||||||||||||||||||||
| 206 | 0 | push @code, "}"; | |||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | if ($has_sch_default) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 208 | 0 | push @code, " else { "; | |||||||||||||||||||||||||||||||
| 209 | 0 | push @code, "$kvar = ", dmp($sn->[1]{default}), ";"; | |||||||||||||||||||||||||||||||
| 210 | 0 | push @code, "}"; | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | } elsif ($has_default_prop) { | ||||||||||||||||||||||||||||||||
| 213 | # no schema is defined, but there is 'default' property | ||||||||||||||||||||||||||||||||
| 214 | 0 | push @code, "$kvar //= ", dmp($as->{default}), ";"; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($as->{req}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 218 | 0 | push @code, $gen_merr->("!exists($kvar)", $arg); | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | 0 | join "", @code; | |||||||||||||||||||||||||||||||
| 222 | 0 | }; | |||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 0 | for (@content) { | |||||||||||||||||||||||||||||||
| 225 | 0 | $i++; | |||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if (/^=cut\b/x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 227 | 0 | $in_pod = 0; | |||||||||||||||||||||||||||||||
| 228 | 0 | next; | |||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | next if $in_pod; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | if (/^=\w+/x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | 0 | $in_pod++; | |||||||||||||||||||||||||||||||
| 233 | 0 | next; | |||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | if (/^\s*package \s+ (\w+(?:::\w+)*) \s*;/x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 237 | 0 | $pkg_name = $1; | |||||||||||||||||||||||||||||||
| 238 | 0 | $self->log_debug("Found package declaration $pkg_name"); | |||||||||||||||||||||||||||||||
| 239 | 0 | $metas = \%{"$pkg_name\::SPEC"}; | |||||||||||||||||||||||||||||||
| 240 | 0 | for (keys %$metas) { | |||||||||||||||||||||||||||||||
| 241 | 0 | $metas->{$_} = normalize_function_metadata($metas->{$_}); | |||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||
| 243 | 0 | next; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | if (/^\s*sub \s+ (\w+)/x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 0 | $self->log_debug("Found sub declaration $1"); | |||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | unless ($pkg_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 248 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 249 | "$fname:$i: module does not have package definition"); | ||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | 0 | $check_prev_sub->(); | |||||||||||||||||||||||||||||||
| 252 | 0 | $sub_name = $1; | |||||||||||||||||||||||||||||||
| 253 | 0 | $sub_has_vargs = 0; | |||||||||||||||||||||||||||||||
| 254 | 0 | %vargs = (); | |||||||||||||||||||||||||||||||
| 255 | 0 | @modules = (); | |||||||||||||||||||||||||||||||
| 256 | 0 | %vars = (); | |||||||||||||||||||||||||||||||
| 257 | 0 | $meta = $metas->{$sub_name}; | |||||||||||||||||||||||||||||||
| 258 | 0 | next; | |||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if (/^\s*? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | (?<code>\s* my \s+ (?<sigil>[\$@%]) (?<var>\w+) \b .+)? | ||||||||||||||||||||||||||||||||
| 262 | (?<tag>\#\s*(?<no>NO_)?VALIDATE_ARG(?<s> S)? | ||||||||||||||||||||||||||||||||
| 263 | (?: \s+ (?<var2>\w+))? \s*$)/x) { | ||||||||||||||||||||||||||||||||
| 264 | 0 | my %m = %+; | |||||||||||||||||||||||||||||||
| 265 | $self->log_debug("Found line with tag $_, m=" . | ||||||||||||||||||||||||||||||||
| 266 | 0 | join(', ', map {"$_=>$m{$_}"} keys %m)); | |||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | next if !$m{no} && !$m{code}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$m{"no"} or $m{"code"}
MC/DC0%0/2
$m{"no"} or $m{"code"}
$m{"no"}
$m{"code"}
Truth table0%0/3
A or B
A= $m{"no"}B= $m{"code"}
| |||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | $arg = $m{var2} // $m{var}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$m{"var2"} // $m{"var"}
MC/DC0%0/2
$m{"var2"} // $m{"var"}
$m{"var2"}
$m{"var"}
Truth table0%0/3
A // B
A= $m{"var2"}B= $m{"var"}
| |||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | if ($m{no}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ($m{s}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | %vargs = map {$_=>2} keys %{$meta->{args} // {}}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$meta->{"args"} // {}
MC/DC0%0/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table0%0/2
A
A= $meta->{"args"}
| |||||||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||||||
| 273 | 0 | $vargs{$arg} = 2; | |||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||
| 275 | 0 | next; | |||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | 0 | $var = $m{sigil} . $m{var}; | |||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | unless ($sub_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 279 | 0 | $self->log_fatal("$fname:$i: # VALIDATE_ARG$m{s} outside sub"); | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | unless ($meta) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 282 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 283 | "$fname:$i: sub $sub_name does not have metadata"); | ||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if (($meta->{v} // 1.0) != 1.1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$meta->{"v"} // 1
MC/DC0%0/1
$meta->{"v"} // 1
$meta->{"v"}
Truth table0%0/2
A
A= $meta->{"v"}
| |||||||||||||||||||||||||||||||||
| 286 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 287 | "$fname:$i: metadata for sub $sub_name is not v1.1 ". | ||||||||||||||||||||||||||||||||
| 288 | "(currently only v1.1 is supported)"); | ||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | if ($m{s} && ($meta->{args_as} // "hash") !~ /^hash(ref)?$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$m{"s"} and not +($meta->{"args_as"} // "hash") =~ /^hash(ref)?$/
$meta->{"args_as"} // "hash"
MC/DC0%0/2
$m{"s"} and not +($meta->{"args_as"} // "hash") =~ /^hash(ref)?$/
$m{"s"}
$meta->{"args_as"}
Truth table0%0/3
A and not(B)
A= $m{"s"}B= $meta->{"args_as"}
| |||||||||||||||||||||||||||||||||
| 291 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 292 | "$fname:$i: metadata for sub $sub_name: ". | ||||||||||||||||||||||||||||||||
| 293 | "args_as=$meta->{args_as} (sorry, currently only ". | ||||||||||||||||||||||||||||||||
| 294 | "args_as=hash/hashref supported for validating all args at once (# VALIDATE_ARGS), try validating one arg at a time (# VALIDATE_ARG))"); | ||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | unless ($meta->{args}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 298 | "$fname:$i: # metadata for sub $sub_name: ". | ||||||||||||||||||||||||||||||||
| 299 | "no args property defined"); | ||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | if ($m{s} && $sub_has_vargs) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$m{"s"} and $sub_has_vargs
MC/DC0%0/2
$m{"s"} and $sub_has_vargs
$m{"s"}
$sub_has_vargs
Truth table0%0/3
A and B
A= $m{"s"}B= $sub_has_vargs
| |||||||||||||||||||||||||||||||||
| 302 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 303 | "$fname:$i: multiple # VALIDATE_ARGS for sub $sub_name"); | ||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | if (!$m{s}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | unless ($meta->{args}{$arg} && $meta->{args}{$arg}{schema}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$meta->{"args"}{$arg} and $meta->{"args"}{$arg}{"schema"}
MC/DC0%0/2
$meta->{"args"}{$arg} and $meta->{"args"}{$arg}{"schema"}
$meta->{"args"}{$arg}
$meta->{"args"}{$arg}{"schema"}
Truth table0%0/3
A and B
A= $meta->{"args"}{$arg}B= $meta->{"args"}{$arg}{"schema"}
| |||||||||||||||||||||||||||||||||
| 307 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 308 | "$fname:$i: metadata for sub $sub_name: ". | ||||||||||||||||||||||||||||||||
| 309 | "no schema for argument $arg"); | ||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if ($m{s} && $m{sigil} !~ /[\$%]/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$m{"s"} and not $m{"sigil"} =~ /[\$%]/
MC/DC0%0/2
$m{"s"} and not $m{"sigil"} =~ /[\$%]/
$m{"s"}
not $m{"sigil"} =~ /[\$%]/
Truth table0%0/3
A and not(B)
A= $m{"s"}B= not $m{"sigil"} =~ /[\$%]/
| |||||||||||||||||||||||||||||||||
| 313 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 314 | "$fname:$i: invalid variable $var ". | ||||||||||||||||||||||||||||||||
| 315 | "for # VALIDATE_ARGS, must be hash/hashref"); | ||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||
| 317 | 0 | ▶ | if (!$m{s} && $m{sigil} ne '$') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $m{"s"} and $m{"sigil"} ne "\$"
MC/DC0%0/2
not $m{"s"} and $m{"sigil"} ne "\$"
not $m{"s"}
$m{"sigil"} ne "\$"
Truth table0%0/3
not(A) and B
A= not $m{"s"}B= $m{"sigil"} ne "\$"
| |||||||||||||||||||||||||||||||||
| 318 | 0 | $self->log_fatal( | |||||||||||||||||||||||||||||||
| 319 | "$fname:$i: invalid variable $var ". | ||||||||||||||||||||||||||||||||
| 320 | "for # VALIDATE_ARG, must be scalar"); | ||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 0 | $munged++; | |||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if ($m{s}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | 0 | $_ = $m{code} . $gen_args->() . "" . $m{tag}; | |||||||||||||||||||||||||||||||
| 326 | } else { | ||||||||||||||||||||||||||||||||
| 327 | 0 | $_ = $m{code} . $gen_arg->() . "" . $m{tag}; | |||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||
| 331 | 0 | $check_prev_sub->(); | |||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | if ($munged) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 334 | 0 | $self->log("Adding argument validation code for $fname"); | |||||||||||||||||||||||||||||||
| 335 | 0 | $file->content(join "", @content); | |||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | 0 | return; | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 342 | 1 | 1; | |||||||||||||||||||||||||||||||
| 343 | # ABSTRACT: Insert argument validator code in output code | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | __END__ | ||||||||||||||||||||||||||||||||