DefaultValueJS.pm

filter: Statementstmt 56.1% 32 / 57 Branchbran 0.0% 0 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 80.0% 8 / 10 Podpod 100% 1 / 1 totaltotal 51.8% 42 / 81 CC 6 SCAR 24.3
CC
6
Cov
48%
gen_default_value_code
30.0
__ANON__
6.0
BEGIN
1.0
CRAP 11.3
↑ Summary DefaultValue.pm »
1 package Data::Sah::DefaultValueJS;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Data::Sah::DefaultValueCommon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IPC::System::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Nodejs::Util qw(get_nodejs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 2 our $DATE = '2024-01-17'; # DATE
16 2 our $DIST = 'Data-Sah-DefaultValue'; # DIST
17 2 our $VERSION = '0.005'; # VERSION
18  
19 2 our @EXPORT_OK = qw(gen_default_value_code);
20  
21 2 our %SPEC;
22  
23 2 ▶our $Log_Default_Value_Code = $ENV{LOG_SAH_DEFAULT_VALUE_CODE} // 0;
Condition50%1/2
$ENV{"LOG_SAH_DEFAULT_VALUE_CODE"} // 0
l!l
02
MC/DC0%0/1
$ENV{"LOG_SAH_DEFAULT_VALUE_CODE"} // 0
$ENV{"LOG_SAH_DEFAULT_VALUE_CODE"}
Truth table50%1/2
A
A= $ENV{"LOG_SAH_DEFAULT_VALUE_CODE"}
Aresult
11
00
24  
25 $SPEC{gen_default_value_code} = {
26 2 v => 1.1,
27 summary => 'Generate code to set default value',
28 description => <<'_',
29  
30 This is mostly for testing. Normally the coercion rules will be used from
31 <pm:Data::Sah>.
32  
33 _
34 args => {
35 %Data::Sah::DefaultValueCommon::gen_default_value_code_args,
36 },
37 result_naked => 1,
38 };
39 0 ▶sub gen_default_value_code {
Subroutine gen_default_value_code: not called CC=5 SCAR=34.0
Pod documented
40 0 my %args = @_;
41  
42 0 my $rules = Data::Sah::DefaultValueCommon::get_default_value_rules(
43 %args,
44 compiler=>'js',
45 );
46  
47 0 my $code;
48 0 ▶ if (@$rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rules) { } 0 0 0
49 0 my $expr = '';
50 $code = join(
51 "",
52 "function (res) {\n",
53 (map {
54 0 " if (res === undefined || res === null) { res = $rules->[$_]{expr_value} }\n"
55 0 } (reverse 0..$#{$rules})),
56 " return res\n",
57 "}",
58 );
59 } else {
60 0 $code = 'function (res) { return res }';
61 }
62  
63 0 ▶ if ($Log_Default_Value_Code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Sah::DefaultValueJS::Log_Default_Value_Code) 0 0 0
64 0 log_trace("Default-value code (gen args: %s): %s", \%args, $code);
65 }
66  
67 0 ▶ return $code if $args{source};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"source"} 0 0 0
68  
69 0 state $nodejs_path = get_nodejs_path();
70 0 ▶ die "Can't find node.js in PATH" unless $nodejs_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodejs_path 0 0 0
71  
72 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
73 0 require File::Temp;
74 0 require JSON::MaybeXS;
75 #require String::ShellQuote;
76  
77 0 my $data = shift;
78  
79 0 state $json = JSON::MaybeXS->new->allow_nonref;
80  
81 # code to be sent to nodejs
82 0 my $src = "var default_value_code = $code;\n\n".
83 "console.log(JSON.stringify(default_value_code(".
84 $json->encode($data).")))";
85  
86 0 my ($jsh, $jsfn) = File::Temp::tempfile();
87 0 print $jsh $src;
88 0 ▶ close($jsh) or die "Can't write JS code to file $jsfn: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $jsh or die "Can't write JS code to file ${jsfn}: $!" 0 0 0
89  
90 0 my $out = IPC::System::Options::readpipe($nodejs_path, $jsfn);
91 0 $json->decode($out);
92 0 };
93 }
94  
95 2 1;
96 # ABSTRACT: Generate code to set default value
97  
98 __END__
↑ Summary DefaultValue.pm »