Catmandu/Fix.pm

filter: Statementstmt 52.8% 203 / 384 Branchbran 37.3% 47 / 126 Conditioncond 44.1% 19 / 43 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 69.0% 38 / 55 Podpod 3.7% 1 / 27 totaltotal 47.9% 316 / 659 CC 80 SCAR 42.4
CC
80
Cov
49%
emit_delete_key
342.0
_emit_create_path
156.0
emit_set_key
72.0
CRAP 69.2
↑ Summary Catmandu/Cmd/export.pm »
1 package Catmandu::Fix;
2  
3 154 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 154 our $VERSION = '1.2025';
6  
7 154 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 154 ▶use Catmandu::Fix::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 154 ▶use Text::Hogan::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 154 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 154 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 154 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 154 ▶use Catmandu::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 is_string
15 is_array_ref
16 is_hash_ref
17 is_code_ref
18 is_glob_ref
19 is_instance
20 is_able
21 require_package
22 154 );
23 154 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 154 with 'Catmandu::Logger';
26 154 with 'Catmandu::Emit';
27  
28 154 has parser => (is => 'lazy');
29 154 has fixer => (is => 'lazy', init_arg => undef);
30 has _captures =>
31 496 ▶ (is => 'ro', lazy => 1, init_arg => undef, default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
32 154 has var =>
33 (is => 'ro', lazy => 1, init_arg => undef, builder => '_generate_var');
34 0 ▶has _fixes => (is => 'ro', init_arg => 'fixes', default => sub {[]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 154 has fixes =>
36 (is => 'ro', lazy => 1, init_arg => undef, builder => '_build_fixes');
37 154 has _reject_var => (
38 is => 'ro',
39 lazy => 1,
40 init_arg => undef,
41 builder => '_build_reject_var'
42 );
43 154 has _fixes_var => (
44 is => 'ro',
45 lazy => 1,
46 init_arg => undef,
47 builder => '_build_fixes_var'
48 );
49 154 has preprocess => (is => 'ro');
50 154 has _hogan =>
51 (is => 'ro', lazy => 1, init_arg => undef, builder => '_build_hogan');
52 154 has _hogan_vars => (is => 'ro', init_arg => 'variables');
53  
54 150 ▶sub _build_parser {
Subroutine _build_parser: called CC=1 SCAR=0.0
55 150 Catmandu::Fix::Parser->new;
56 }
57  
58 496 ▶sub _build_fixes {
Subroutine _build_fixes: called CC=10 SCAR=23.0
59 496 my ($self) = @_;
60 496 my $fixes_arg = $self->_fixes;
61 496 my $fixes = [];
62  
63 496 for my $fix (@$fixes_arg) {
64  
65 498 ▶ if (is_code_ref($fix)) {
Branch: 5 branches
BranchTrueFalseTotal
if (is_code_ref($fix)) { } 1 497 498
elsif (ref $fix and ref($fix) =~ /^IO::/) { } 1 496 497
elsif (is_glob_ref($fix)) { } 1 495 496
elsif (ref $fix) { } 344 151 495
elsif (is_string($fix)) { } 151 0 151
66 1 push @$fixes, require_package('Catmandu::Fix::code')->new($fix);
67 }
68 ▶ elsif (ref $fix && ref($fix) =~ /^IO::/) {
Condition100%3/3
ref $fix and ref($fix) =~ /^IO::/
!ll&&!rl&&r
1513451
MC/DC100%2/2
ref $fix and ref($fix) =~ /^IO::/
ref $fix ref($fix) =~ /^IO::/
Truth table100%3/3
A and B
A= ref $fixB= ref($fix) =~ /^IO::/
ABresult
0X0
100
111
69 1 my $txt = Catmandu::Util::read_io($fix);
70 1 $txt = $self->_preprocess($txt);
71 1 push @$fixes, @{$self->parser->parse($txt)};
72 }
73 elsif (is_glob_ref($fix)) {
74 1 my $fh = Catmandu::Util::io($fix, binmode => ':encoding(UTF-8)');
75 1 my $txt = Catmandu::Util::read_io($fh);
76 1 $txt = $self->_preprocess($txt);
77 1 push @$fixes, @{$self->parser->parse($txt)};
78 }
79 elsif (ref $fix) {
80 344 push @$fixes, $fix;
81 }
82 elsif (is_string($fix)) {
83 151 ▶ if ($fix =~ /[^\s]/ && $fix !~ /\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix =~ /[^\s]/ and not $fix =~ /\(/) 10 141 151
Condition66%2/3
$fix =~ /[^\s]/ and not $fix =~ /\(/
!ll&&!rl&&r
014110
MC/DC50%1/2
$fix =~ /[^\s]/ and not $fix =~ /\(/
$fix =~ /[^\s]/ not $fix =~ /\(/
Truth table66%2/3
A and not(B)
A= $fix =~ /[^\s]/B= not $fix =~ /\(/
ABresult
0X0
100
111
84 10 $fix = Path::Tiny::path($fix)->slurp_utf8;
85 }
86 151 $fix = $self->_preprocess($fix);
87 151 push @$fixes, @{$self->parser->parse($fix)};
88 }
89 }
90  
91 493 $fixes;
92 }
93  
94 495 ▶sub _build_fixer {
Subroutine _build_fixer: called CC=1 SCAR=0.0
95 495 my ($self) = @_;
96  
97 495 my $reject = $self->_reject;
98 495 my $sub = $self->_eval_sub(
99 $self->emit,
100 args => [$self->var],
101 captures => $self->_captures
102 );
103  
104 512 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.4
105 512 my $data = $_[0];
106  
107 512 ▶ if (is_hash_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($data)) 490 22 512
108 490 my $d = $sub->($data);
109 486 ▶ return if ref $d && $d == $reject;
Branch: 1 branch
BranchTrueFalseTotal
if ref $d and $d == $reject 9 477 486
Condition66%2/3
ref $d and $d == $reject
!ll&&!rl&&r
04779
MC/DC50%1/2
ref $d and $d == $reject
ref $d $d == $reject
Truth table66%2/3
A and B
A= ref $dB= $d == $reject
ABresult
0X0
100
111
110 477 return $d;
111 }
112  
113 22 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($data)) 9 13 22
114 16 ▶ return [grep {!(ref $_ && $_ == $reject)}
Condition66%2/3
ref $_ && $_ == $reject
!ll&&!rl&&r
0142
MC/DC50%1/2
ref $_ && $_ == $reject
ref $_ $_ == $reject
Truth table66%2/3
A && B
A= ref $_B= $_ == $reject
ABresult
0X0
100
111
115 9 map {$sub->($_)} @$data];
116 }
117  
118 13 ▶ if (is_code_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_code_ref($data)) 3 10 13
119 6 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
120 6 while (1) {
121 6 ▶ my $d = $sub->($data->() // return);
Condition100%2/2
&$data() // (return)
l!l
51
MC/DC100%1/1
&$data() // (return)
&$data()
Truth table100%2/2
A
A= &$data()
Aresult
11
00
122 5 ▶ return if ref $d && $d == $reject;
Branch: 1 branch
BranchTrueFalseTotal
if ref $d and $d == $reject 0 5 5
Condition33%1/3
ref $d and $d == $reject
!ll&&!rl&&r
050
MC/DC0%0/2
ref $d and $d == $reject
ref $d $d == $reject
Truth table33%1/3
A and B
A= ref $dB= $d == $reject
ABresult
0X0
100
111
123 5 return $d;
124 }
125 3 };
126 }
127  
128 10 ▶ if ( is_instance($data)
Branch: 1 branch
BranchTrueFalseTotal
if (is_instance($data) and is_able($data, "does") and $data->does("Catmandu::Iterable")) 9 1 10
Condition66%4/6
is_instance($data) and is_able($data, "does") and $data->does("Catmandu::Iterable")
!ll&&!rl&&r
109
is_instance($data) and is_able($data, "does")
!ll&&!rl&&r
019
MC/DC33%1/3
is_instance($data) and is_able($data, "does") and $data->does("Catmandu::Iterable")
is_instance($data) is_able($data, "does") $data->does("Catmandu::Iterable")
Truth table50%2/4
A and B and C
A= is_instance($data)B= is_able($data, "does")C= $data->does("Catmandu::Iterable")
ABCresult
0XX0
10X0
1100
1111
129 && is_able($data, 'does')
130 && $data->does('Catmandu::Iterable'))
131 {
132 24 ▶ return $data->map(sub {$sub->($_[0])})
Subroutine __ANON__: called CC=1 SCAR=0.0
133 9 ▶ ->reject(sub {ref $_[0] && $_[0] == $reject});
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and $_[0] == $reject 23 0 23
134 }
135  
136 Catmandu::BadArg->throw(
137 1 "must be hashref, arrayref, coderef or iterable object");
138 492 };
139 }
140  
141 496 ▶sub _build_reject_var {
Subroutine _build_reject_var: called CC=1 SCAR=0.0
142 496 my ($self) = @_;
143 496 $self->capture($self->_reject);
144 }
145  
146 0 ▶sub _build_fixes_var {
Subroutine _build_fixes_var: not called CC=1 SCAR=6.9
147 0 my ($self) = @_;
148 0 $self->capture($self->fixes);
149 }
150  
151 1 ▶sub _build_hogan {
Subroutine _build_hogan: called CC=1 SCAR=0.0
152 1 Text::Hogan::Compiler->new;
153 }
154  
155 153 ▶sub _preprocess {
Subroutine _preprocess: called CC=4 SCAR=14.1
156 153 my ($self, $text) = @_;
157 153 ▶ return $text unless $self->preprocess || $self->_hogan_vars;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->preprocess or $self->_hogan_vars 152 1 153
Condition66%2/3
$self->preprocess or $self->_hogan_vars
l!l&&r!l&&!r
10152
MC/DC50%1/2
$self->preprocess or $self->_hogan_vars
$self->preprocess $self->_hogan_vars
Truth table66%2/3
A or B
A= $self->preprocessB= $self->_hogan_vars
ABresult
1X1
011
000
158 1 ▶ my $vars = $self->_hogan_vars || {};
Condition50%1/2
$self->_hogan_vars || {}
l!l
10
MC/DC0%0/1
$self->_hogan_vars || {}
$self->_hogan_vars
Truth table50%1/2
A
A= $self->_hogan_vars
Aresult
11
00
159 1 $self->_hogan->compile($text, {numeric_string_as_string => 1})
160 ->render($vars);
161 }
162  
163 515 ▶sub fix {
Subroutine fix: called CC=1 SCAR=0.0
Pod documented
164 515 my ($self, $data) = @_;
165 515 $self->fixer->($data);
166 }
167  
168 116 ▶sub generate_var {
Subroutine generate_var: called CC=1 SCAR=0.0
Pod undocumented
169 116 $_[0]->_generate_var;
170 }
171  
172 0 ▶sub generate_label {
Subroutine generate_label: not called CC=1 SCAR=6.9
Pod undocumented
173 0 $_[0]->_generate_label;
174 }
175  
176 1454 ▶sub capture {
Subroutine capture: called CC=1 SCAR=0.0
Pod undocumented
177 1454 my ($self, $capture) = @_;
178 1454 my $var = $self->_generate_var;
179 1454 $self->_captures->{$var} = $capture;
180 1454 $var;
181 }
182  
183 496 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
Pod undocumented
184 496 my ($self) = @_;
185 496 my $var = $self->var;
186 496 my $err = $self->_generate_var;
187 496 my $reject_var = $self->_reject_var;
188 496 my $perl = "";
189  
190 496 $perl .= "eval {";
191  
192 # Loop over all the fixes and emit their code
193 496 $perl .= $self->emit_fixes($self->fixes);
194  
195 493 $perl .= "return ${var};";
196 493 $perl .= $self->_reject_label . ": return ${reject_var};";
197 493 $perl .= "} or do {";
198 493 $perl .= $self->_emit_declare_vars($err, '$@');
199 493 $perl .= "${err}->throw if is_instance(${err},'Throwable::Error');";
200 493 $perl .= "Catmandu::FixError->throw(message => ${err}, data => ${var});";
201 493 $perl .= "};";
202  
203 493 $self->log->debug($perl);
204  
205 493 $perl;
206 }
207  
208 # Emit an array of fixes
209 759 ▶sub emit_fixes {
Subroutine emit_fixes: called CC=2 SCAR=6.9
Pod undocumented
210 759 my ($self, $fixes) = @_;
211 759 my $perl = '';
212  
213 759 ▶ for (my $i = 0; $i < @{$fixes}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$fixes;}) 763 759 1522
214 763 my $fix = $fixes->[$i];
215 763 $perl .= $self->emit_fix($fix);
216 }
217  
218 759 $perl;
219 }
220  
221 26 ▶sub emit_reject {
Subroutine emit_reject: called CC=1 SCAR=0.0
Pod undocumented
222 26 $_[0]->_emit_reject;
223 }
224  
225 888 ▶sub emit_fix {
Subroutine emit_fix: called CC=3 SCAR=11.0
Pod undocumented
226 888 my ($self, $fix) = @_;
227 888 my $perl;
228  
229 888 ▶ if ($fix->can('emit')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fix->can('emit')) { } 877 11 888
elsif ($fix->can('fix')) { } 11 0 11
230 $perl = $self->emit_block(
231 877 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
232 877 my ($label) = @_;
233 877 $fix->emit($self, $label);
234 }
235 877 );
236 }
237 elsif ($fix->can('fix')) {
238 11 my $var = $self->var;
239 11 my $ref = $self->_generate_var;
240 11 $self->_captures->{$ref} = $fix;
241 11 $perl = "${var} = ${ref}->fix(${var});";
242 }
243 else {
244 0 Catmandu::Error->throw('not a fix');
245 }
246  
247 888 $perl;
248 }
249  
250 877 ▶sub emit_block {
Subroutine emit_block: called CC=1 SCAR=0.0
Pod undocumented
251 877 my ($self, $cb) = @_;
252 877 my $label = $self->_generate_label;
253 877 my $perl = "${label}: {";
254 877 $perl .= $cb->($label);
255 877 $perl .= "};";
256 877 $perl;
257 }
258  
259 0 ▶sub emit_clear_hash_ref {
Subroutine emit_clear_hash_ref: not called CC=1 SCAR=6.9
Pod undocumented
260 0 my ($self, $var) = @_;
261 0 "undef %{${var}} if is_hash_ref(${var});";
262 }
263  
264 0 ▶sub emit_value {
Subroutine emit_value: not called CC=1 SCAR=6.9
Pod undocumented
265 0 shift->_emit_value(@_);
266 }
267  
268 8 ▶sub emit_string {
Subroutine emit_string: called CC=1 SCAR=0.0
Pod undocumented
269 8 shift->_emit_string(@_);
270 }
271  
272 0 ▶sub emit_match {
Subroutine emit_match: not called CC=1 SCAR=6.9
Pod undocumented
273 0 my ($self, $pattern) = @_;
274 0 $pattern =~ s/\//\\\//g;
275 0 $pattern =~ s/\\$/\\\\/; # pattern can't end with an escape in m/.../
276 0 "m/$pattern/";
277 }
278  
279 0 ▶sub emit_substitution {
Subroutine emit_substitution: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my ($self, $pattern, $replace) = @_;
281 0 $pattern =~ s/\//\\\//g;
282 0 $pattern =~ s/\\$/\\\\/; # pattern can't end with an escape in m/.../
283 0 $replace =~ s/\//\\\//g;
284 0 $replace =~ s/\\$/\\\\/; # pattern can't end with an escape in m/.../
285 0 "s/$pattern/$replace/";
286 }
287  
288 4 ▶sub emit_declare_vars {
Subroutine emit_declare_vars: called CC=1 SCAR=0.0
Pod undocumented
289 4 shift->_emit_declare_vars(@_);
290 }
291  
292 0 ▶sub emit_new_scope {
Subroutine emit_new_scope: not called CC=1 SCAR=6.9
Pod undocumented
293 0 "{";
294 }
295  
296 0 ▶sub emit_end_scope {
Subroutine emit_end_scope: not called CC=1 SCAR=6.9
Pod undocumented
297 0 "};";
298 }
299  
300 0 ▶sub emit_foreach {
Subroutine emit_foreach: not called CC=1 SCAR=6.9
Pod undocumented
301 0 my ($self, $var, $cb) = @_;
302 0 my $perl = "";
303 0 my $v = $self->_generate_var;
304 0 $perl .= "foreach (\@{${var}}) {";
305 0 $perl .= $self->emit_declare_vars($v, '$_');
306 0 $perl .= $cb->($v);
307 0 $perl .= "}";
308 0 $perl;
309 }
310  
311 4 ▶sub emit_foreach_key {
Subroutine emit_foreach_key: called CC=1 SCAR=0.0
Pod undocumented
312 4 my ($self, $var, $cb) = @_;
313 4 my $perl = "";
314 4 my $v = $self->_generate_var;
315 4 $perl .= "foreach (keys(\%{${var}})) {";
316 4 $perl .= $self->emit_declare_vars($v, '$_');
317 4 $perl .= $cb->($v);
318 4 $perl .= "}";
319 4 $perl;
320 }
321  
322 8 ▶sub emit_walk_path {
Subroutine emit_walk_path: called CC=2 SCAR=6.9
Pod undocumented
323 8 my ($self, $var, $keys, $cb) = @_;
324  
325 8 $keys = [@$keys]; # protect keys
326  
327 8 ▶ if (@$keys) { # protect $var
Branch: 1 branch
BranchTrueFalseTotal
if (@$keys) { } 6 2 8
328 6 my $v = $self->_generate_var;
329 6 $self->_emit_declare_vars($v, $var)
330 . $self->_emit_walk_path($v, $keys, $cb);
331 }
332 else {
333 2 $cb->($var);
334 }
335 }
336  
337 12 ▶sub _emit_walk_path {
Subroutine _emit_walk_path: called CC=6 SCAR=24.2
338 12 my ($self, $var, $keys, $cb) = @_;
339  
340 12 ▶ @$keys || return $cb->($var);
Branch: 1 branch
BranchTrueFalseTotal
@$keys or return &$cb($var) 6 6 12
341  
342 6 my $key = shift @$keys;
343 6 my $str_key = $self->emit_string($key);
344 6 my $perl = "";
345  
346 6 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 0 6 6
elsif ($key eq '*') { } 0 6 6
347 0 $perl .= "if (is_hash_ref(${var})) {";
348 0 $perl .= "${var} = ${var}->{${str_key}};";
349 0 $perl .= $self->_emit_walk_path($var, [@$keys], $cb);
350 0 $perl .= "} elsif (is_array_ref(${var}) && \@{${var}} > ${key}) {";
351 0 $perl .= "${var} = ${var}->[${key}];";
352 0 $perl .= $self->_emit_walk_path($var, [@$keys], $cb);
353 0 $perl .= "}";
354 }
355 elsif ($key eq '*') {
356 0 my $v = $self->_generate_var;
357 0 $perl .= "if (is_array_ref(${var})) {";
358 $perl .= $self->emit_foreach(
359 $var,
360 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 0 return $self->_emit_walk_path(shift, $keys, $cb);
362 }
363 0 );
364 0 $perl .= "}";
365 }
366 else {
367 6 ▶ if ($key eq '$first') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 0 6 6
elsif ($key eq '$last') { } 0 6 6
368 0 $perl .= "if (is_array_ref(${var}) && \@{${var}}) {";
369 0 $perl .= "${var} = ${var}->[0];";
370 }
371 elsif ($key eq '$last') {
372 0 $perl .= "if (is_array_ref(${var}) && \@{${var}}) {";
373 0 $perl .= "${var} = ${var}->[\@{${var}} - 1];";
374 }
375 else {
376 6 $perl .= "if (is_hash_ref(${var})) {";
377 6 $perl .= "${var} = ${var}->{${str_key}};";
378 }
379 6 $perl .= $self->_emit_walk_path($var, $keys, $cb);
380 6 $perl .= "}";
381 }
382  
383 6 $perl;
384 }
385  
386 0 ▶sub emit_create_path {
Subroutine emit_create_path: not called CC=1 SCAR=6.9
Pod undocumented
387 0 my ($self, $var, $keys, $cb) = @_;
388 0 $self->_emit_create_path($var, [@$keys], $cb);
389 }
390  
391 0 ▶sub _emit_create_path {
Subroutine _emit_create_path: not called CC=12 SCAR=50.5
392 0 my ($self, $var, $keys, $cb) = @_;
393  
394 0 ▶ @$keys || return $cb->($var);
Branch: 1 branch
BranchTrueFalseTotal
@$keys or return &$cb($var) 0 0 0
395  
396 0 my $key = shift @$keys;
397 0 my $str_key = $self->emit_string($key);
398 0 my $perl = "";
399  
400 0 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 0 0 0
elsif ($key eq '*') { } 0 0 0
401 0 my $v1 = $self->_generate_var;
402 0 my $v2 = $self->_generate_var;
403 0 $perl .= "if (is_hash_ref(${var})) {";
404 0 $perl .= "my ${v1} = ${var};";
405 0 $perl
406 .= $self->_emit_create_path("${v1}->{${str_key}}", [@$keys], $cb);
407 0 $perl .= "} elsif (is_maybe_array_ref(${var})) {";
408 0 $perl .= "my ${v2} = ${var} //= [];";
409 0 $perl .= $self->_emit_create_path("${v2}->[${key}]", [@$keys], $cb);
410 0 $perl .= "}";
411 }
412 elsif ($key eq '*') {
413 0 my $v1 = $self->_generate_var;
414 0 my $v2 = $self->_generate_var;
415 0 $perl .= "if (is_array_ref(${var})) {";
416 0 $perl .= "my ${v1} = ${var};";
417 0 $perl .= "for (my ${v2} = 0; ${v2} < \@{${v1}}; ${v2}++) {";
418 0 $perl .= $self->_emit_create_path("${v1}->[${v2}]", $keys, $cb);
419 0 $perl .= "}";
420 0 $perl .= "}";
421 }
422 else {
423 0 my $v = $self->_generate_var;
424 0 ▶ if ( $key eq '$first'
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '$first' or $key eq '$last' or $key eq '$prepend' or $key eq '$append') { } 0 0 0
Condition0%0/9
$key eq "\$first" or $key eq "\$last" or $key eq "\$prepend" or $key eq "\$append"
l!l&&r!l&&!r
000
$key eq "\$first" or $key eq "\$last" or $key eq "\$prepend"
l!l&&r!l&&!r
000
$key eq "\$first" or $key eq "\$last"
l!l&&r!l&&!r
000
MC/DC0%0/4
$key eq "\$first" or $key eq "\$last" or $key eq "\$prepend" or $key eq "\$append"
$key eq "\$first" $key eq "\$last" $key eq "\$prepend" $key eq "\$append"
Truth table0%0/5
A or B or C or D
A= $key eq "\$first"B= $key eq "\$last"C= $key eq "\$prepend"D= $key eq "\$append"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
425 || $key eq '$last'
426 || $key eq '$prepend'
427 || $key eq '$append')
428 {
429 0 $perl .= "if (is_maybe_array_ref(${var})) {";
430 0 $perl .= "my ${v} = ${var} //= [];";
431 0 ▶ if ($key eq '$first') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 0 0 0
elsif ($key eq '$last') { } 0 0 0
elsif ($key eq '$prepend') { } 0 0 0
elsif ($key eq '$append') { } 0 0 0
432 0 $perl .= $self->_emit_create_path("${v}->[0]", $keys, $cb);
433 }
434 elsif ($key eq '$last') {
435 0 $perl .= "if (\@${v}) {";
436 0 $perl .= $self->_emit_create_path("${v}->[\@${v} - 1]",
437 [@$keys], $cb);
438 0 $perl .= "} else {";
439 0 $perl .= $self->_emit_create_path("${v}->[0]", [@$keys], $cb);
440 0 $perl .= "}";
441 }
442 elsif ($key eq '$prepend') {
443 0 $perl .= "if (\@${v}) {";
444 0 $perl .= "unshift(\@${v}, undef);";
445 0 $perl .= "}";
446 0 $perl .= $self->_emit_create_path("${v}->[0]", $keys, $cb);
447 }
448 elsif ($key eq '$append') {
449 0 $perl
450 .= $self->_emit_create_path("${v}->[\@${v}]", $keys, $cb);
451 }
452 0 $perl .= "}";
453 }
454 else {
455 0 $perl .= "if (is_maybe_hash_ref(${var})) {";
456 0 $perl .= "my ${v} = ${var} //= {};";
457 0 $perl
458 .= $self->_emit_create_path("${v}->{${str_key}}", $keys, $cb);
459 0 $perl .= "}";
460 }
461 }
462  
463 0 $perl;
464 }
465  
466 0 ▶sub emit_get_key {
Subroutine emit_get_key: not called CC=6 SCAR=37.4
Pod undocumented
467 0 my ($self, $var, $key, $cb) = @_;
468  
469 0 ▶ return $cb->($var) unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
470  
471 0 my $str_key = $self->emit_string($key);
472 0 my $perl = "";
473  
474 0 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 0 0 0
elsif ($key eq '$first') { } 0 0 0
elsif ($key eq '$last') { } 0 0 0
elsif ($key eq '*') { } 0 0 0
475 0 $perl .= "if (is_hash_ref(${var}) && exists(${var}->{${str_key}})) {";
476 0 $perl .= $cb->("${var}->{${str_key}}");
477 0 $perl .= "} elsif (is_array_ref(${var}) && \@{${var}} > ${key}) {";
478 0 $perl .= $cb->("${var}->[${key}]");
479 0 $perl .= "}";
480 }
481 elsif ($key eq '$first') {
482 0 $perl .= "if (is_array_ref(${var}) && \@{${var}}) {";
483 0 $perl .= $cb->("${var}->[0]");
484 0 $perl .= "}";
485 }
486 elsif ($key eq '$last') {
487 0 $perl .= "if (is_array_ref(${var}) && \@{${var}}) {";
488 0 $perl .= $cb->("${var}->[\@{${var}} - 1]");
489 0 $perl .= "}";
490 }
491 elsif ($key eq '*') {
492 0 my $i = $self->_generate_var;
493 0 $perl .= "if (is_array_ref(${var})) {";
494 0 $perl .= "for (my ${i} = 0; ${i} < \@{${var}}; ${i}++) {";
495 0 $perl .= $cb->("${var}->[${i}]", $i);
496 0 $perl .= "}}";
497 }
498 else {
499 0 $perl .= "if (is_hash_ref(${var}) && exists(${var}->{${str_key}})) {";
500 0 $perl .= $cb->("${var}->{${str_key}}");
501 0 $perl .= "}";
502 }
503  
504 0 $perl;
505 }
506  
507 0 ▶sub emit_set_key {
Subroutine emit_set_key: not called CC=8 SCAR=42.8
Pod undocumented
508 0 my ($self, $var, $key, $val) = @_;
509  
510 0 ▶ return "${var} = $val;" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
511  
512 0 my $perl = "";
513 0 my $str_key = $self->emit_string($key);
514  
515 0 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 0 0 0
elsif ($key eq '$first') { } 0 0 0
elsif ($key eq '$last') { } 0 0 0
elsif ($key eq '$prepend') { } 0 0 0
elsif ($key eq '$append') { } 0 0 0
elsif ($key eq '*') { } 0 0 0
516 0 $perl .= "if (is_hash_ref(${var})) {";
517 0 $perl .= "${var}->{${str_key}} = $val;";
518 0 $perl .= "} elsif (is_array_ref(${var})) {";
519 0 $perl .= "${var}->[${key}] = $val;";
520 0 $perl .= "}";
521 }
522 elsif ($key eq '$first') {
523 0 $perl .= "if (is_array_ref(${var})) {";
524 0 $perl .= "${var}->[0] = $val;";
525 0 $perl .= "}";
526 }
527 elsif ($key eq '$last') {
528 0 $perl .= "if (is_array_ref(${var})) {";
529 0 $perl .= "${var}->[\@{${var}} - 1] = $val;";
530 0 $perl .= "}";
531 }
532 elsif ($key eq '$prepend') {
533 0 $perl .= "if (is_array_ref(${var})) {";
534 0 $perl .= "unshift(\@{${var}}, $val);";
535 0 $perl .= "}";
536 }
537 elsif ($key eq '$append') {
538 0 $perl .= "if (is_array_ref(${var})) {";
539 0 $perl .= "push(\@{${var}}, $val);";
540 0 $perl .= "}";
541 }
542 elsif ($key eq '*') {
543 0 my $i = $self->_generate_var;
544 0 $perl .= "if (is_array_ref(${var})) {";
545 0 $perl .= "for (my ${i} = 0; ${i} < \@{${var}}; ${i}++) {";
546 0 $perl .= "${var}->[${i}] = $val;";
547 0 $perl .= "}}";
548 }
549 else {
550 0 $perl .= "if (is_hash_ref(${var})) {";
551 0 $perl .= "${var}->{${str_key}} = $val;";
552 0 $perl .= "}";
553 }
554  
555 0 $perl;
556 }
557  
558 0 ▶sub emit_delete_key {
Subroutine emit_delete_key: not called CC=18 SCAR=58.3
Pod undocumented
559 0 my ($self, $var, $key, $cb) = @_;
560  
561 0 my $str_key = $self->emit_string($key);
562 0 my $perl = "";
563 0 my $vals;
564 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
565 0 $vals = $self->_generate_var;
566 0 $perl = $self->emit_declare_vars($vals, '[]');
567 }
568  
569 0 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 0 0 0
elsif ($key eq '$first' or $key eq '$last' or $key eq '*') { } 0 0 0
570 0 $perl .= "if (is_hash_ref(${var}) && exists(${var}->{${str_key}})) {";
571 0 ▶ $perl .= "push(\@{${vals}}, " if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
572 0 $perl .= "delete(${var}->{${str_key}})";
573 0 ▶ $perl .= ")" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
574 0 $perl .= ";";
575 0 $perl .= "} elsif (is_array_ref(${var}) && \@{${var}} > ${key}) {";
576 0 ▶ $perl .= "push(\@{${vals}}, " if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
577 0 $perl .= "splice(\@{${var}}, ${key}, 1)";
578 0 ▶ $perl .= ")" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
579 }
580 ▶ elsif ($key eq '$first' || $key eq '$last' || $key eq '*') {
Condition0%0/6
$key eq "\$first" or $key eq "\$last" or $key eq "*"
l!l&&r!l&&!r
000
$key eq "\$first" or $key eq "\$last"
l!l&&r!l&&!r
000
MC/DC0%0/3
$key eq "\$first" or $key eq "\$last" or $key eq "*"
$key eq "\$first" $key eq "\$last" $key eq "*"
Truth table0%0/4
A or B or C
A= $key eq "\$first"B= $key eq "\$last"C= $key eq "*"
ABCresult
1XX1
01X1
0011
0000
581 0 $perl .= "if (is_array_ref(${var}) && \@{${var}}) {";
582 0 ▶ $perl .= "push(\@{${vals}}, " if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
583 0 ▶ $perl .= "splice(\@{${var}}, 0, 1)" if $key eq '$first';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "\$first" 0 0 0
584 0 ▶ $perl .= "splice(\@{${var}}, \@{${var}} - 1, 1)" if $key eq '$last';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "\$last" 0 0 0
585 0 ▶ $perl .= "splice(\@{${var}}, 0, \@{${var}})" if $key eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "*" 0 0 0
586 0 ▶ $perl .= ")" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
587 }
588 else {
589 0 $perl .= "if (is_hash_ref(${var}) && exists(${var}->{${str_key}})) {";
590 0 ▶ $perl .= "push(\@{${vals}}, " if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
591 0 $perl .= "delete(${var}->{${str_key}})";
592 0 ▶ $perl .= ")" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
593 }
594 0 $perl .= ";";
595 0 $perl .= "}";
596 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
597 0 $perl .= $cb->($vals);
598 }
599  
600 0 $perl;
601 }
602  
603 8 ▶sub emit_retain_key {
Subroutine emit_retain_key: called CC=6 SCAR=17.9
Pod undocumented
604 8 my ($self, $var, $key) = @_;
605  
606 8 my $perl = "";
607  
608 8 ▶ if ($key =~ /^[0-9]+$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /^[0-9]+$/) { } 2 6 8
elsif ($key eq '$first') { } 1 5 6
elsif ($key eq '$last') { } 1 4 5
elsif ($key eq '*') { } 2 2 4
609 2 $perl .= "if (is_hash_ref(${var})) {";
610 $perl .= $self->emit_foreach_key(
611 $var,
612 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
613 2 my $v = shift;
614 2 "delete(${var}->{${v}}) if ${v} ne ${key};";
615 }
616 2 );
617 2 $perl .= "} elsif (is_array_ref(${var})) {";
618 2 $perl .= "if (\@{${var}} > ${key}) {";
619 2 ▶ $perl .= "splice(\@{${var}}, 0, ${key});" if $key > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $key > 0 2 0 2
620 2 $perl .= "splice(\@{${var}}, 1, \@{${var}} - 1);";
621 2 $perl .= "} else {";
622 2 $perl .= "splice(\@{${var}}, 0, \@{${var}});";
623 2 $perl .= "}";
624 2 $perl .= "}";
625 }
626 elsif ($key eq '$first') {
627 1 $perl .= "if (is_array_ref(${var}) && \@{${var}} > 1) {";
628 1 $perl .= "splice(\@{${var}}, 1, \@{${var}} - 1);";
629 1 $perl .= "}";
630 }
631 elsif ($key eq '$last') {
632 1 $perl .= "if (is_array_ref(${var}) && \@{${var}} > 1) {";
633 1 $perl .= "splice(\@{${var}}, 0, \@{${var}} - 1);";
634 1 $perl .= "}";
635 }
636 elsif ($key eq '*') {
637  
638 # retain everything
639 }
640 else {
641 2 $key = $self->emit_string($key);
642 2 $perl .= "if (is_hash_ref(${var})) {";
643 $perl .= $self->emit_foreach_key(
644 $var,
645 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
646 2 my $v = shift;
647 2 "if ($v ne ${key}) {" . "delete(${var}->{${v}});" . "}";
648 }
649 2 );
650 2 $perl .= "}";
651 }
652  
653 8 $perl;
654 }
655  
656 0 ▶sub emit_clone {
Subroutine emit_clone: not called CC=1 SCAR=6.9
Pod undocumented
657 0 my ($self, $var) = @_;
658 0 "$var = clone($var);";
659 }
660  
661 # Split a path on '.' or '/', but not on '\.' or '\/'.
662 8 ▶sub split_path {
Subroutine split_path: called CC=1 SCAR=0.0
Pod undocumented
663 8 my ($self, $path) = @_;
664 8 Catmandu::Util::split_path($path);
665 }
666  
667 154 1;
668  
669 __END__
↑ Summary Catmandu/Cmd/export.pm »