| 1 | package Perinci::Sub::ValidateArgs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-03-10'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'Perinci-Sub-ValidateArgs'; # DIST | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.013'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Data::Dmp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | gen_args_validator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | gen_args_validator_from_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | validate_args_using_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # old name, deprecated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | *gen_args_validator = \&gen_args_validator_from_meta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # XXX cache key should also contain data_term | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #my %dsah_compile_cache; # key = schema (C<string> or R<refaddr>), value = compilation result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | our %SPEC; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | our %arg_meta = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | meta => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | schema => 'hash*', # XXX rinci::function_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | our %argopt_meta = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | meta => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | schema => 'hash*', # XXX rinci::function_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | If not specified, will be searched from caller's `%SPEC` package variable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | our %argopt_die = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | die => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | summary => 'Whether validator should die or just return '. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 'an error message/response', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | schema => 'bool', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | our %args_gen_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | %argopt_meta, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | %argopt_die, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | source => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | summary => 'Whether we want to get the source code instead', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | schema => 'bool', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | The default is to generate Perl validator code, compile it with `eval()`, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | return the resulting coderef. When this option is set to true, the generated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | source string will be returned instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $SPEC{gen_args_validator_from_meta} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | summary => 'Generate argument validator from Rinci function metadata', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | If you don't intend to reuse the generated validator, you can also use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | `validate_args_using_meta`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | %args_gen_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 20 | ▶ | sub gen_args_validator_from_meta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gen_args_validator_from_meta: called CC=39 SCAR=37.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 20 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | my $meta = $args{meta}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 20 | ▶ | unless ($meta) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | ▶ | my @caller = caller(1) or die "Call gen_args_validator_from_meta() inside ". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | "your function or provide 'meta'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | my ($pkg, $func) = $caller[3] =~ /(.+)::(.+)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | ▶ | $meta = ${"$pkg\::SPEC"}{$func} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | or die "No metadata for $caller[3]"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 20 | ▶ | my $args_as = $meta->{args_as} // 'hash'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$meta->{"args_as"} // "hash"
MC/DC100%1/1
$meta->{"args_as"} // "hash"
$meta->{"args_as"}
Truth table100%2/2
A
A= $meta->{"args_as"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 20 | ▶ | my $meta_args = $meta->{args} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$meta->{"args"} // {}
MC/DC100%1/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table100%2/2
A
A= $meta->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 20 | my @meta_args = sort keys %$meta_args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 20 | my @code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | my @modules_for_all_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 20 | my @mod_stmts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 20 | my $use_dpath; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 72 | ▶ | my $gencode_err = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 72 | my ($status, $term_msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 72 | ▶ | if ($args{die}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 16 | return "die $term_msg;"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif ($meta->{result_naked}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # perhaps if result_naked=1, die by default? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | return "return $term_msg;"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 52 | return "return [$status, $term_msg];"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 20 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 38 | ▶ | my $addcode_validator = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 38 | state $plc = do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 19 | require Data::Sah; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 19 | Data::Sah->new->get_compiler("perl"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 38 | my ($schema, $data_name, $data_term) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 38 | my $cd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 38 | ▶ | my $cache_key = ref($schema) ? "R$schema" : "S$schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | #unless ($cd = $dsah_compile_cache{$cache_key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 38 | $cd = $plc->compile( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | data_name => $data_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | data_term => $data_term, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | err_term => '$err', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | return_type => 'str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | indent_level => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 38 | ▶ | die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # $dsah_compile_cache{$cache_key} = $cd; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | #} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 38 | push @code, " \$err = undef;\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 38 | ▶ | push @code, " \$_sahv_dpath = [];\n" if $cd->{use_dpath}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 38 | push @code, " unless (\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 38 | push @code, $cd->{result}, ") { ".$gencode_err->(400, "\"Validation failed for argument '$data_name': \$err\"")." }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 38 | for my $mod_rec (@{ $cd->{modules} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 150 | ▶ | next unless $mod_rec->{phase} eq 'runtime'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 94 | ▶ | next if grep { ($mod_rec->{use_statement} && $_->{use_statement} && $_->{use_statement} eq $mod_rec->{use_statement}) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 149 | ▶ | $_->{name} eq $mod_rec->{name} } @modules_for_all_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition88%8/9
$mod_rec->{'use_statement'} && $_->{'use_statement'} && $_->{'use_statement'} eq $mod_rec->{'use_statement'} || $_->{'name'} eq $mod_rec->{'name'}
$mod_rec->{"use_statement"} and $_->{"use_statement"} and $_->{"use_statement"} eq $mod_rec->{"use_statement"}
$mod_rec->{"use_statement"} and $_->{"use_statement"}
MC/DC75%3/4
$mod_rec->{'use_statement'} && $_->{'use_statement'} && $_->{'use_statement'} eq $mod_rec->{'use_statement'} || $_->{'name'} eq $mod_rec->{'name'}
$mod_rec->{"use_statement"}
$_->{"use_statement"}
$_->{"use_statement"} eq $mod_rec->{"use_statement"}
$_->{'name'} eq $mod_rec->{'name'}
Truth table57%4/7
A and B and C || D
A= $mod_rec->{"use_statement"}B= $_->{"use_statement"}C= $_->{"use_statement"} eq $mod_rec->{"use_statement"}D= $_->{'name'} eq $mod_rec->{'name'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 56 | push @modules_for_all_args, $mod_rec; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 56 | push @mod_stmts, $plc->stmt_require_module($mod_rec)."\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 38 | ▶ | if ($cd->{use_dpath}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 18 | $use_dpath = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 20 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 20 | ▶ | if ($args_as eq 'hash' || $args_as eq 'hashref') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$args_as eq "hash" or $args_as eq "hashref"
MC/DC100%2/2
$args_as eq "hash" or $args_as eq "hashref"
$args_as eq "hash"
$args_as eq "hashref"
Truth table100%3/3
A or B
A= $args_as eq "hash"B= $args_as eq "hashref"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 15 | push @code, " # check unknown args\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 15 | push @code, " for (keys %\$args) { unless (/\\A(".join("|", map { quotemeta } @meta_args).")\\z/) { ".$gencode_err->(400, '"Unknown argument \'$_\'"')." } }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 15 | push @code, "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 15 | for my $arg_name (@meta_args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 28 | my $arg_spec = $meta_args->{$arg_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 28 | my $term_arg = "\$args->{'$arg_name'}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 28 | push @code, " # check argument $arg_name\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 28 | ▶ | if (defined $arg_spec->{default}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | push @code, " $term_arg //= ".dmp($arg_spec->{default}).";\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 28 | push @code, " if (exists $term_arg) {\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 28 | ▶ | $addcode_validator->($arg_spec->{schema}, $arg_name, $term_arg) if $arg_spec->{schema}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 28 | ▶ | if ($arg_spec->{req}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 14 | push @code, " } else {\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 14 | push @code, " ".$gencode_err->(400, "\"Missing required argument '$arg_name'\"")."\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 28 | push @code, " }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 15 | ▶ | push @code, "\n" if @meta_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | ▶ | } elsif ($args_as eq 'array' || $args_as eq 'arrayref') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args_as eq "array" or $args_as eq "arrayref"
MC/DC0%0/2
$args_as eq "array" or $args_as eq "arrayref"
$args_as eq "array"
$args_as eq "arrayref"
Truth table66%2/3
A or B
A= $args_as eq "array"B= $args_as eq "arrayref"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # map the arguments' position | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | my @arg_names = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | ▶ | ($meta_args->{$a}{pos}//9999) <=> ($meta_args->{$b}{pos}//9999) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$meta_args->{$a}{"pos"} // 9999
$meta_args->{$b}{"pos"} // 9999
MC/DC0%0/1
$meta_args->{$a}{"pos"} // 9999
$meta_args->{$a}{"pos"}
MC/DC0%0/1
$meta_args->{$b}{"pos"} // 9999
$meta_args->{$b}{"pos"}
Truth table50%1/2
A
A= $meta_args->{$a}{"pos"}
Truth table50%1/2
A
A= $meta_args->{$b}{"pos"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } keys %$meta_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | ▶ | if (@arg_names && ($meta_args->{$arg_names[-1]}{slurpy} // $meta_args->{$arg_names[-1]}{greedy})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@arg_names and $$meta_args{$arg_names[-1]}{"slurpy"} // $$meta_args{$arg_names[-1]}{"greedy"}
$$meta_args{$arg_names[-1]}{"slurpy"} // $$meta_args{$arg_names[-1]}{"greedy"}
MC/DC33%1/3
@arg_names and $$meta_args{$arg_names[-1]}{"slurpy"} // $$meta_args{$arg_names[-1]}{"greedy"}
@arg_names
$$meta_args{$arg_names[-1]}{"slurpy"}
$$meta_args{$arg_names[-1]}{"greedy"}
Truth table50%2/4
A and B // C
A= @arg_namesB= $$meta_args{$arg_names[-1]}{"slurpy"}C= $$meta_args{$arg_names[-1]}{"greedy"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | my $pos = @arg_names - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | push @code, " # handle slurpy last arg\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | push @code, " if (\@\$args >= $pos) { \$args->[$pos] = [splice \@\$args, $pos] }\n\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 5 | my $start_of_optional; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | for my $i (0..$#arg_names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 10 | my $arg_name = $arg_names[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 10 | my $arg_spec = $meta_args->{$arg_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 10 | ▶ | if ($arg_spec->{req}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 5 | ▶ | if (defined $start_of_optional) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | die "Error in metadata: after a param is optional ". | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | "(#$start_of_optional) the rest (#$i) must also be optional"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | ▶ | $start_of_optional //= $i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$start_of_optional //= $i
MC/DC0%0/2
$start_of_optional //= $i
$start_of_optional
$i
Truth table33%1/3
A //= B
A= $start_of_optionalB= $i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | push @code, " # check number of args\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 5 | ▶ | if ($start_of_optional) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 5 | push @code, " if (\@\$args < $start_of_optional || \@\$args > ".(@arg_names).") { ".$gencode_err->(400, "\"Wrong number of arguments (expected $start_of_optional..".(@arg_names).", got \".(\@\$args).\")\"") . " }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } elsif (defined $start_of_optional) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | push @code, " if (\@\$args > ".(@arg_names).") { ".$gencode_err->(400, "\"Wrong number of arguments (expected 0..".(@arg_names).", got \".(\@\$args).\")\"") . " }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | push @code, " if (\@\$args != ".(@arg_names).") { ".$gencode_err->(400, "\"Wrong number of arguments (expected ".(@arg_names).", got \".(\@\$args).\")\"") . " }\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | push @code, "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | for my $i (0..$#arg_names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 10 | my $arg_name = $arg_names[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 10 | my $arg_spec = $meta_args->{$arg_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 10 | my $term_arg = "\$args->[$i]"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 10 | ▶ | if (!defined($arg_spec->{pos})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | die "Error in metadata: argument '$arg_name' does not ". | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | "have pos property set"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } elsif ($arg_spec->{pos} != $i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | die "Error in metadata: argument '$arg_name' does not ". | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | "the correct pos value ($arg_spec->{pos}, should be $i)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | ▶ | } elsif (($arg_spec->{slurpy} // $arg_spec->{greedy}) && $i < $#arg_names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and $i < $#arg_names
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"}
MC/DC0%0/3
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and $i < $#arg_names
$arg_spec->{"slurpy"}
$arg_spec->{"greedy"}
$i < $#arg_names
Truth table40%2/5
A // B and C
A= $arg_spec->{"slurpy"}B= $arg_spec->{"greedy"}C= $i < $#arg_names
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | die "Error in metadata: argument '$arg_name' has slurpy=1 ". | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | "but is not the last argument"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 10 | push @code, " # check argument $arg_name\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 10 | ▶ | if (defined $arg_spec->{default}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | push @code, " $term_arg //= ".dmp($arg_spec->{default}).";\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 10 | my $open_block; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 10 | ▶ | if (defined($start_of_optional) && $i >= $start_of_optional) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $start_of_optional and $i >= $start_of_optional
MC/DC50%1/2
defined $start_of_optional and $i >= $start_of_optional
defined $start_of_optional
$i >= $start_of_optional
Truth table66%2/3
A and B
A= defined $start_of_optionalB= $i >= $start_of_optional
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 5 | $open_block++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | push @code, " if (\@\$args > $i) {\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 10 | ▶ | $addcode_validator->($arg_spec->{schema}, $arg_name, $term_arg) if $arg_spec->{schema}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 10 | ▶ | push @code, " }\n" if $open_block; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 10 | push @code, "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | die "Unsupported args_as '$args_as'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 20 | push @code, " return undef;\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 20 | push @code, "}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 20 | unshift @code, ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | "sub {\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | " my \$args = shift;\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | " my \$err;\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | (" my \$_sahv_dpath;\n") x !!$use_dpath, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | "\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 20 | my $code = join("", @mod_stmts, @code); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 20 | ▶ | if ($args{source}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | return $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | #use String::LineNumber 'linenum'; say linenum $code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 19 | ▶ | my $sub = eval $code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 19 | ▶ | die if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 19 | return $sub; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | $SPEC{validate_args_using_meta} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | summary => 'Validate arguments using Rinci function metadata', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | If you intend to reuse the generated validator, you can also use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | `gen_args_validator_from_meta`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | Note: currently cannot handle `args_as => 'array'`, only `args_as => 'arrayref`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | %arg_meta, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | %argopt_die, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | schema => ['any*', of=>['hash*', 'array*']], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 12 | ▶ | sub validate_args_using_meta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_args_using_meta: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 12 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | my $validator = gen_args_validator_from_meta( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | meta => $args{meta}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | die => $args{die}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 12 | ▶ | $validator->($args{args}) // [200, "OK"]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
&$validator($args{"args"}) // [200, "OK"]
MC/DC100%1/1
&$validator($args{"args"}) // [200, "OK"]
&$validator($args{"args"})
Truth table100%2/2
A
A= &$validator($args{"args"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # ABSTRACT: Validate function arguments using schemas in Rinci function metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||