CoerceJS.pm

filter: Statementstmt 72.0% 62 / 86 Branchbran 47.6% 20 / 42 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 65.3% 96 / 147 CC 25 SCAR 39.0
CC
25
Cov
64%
gen_coercer
69.1
__ANON__
12.0
BEGIN
1.0
CRAP 49.5
↑ Summary CoerceCommon.pm »
1 package Data::Sah::CoerceJS;
2  
3 6 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Data::Sah::CoerceCommon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 6 our $DATE = '2023-10-24'; # DATE
13 6 our $DIST = 'Data-Sah-Coerce'; # DIST
14 6 our $VERSION = '0.054'; # VERSION
15  
16 6 our @EXPORT_OK = qw(gen_coercer);
17  
18 6 our %SPEC;
19  
20 6 ▶our $Log_Coercer_Code = $ENV{LOG_SAH_COERCER_CODE} // 0;
Condition50%1/2
$ENV{"LOG_SAH_COERCER_CODE"} // 0
l!l
06
MC/DC0%0/1
$ENV{"LOG_SAH_COERCER_CODE"} // 0
$ENV{"LOG_SAH_COERCER_CODE"}
Truth table50%1/2
A
A= $ENV{"LOG_SAH_COERCER_CODE"}
Aresult
11
00
21  
22 $SPEC{gen_coercer} = {
23 6 v => 1.1,
24 summary => 'Generate coercer code',
25 description => <<'_',
26  
27 This is mostly for testing. Normally the coercion rules will be used from
28 <pm:Data::Sah>.
29  
30 _
31 args => {
32 %Data::Sah::CoerceCommon::gen_coercer_args,
33 engine => {
34 schema => ['str*', in=>['quickjs', 'nodejs']],
35 default => 'quickjs',
36 },
37 },
38 result_naked => 1,
39 };
40 4 ▶sub gen_coercer {
Subroutine gen_coercer: called CC=23 SCAR=42.4
Pod documented
41 4 my %args = @_;
42  
43 4 ▶ my $rt = $args{return_type} // 'val';
Condition100%2/2
$args{"return_type"} // "val"
l!l
13
MC/DC100%1/1
$args{"return_type"} // "val"
$args{"return_type"}
Truth table100%2/2
A
A= $args{"return_type"}
Aresult
11
00
44 # old values still supported but deprecated
45 4 ▶ $rt = 'bool_coerced+val' if $rt eq 'status+val';
Branch: 1 branch
BranchTrueFalseTotal
if $rt eq "status+val" 0 4 4
46 4 ▶ $rt = 'bool_coerced+str_errmsg+val' if $rt eq 'status+err+val';
Branch: 1 branch
BranchTrueFalseTotal
if $rt eq "status+err+val" 0 4 4
47  
48 4 my $rules = Data::Sah::CoerceCommon::get_coerce_rules(
49 %args,
50 compiler=>'js',
51 data_term=>'data',
52 );
53  
54 4 my $code;
55 4 ▶ if (@$rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rules) { } 4 0 4
56 4 my $expr;
57 4 for my $i (reverse 0..$#{$rules}) {
58 7 my $rule = $rules->[$i];
59  
60 7 my $prev_term;
61 7 ▶ if ($i == $#{$rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#{$rules;}) { } 4 3 7
62 4 ▶ if ($rt eq 'val') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt eq 'val') { } 3 1 4
elsif ($rt eq 'bool_coerced+val') { } 1 0 1
63 3 $prev_term = 'data';
64 } elsif ($rt eq 'bool_coerced+val') {
65 1 $prev_term = '[null, data]';
66 } else { # bool_coerced+str_errmsg+val
67 0 $prev_term = '[null, null, data]';
68 }
69 } else {
70 3 $prev_term = $expr;
71 }
72  
73 7 ▶ if ($rt eq 'val') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt eq 'val') { } 5 2 7
elsif ($rt eq 'bool_coerced+val') { } 2 0 2
74 5 ▶ if ($rule->{meta}{might_fail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'meta'}{'might_fail'}) { } 1 4 5
75 1 $expr = "(function() { if ($rule->{expr_match}) { var _tmp1 = $rule->{expr_coerce}; if (_tmp1[0]) { return null } else { return _tmp1[1] } } else { return $prev_term } })()";
76 } else {
77 4 $expr = "($rule->{expr_match}) ? ($rule->{expr_coerce}) : $prev_term";
78 }
79 } elsif ($rt eq 'bool_coerced+val') {
80 2 ▶ if ($rule->{meta}{might_fail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'meta'}{'might_fail'}) { } 0 2 2
81 0 $expr = "(function() { if ($rule->{expr_match}) { var _tmp1 = $rule->{expr_coerce}; if (_tmp1[0]) { return [true, null] } else { return [true, _tmp1[1]] } } else { return $prev_term } })()";
82 } else {
83 2 $expr = "($rule->{expr_match}) ? [true, $rule->{expr_coerce}] : $prev_term";
84 }
85 } else { # bool_coerced+str_errmsg+val
86 0 ▶ if ($rule->{meta}{might_fail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'meta'}{'might_fail'}) { } 0 0 0
87 0 $expr = "(function() { if ($rule->{expr_match}) { var _tmp1 = $rule->{expr_coerce}; if (_tmp1[0]) { return [true, _tmp1[0], null] } else { return [true, null, _tmp1[1]] } } else { return $prev_term } })()";
88 } else {
89 0 $expr = "($rule->{expr_match}) ? [true, null, $rule->{expr_coerce}] : $prev_term";
90 }
91 }
92 }
93  
94 4 ▶ $code = join(
Branch: 2 branches
BranchTrueFalseTotal
$rt eq 'val' ? : 3 1 4
$rt eq 'bool_coerced+val' ? : 1 0 1
95 "",
96 "function (data) {\n",
97 " if (data === undefined || data === null) {\n",
98 " ", ($rt eq 'val' ? "return null;" :
99 $rt eq 'bool_coerced+val' ? "return [null, null];" :
100 "return [null, null, null];" # bool_coerced+str_errmsg+val
101 ), "\n",
102 " }\n",
103 " return ($expr);\n",
104 "}",
105 );
106 } else {
107 0 ▶ if ($rt eq 'val') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt eq 'val') { } 0 0 0
elsif ($rt eq 'bool_coerced+val') { } 0 0 0
108 0 $code = 'function (data) { return data }';
109 } elsif ($rt eq 'bool_coerced+val') {
110 0 $code = 'function (data) { return [null, data] }';
111 } else { # bool_coerced+str_errmsg+val
112 0 $code = 'function (data) { return [null, null, data] }';
113 }
114 }
115  
116 4 ▶ if ($Log_Coercer_Code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log_Coercer_Code) 0 4 4
117 0 log_trace("Coercer code (gen args: %s): %s", \%args, $code);
118 }
119  
120 4 ▶ return $code if $args{source};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"source"} 0 4 4
121  
122 4 require JSON;
123 4 state $json = JSON->new->allow_nonref;
124  
125 4 ▶ my $engine = $args{engine} // 'quickjs';
Condition50%1/2
$args{"engine"} // "quickjs"
l!l
04
MC/DC0%0/1
$args{"engine"} // "quickjs"
$args{"engine"}
Truth table50%1/2
A
A= $args{"engine"}
Aresult
11
00
126  
127 4 ▶ if ($engine eq 'quickjs') {
Branch: 2 branches
BranchTrueFalseTotal
if ($engine eq 'quickjs') { } 4 0 4
elsif ($engine eq 'nodejs') { } 0 0 0
128  
129 21 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 21 require JavaScript::QuickJS;
131  
132 21 my $data = shift;
133  
134 21 my $src = "var coercer = $code; coercer(".$json->encode($data).")";
135 21 JavaScript::QuickJS->new->eval($src);
136 4 };
137  
138 } elsif ($engine eq 'nodejs') {
139  
140 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
141 0 require File::Temp;
142 0 require IPC::System::Options;
143 0 require Nodejs::Util;
144 #require String::ShellQuote;
145  
146 0 state $nodejs_path = Nodejs::Util::get_nodejs_path();
147 0 ▶ die "Can't find node.js in PATH" unless $nodejs_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodejs_path 0 0 0
148  
149 0 my $data = shift;
150  
151 # code to be sent to nodejs
152 0 my $src = "var coercer = $code; ".
153 "console.log(JSON.stringify(coercer(".
154 $json->encode($data).")))";
155  
156 0 my ($jsh, $jsfn) = File::Temp::tempfile();
157 0 print $jsh $src;
158 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
159  
160 0 my $out = IPC::System::Options::readpipe($nodejs_path, $jsfn);
161 0 return $json->decode($out);
162 0 };
163  
164 } else {
165  
166 0 die "Unknown engine '$engine'";
167  
168 }
169 }
170  
171 6 1;
172 # ABSTRACT: Generate coercer code
173  
174 __END__
↑ Summary CoerceCommon.pm »