blib/lib/App/NDTools/Slurp.pm

filter: Statementstmt 75.9% 142 / 187 Branchbran 60.2% 53 / 88 Conditioncond 81.2% 13 / 16 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 7 totaltotal 71.7% 236 / 329 CC 55 SCAR 36.9
CC
55
Cov
71%
_encode_yaml
160.7
_decode_yaml
16.8
s_encode
8.1
CRAP 40.1
↑ Summary blib/lib/App/NDTools/Test.pm »
1 package App::NDTools::Slurp;
2  
3 # input/output related subroutines for NDTools
4  
5 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Scalar::Util qw(readonly);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 ▶use App::NDTools::INC;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use App::NDTools::Util qw(is_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use Log::Log4Cli;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 our @EXPORT_OK = qw(
19 s_decode
20 s_dump
21 s_dump_file
22 s_encode
23 s_fmt_by_uri
24 s_load
25 s_load_uri
26 );
27  
28 22 our %FORMATS = (
29 JSON => {
30 allow_nonref => 1,
31 canonical => 1,
32 pretty => 1,
33 relaxed => 1,
34 space_before => 0,
35 },
36 );
37  
38 22 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 22 TRUE => JSON::true,
40 FALSE => JSON::false,
41 22 };
42  
43 18 ▶sub _decode_yaml($) {
Subroutine _decode_yaml: called CC=15 SCAR=28.2
44 18 require YAML::XS;
45  
46 18 my $data = YAML::XS::Load($_[0]);
47  
48 # YAML::XS decode boolean vals as PL_sv_yes and PL_sv_no, both - read only
49 # at least until https://github.com/ingydotnet/yaml-libyaml-pm/issues/25
50 # second thing here: get rid of dualvars: YAML::XS load numbers as
51 # dualvars, but JSON::XS dumps them as strings =(
52  
53 18 my @stack = (\$data);
54 18 my $ref;
55  
56 18 ▶ while ($ref = shift @stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = shift @stack) 49 18 67
57 49 ▶ if (ref ${$ref} eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref ${$ref;} eq 'ARRAY') { } 4 45 49
elsif (ref ${$ref;} eq 'HASH') { } 43 2 45
elsif (&is_number(${$ref;})) { } 1 1 2
58 4 for (0 .. $#{${$ref}}) {
59 13 ▶ if (ref ${$ref}->[$_]) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref ${$ref;}->[$_]) { } 3 10 13
elsif (&readonly(${$ref;}->[$_])) { } 0 10 10
elsif (&is_number(${$ref;}->[$_])) { } 7 3 10
60 3 push @stack, \${$ref}->[$_];
61 10 } elsif (readonly ${$ref}->[$_]) {
62 0 ▶ splice @{${$ref}}, $_, 1, (${$ref}->[$_] ? TRUE : FALSE);
Branch: 1 branch
BranchTrueFalseTotal
${$ref;}->[$_] ? : 0 0 0
63 10 } elsif (is_number ${$ref}->[$_]) {
64 7 ${$ref}->[$_] += 0;
65 }
66 }
67 45 } elsif (ref ${$ref} eq 'HASH') {
68 43 for (keys %{${$ref}}) {
69 117 ▶ if (ref ${$ref}->{$_}) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref ${$ref;}->{$_}) { } 28 89 117
elsif (&readonly(${$ref;}->{$_})) { } 0 89 89
elsif (&is_number(${$ref;}->{$_})) { } 23 66 89
70 28 push @stack, \${$ref}->{$_};
71 89 } elsif (readonly ${$ref}->{$_}) {
72 0 ▶ ${$ref}->{$_} = delete ${$ref}->{$_} ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
delete ${$ref;}->{$_} ? : 0 0 0
73 89 } elsif (is_number ${$ref}->{$_}) {
74 23 ${$ref}->{$_} += 0;
75 }
76 }
77 2 } elsif (is_number ${$ref}) {
78 1 ${$ref} += 0;
79 }
80 }
81  
82 18 return $data;
83 }
84  
85 9 ▶sub _encode_yaml($) {
Subroutine _encode_yaml: called CC=17 SCAR=50.8
86 9 require YAML::XS;
87 9 my $modern_yaml_xs = eval { YAML::XS->VERSION(0.67) };
88  
89 # replace booleans for YAML::XS (accepts only boolean and JSON::PP::Boolean
90 # since 0.67 and PL_sv_yes/no in earlier versions). No roundtrip for
91 # versions < 0.67: 1 and 0 used for booleans (there is no way to set
92 # PL_sv_yes/no into arrays/hashes without XS code)
93  
94 9 my ($false, $true) = (0, 1);
95  
96 9 ▶ local $YAML::XS::Boolean = "JSON::PP" if ($modern_yaml_xs);
Branch: 1 branch
BranchTrueFalseTotal
if $modern_yaml_xs 9 0 9
97  
98 9 ▶ if ($modern_yaml_xs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modern_yaml_xs) 9 0 9
99 4 ▶ return YAML::XS::Dump($_[0]) if (ref TRUE eq 'JSON::PP::Boolean');
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ref bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) eq "JSON::PP::Boolean" 9 0 9
100  
101 0 require JSON::PP;
102 0 ($false, $true) = (JSON::PP::false(), JSON::PP::true());
103 }
104  
105 0 my @stack = (\$_[0]);
106 0 my $ref;
107 0 my $bool_type = ref TRUE;
108  
109 0 ▶ while ($ref = shift @stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = shift @stack) 0 0 0
110 0 ▶ if (ref ${$ref} eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref ${$ref;} eq 'ARRAY') { } 0 0 0
elsif (ref ${$ref;} eq 'HASH') { } 0 0 0
elsif (ref ${$ref;} eq $bool_type) { } 0 0 0
111 0 for (0 .. $#{${$ref}}) {
112 0 ▶ if (ref ${$ref}->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref ${$ref;}->[$_]) { } 0 0 0
elsif (ref ${$ref;}->[$_] eq $bool_type) { } 0 0 0
113 0 push @stack, \${$ref}->[$_];
114 0 } elsif (ref ${$ref}->[$_] eq $bool_type) {
115 0 ▶ ${$ref}->[$_] = ${$ref}->[$_] ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
${$ref;}->[$_] ? : 0 0 0
116 }
117 }
118 0 } elsif (ref ${$ref} eq 'HASH') {
119 0 for (keys %{${$ref}}) {
120 0 ▶ if (ref ${$ref}->{$_}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref ${$ref;}->{$_}) { } 0 0 0
elsif (ref ${$ref;}->{$_} eq $bool_type) { } 0 0 0
121 0 push @stack, \${$ref}->{$_};
122 0 } elsif (ref ${$ref}->{$_} eq $bool_type) {
123 0 ▶ ${$ref}->{$_} = ${$ref}->{$_} ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
${$ref;}->{$_} ? : 0 0 0
124 }
125 }
126 0 } elsif (ref ${$ref} eq $bool_type) {
127 0 ▶ ${$ref} = ${$ref} ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
${$ref;} ? : 0 0 0
128 }
129 }
130  
131 0 return YAML::XS::Dump($_[0]);
132 }
133  
134 363 ▶sub s_decode($$;$) {
Subroutine s_decode: called CC=6 SCAR=18.0
Pod undocumented
135 363 my ($data, $fmt, $opts) = @_;
136 363 my $format = uc($fmt);
137  
138 363 ▶ if ($format eq 'JSON') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'JSON') { } 344 19 363
elsif ($format eq 'YAML') { } 18 1 19
elsif ($format eq 'RAW') { } 1 0 1
139 344 ▶ my $o = { %{$FORMATS{JSON}}, %{$opts || {}} };
Condition50%1/2
$opts || {}
l!l
0344
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
140 344 $data = eval {
141 JSON->new(
142 )->allow_nonref($o->{allow_nonref}
143 )->relaxed($o->{relaxed}
144 344 )->decode($data);
145 };
146 } elsif ($format eq 'YAML') {
147 18 $data = eval { _decode_yaml($data) };
148 } elsif ($format eq 'RAW') {
149 ;
150 } else {
151 0 die_fatal "Unable to decode '$fmt' (not supported)";
152 }
153  
154 363 ▶ die_fatal "Failed to decode '$fmt': " . $@, 4 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 6 357 363
155  
156 357 return $data;
157 }
158  
159 130 ▶sub s_dump(@) {
Subroutine s_dump: called CC=4 SCAR=13.9
Pod undocumented
160 130 my ($uri, $fmt, $opts) = splice @_, 0, 3;
161  
162 130 ▶ $uri = \*STDOUT if ($uri eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq "-" 1 129 130
163  
164 130 ▶ $fmt = s_fmt_by_uri($uri) unless (defined $fmt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt 82 48 130
165 130 my $data = join('', map { s_encode($_, $fmt, $opts) } @_);
166  
167 130 ▶ if (ref $uri eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri eq 'GLOB') { } 42 88 130
168 42 print $uri $data;
169 } else {
170 88 s_dump_file($uri, $data);
171 }
172 }
173  
174 88 ▶sub s_dump_file($$) {
Subroutine s_dump_file: called CC=2 SCAR=7.0
Pod undocumented
175 88 my ($file, $data) = @_;
176  
177 88 ▶ open(my $fh, '>', $file) or die_fatal "Failed to open '$file' ($!)", 2;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or &die_fatal("Failed to open '${file}' ($!)", 2) 88 0 88
178 88 print $fh $data;
179 88 close($fh);
180 }
181  
182 143 ▶sub s_encode($$;$) {
Subroutine s_encode: called CC=8 SCAR=20.9
Pod undocumented
183 143 my ($data, $fmt, $opts) = @_;
184 143 my $format = uc($fmt);
185  
186 143 ▶ if ($format eq 'JSON' or $format eq 'RAW' and ref $data) {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'JSON' or $format eq 'RAW' and ref $data) { } 131 12 143
elsif ($format eq 'YAML') { } 9 3 12
elsif ($format eq 'RAW') { } 3 0 3
Condition100%6/6
$format eq "JSON" or $format eq "RAW" and ref $data
l!l&&r!l&&!r
129212
$format eq "RAW" and ref $data
!ll&&!rl&&r
932
MC/DC100%3/3
$format eq "JSON" or $format eq "RAW" and ref $data
$format eq "JSON" $format eq "RAW" ref $data
Truth table100%4/4
A or B and C
A= $format eq "JSON"B= $format eq "RAW"C= ref $data
ABCresult
1XX1
0111
00X0
0100
187 131 ▶ my $o = { %{$FORMATS{JSON}}, %{$opts || {}} };
Condition100%2/2
$opts || {}
l!l
4982
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
188 131 $data = eval {
189 JSON->new(
190 )->allow_nonref($o->{allow_nonref}
191 )->canonical($o->{canonical}
192 )->pretty($o->{pretty}
193 )->space_before($o->{space_before}
194 131 )->encode($data);
195 };
196 } elsif ($format eq 'YAML') {
197 9 $data = eval { _encode_yaml($data) };
198 } elsif ($format eq 'RAW') {
199 3 $data .= "\n";
200 } else {
201 0 die_fatal "Unable to encode to '$fmt' (not supported)";
202 }
203  
204 143 ▶ die_fatal "Failed to encode structure to $fmt: " . $@, 4 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 143 143
205  
206 143 return $data;
207 }
208  
209 418 ▶sub s_fmt_by_uri($) {
Subroutine s_fmt_by_uri: called CC=5 SCAR=16.2
Pod undocumented
210 418 my @names = split(/\./, basename(shift));
211 418 ▶ if (@names and @names > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@names and @names > 1) 413 5 418
Condition66%2/3
@names and @names > 1
!ll&&!rl&&r
05413
MC/DC50%1/2
@names and @names > 1
@names @names > 1
Truth table66%2/3
A and B
A= @namesB= @names > 1
ABresult
0X0
100
111
212 413 my $ext = uc($names[-1]);
213 413 ▶ return 'YAML' if ($ext eq 'YML' or $ext eq 'YAML');
Branch: 1 branch
BranchTrueFalseTotal
if $ext eq "YML" or $ext eq "YAML" 1 412 413
Condition66%2/3
$ext eq "YML" or $ext eq "YAML"
l!l&&r!l&&!r
01412
MC/DC50%1/2
$ext eq "YML" or $ext eq "YAML"
$ext eq "YML" $ext eq "YAML"
Truth table66%2/3
A or B
A= $ext eq "YML"B= $ext eq "YAML"
ABresult
1X1
011
000
214 }
215  
216 417 return 'JSON'; # by default
217 }
218  
219 356 ▶sub s_load($$;@) {
Subroutine s_load: called CC=3 SCAR=11.0
Pod undocumented
220 356 my ($uri, $fmt, %opts) = @_;
221  
222 356 ▶ $uri = \*STDIN if ($uri eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq "-" 1 355 356
223 356 my $data = s_load_uri($uri);
224 354 ▶ $fmt = s_fmt_by_uri($uri) unless (defined $fmt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt 336 18 354
225  
226 354 return s_decode($data, $fmt);
227 }
228  
229 356 ▶sub s_load_uri($) {
Subroutine s_load_uri: called CC=3 SCAR=11.0
Pod undocumented
230 356 my $uri = shift;
231 356 my $data;
232  
233 356 ▶ if (ref $uri eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri eq 'GLOB') { } 3 353 356
234 3 $data = do { local $/; <$uri> };
235 } else {
236 353 ▶ open(my $fh, '<', $uri) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $uri or &die_fatal("Failed to open file '${uri}' ($!)", 2) 351 2 353
237 die_fatal "Failed to open file '$uri' ($!)", 2;
238 351 $data = do { local $/; <$fh> }; # load whole file
239 351 close($fh);
240 }
241  
242 354 return $data;
243 }
244  
245 22 1;
↑ Summary blib/lib/App/NDTools/Test.pm »