blib/lib/App/NDTools/NDProc/Module/Merge.pm

filter: Statementstmt 94.4% 152 / 161 Branchbran 90.0% 45 / 50 Conditioncond 63.3% 19 / 30 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 86.2% 25 / 29 Podpod 0.0% 0 / 10 totaltotal 83.2% 249 / 299 CC 38 SCAR 17.8
CC
38
Cov
90%
map_paths
13.3
process_path
9.3
configure
7.0
CRAP 5.9
« blib/lib/App/NDTools/NDQuery.pm ↑ Summary blib/lib/App/NDTools/NDProc.pm »
1 package App::NDTools::NDProc::Module::Merge;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use parent 'App::NDTools::NDProc::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Hash::Merge qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Hash::Merge::Extra 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::MoreUtils qw(before);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Log::Log4Cli;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Struct::Path 0.80 qw(implicit_step path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Struct::Path::PerlStyle 0.80 qw(str2path path2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = '0.17';
16  
17 1 ▶sub MODINFO { "Merge structures according provided rules" }
Subroutine MODINFO: called CC=1 SCAR=0.0
Pod undocumented
18  
19 sub arg_opts {
20 20 ▶ my $self = shift;
Subroutine arg_opts: called CC=1 SCAR=0.0
Pod undocumented
21  
22 20 my %opts = $self->SUPER::arg_opts();
23 20 delete $opts{'path=s@'};
24  
25 return (
26 %opts,
27 'ignore=s@' => \$self->{OPTS}->{ignore},
28 'merge|path=s' => sub {
29 20 ▶ if ($self->{rules} and @{$self->{rules}}) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rules'} and @{$self->{'rules'};}) { } 19 1 20
Condition66%2/3
$self->{"rules"} and @{$self->{"rules"};}
!ll&&!rl&&r
0119
MC/DC50%1/2
$self->{"rules"} and @{$self->{"rules"};}
$self->{"rules"} @{$self->{"rules"};}
Truth table66%2/3
A and B
A= $self->{"rules"}B= @{$self->{"rules"};}
ABresult
0X0
100
111
30 19 push @{$self->{rules}->[-1]->{path}}, { merge => $_[1] };
31 } else {
32 1 push @{$self->{OPTS}->{path}}, { merge => $_[1] };
33 }
34 },
35 'source=s' => sub {
36 21 ▶ push @{$self->{rules}}, { source => $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
37 },
38 'strict!' => sub {
39 5 ▶ $self->set_path_related_opt($_[0], $_[1]),
Subroutine __ANON__: called CC=1 SCAR=0.0
40 },
41 'style=s' => sub {
42 5 ▶ $self->set_path_related_opt($_[0], $_[1])
Subroutine __ANON__: called CC=1 SCAR=0.0
43 },
44 )
45 20 }
46  
47 sub configure {
48 65 ▶ my $self = shift;
Subroutine configure: called CC=7 SCAR=19.5
Pod undocumented
49  
50 65 ▶ $self->{rules} = [] unless ($self->{rules});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rules"} 45 20 65
51  
52 # resolve rules
53 65 for my $rule (@{$self->{rules}}) {
54  
55 # merge with global wide opts
56 21 my $globals = dclone($self->{OPTS});
57 2 unshift @{$rule->{path}}, @{delete $globals->{path}}
58 21 ▶ if ($globals->{path} and @{$globals->{path}});
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{"path"} and @{$globals->{"path"};} 2 19 21
Condition66%2/3
$globals->{"path"} and @{$globals->{"path"};}
!ll&&!rl&&r
0192
MC/DC50%1/2
$globals->{"path"} and @{$globals->{"path"};}
$globals->{"path"} @{$globals->{"path"};}
Truth table66%2/3
A and B
A= $globals->{"path"}B= @{$globals->{"path"};}
ABresult
0X0
100
111
59 21 $rule = { %{$globals}, %{$rule} };
60  
61 # path as simple string if no no specific opts defined
62 21 ▶ map { $_ = $_->{merge} if (exists $_->{merge} and keys %{$_} == 1) }
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"merge"} and keys %{$_;} == 1 16 5 21
Condition66%2/3
exists $_->{"merge"} and keys %{$_;} == 1
!ll&&!rl&&r
0516
MC/DC50%1/2
exists $_->{"merge"} and keys %{$_;} == 1
exists $_->{"merge"} keys %{$_;} == 1
Truth table66%2/3
A and B
A= exists $_->{"merge"}B= keys %{$_;} == 1
ABresult
0X0
100
111
63 21 @{$rule->{path}};
64 }
65 }
66  
67 sub defaults {
68 45 ▶ my $self = shift;
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
69  
70 return {
71 45 %{$self->SUPER::defaults()},
72 'strict' => 1,
73 'style' => 'R_OVERRIDE',
74 };
75 }
76  
77 sub get_opts {
78 20 ▶ return @{$_[0]->{rules}};
Subroutine get_opts: called CC=1 SCAR=0.0
Pod undocumented
79 }
80  
81 sub map_paths {
82 23 ▶ my ($data, $srcs, $spath) = @_;
Subroutine map_paths: called CC=13 SCAR=25.8
Pod undocumented
83  
84 23 ▶ my @explicit = before { implicit_step($_) } @{$spath};
Subroutine __ANON__: called CC=1 SCAR=0.0
85 16 return path(${$data}, $spath, paths => 1, expand => 1)
86 23 ▶ if (@explicit == @{$spath}); # fully qualified path
Branch: 1 branch
BranchTrueFalseTotal
if @explicit == @{$spath;} 16 7 23
87  
88 7 my @out;
89 7 my @dsts = path(${$data}, $spath, paths => 1);
90  
91 7 $srcs = [ @{$srcs} ];
92 7 ▶ while (@{$srcs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$srcs;}) 7 7 14
93 7 my ($sp, $sr) = splice @{$srcs}, 0, 2;
94  
95 7 ▶ if (@dsts) { # destination struct may match - use this paths beforehand
Branch: 1 branch
BranchTrueFalseTotal
if (@dsts) 3 4 7
96 3 push @out, splice @dsts, 0, 2;
97 3 next;
98 }
99  
100 4 my @e_path = @{$spath};
101 4 ▶ while (my $step = pop @e_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = pop @e_path) 18 0 18
102 18 ▶ if (ref $step eq 'ARRAY' and implicit_step($step)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $step eq 'ARRAY' and implicit_step($step)) { } 5 13 18
elsif (ref $step eq 'HASH' and implicit_step($step)) { } 1 20 21
Condition66%2/3
ref $step eq "ARRAY" and implicit_step($step)
!ll&&!rl&&r
1305
MC/DC50%1/2
ref $step eq "ARRAY" and implicit_step($step)
ref $step eq "ARRAY" implicit_step($step)
Truth table66%2/3
A and B
A= ref $step eq "ARRAY"B= implicit_step($step)
ABresult
0X0
100
111
103 5 ▶ if (my @tmp = path(${$data}, \@e_path, deref => 1, paths => 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@tmp) = &path(${$data;}, \@e_path, "deref", 1, "paths", 1)) 4 1 5
104 # expand last existed array, addressed by implicit step
105 4 @e_path = ( @{$tmp[0]}, [ scalar @{$tmp[1]} ] );
106 4 last;
107 }
108 ▶ } elsif (ref $step eq 'HASH' and implicit_step($step)) {
Condition100%3/3
ref $step eq "HASH" and implicit_step($step)
!ll&&!rl&&r
841
MC/DC100%2/2
ref $step eq "HASH" and implicit_step($step)
ref $step eq "HASH" implicit_step($step)
Truth table100%3/3
A and B
A= ref $step eq "HASH"B= implicit_step($step)
ABresult
0X0
100
111
109 1 ▶ if (my @tmp = path(${$data}, [ @e_path, $step ], paths => 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@tmp) = &path(${$data;}, [@e_path, $step], "paths", 1)) 0 1 1
110 0 @e_path = @{$tmp[0]};
111 0 last;
112 }
113 }
114 }
115  
116 4 ▶ @e_path = @{$sp}[0 .. $#explicit] unless (@e_path);
Branch: 1 branch
BranchTrueFalseTotal
unless @e_path 0 4 4
117 4 my @i_path = @{$sp}[@e_path .. $#{$sp}];
118  
119 4 ▶ map { $_ = [0] if (ref $_ eq 'ARRAY') } @i_path; # drop array's indexes in implicit part of path
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "ARRAY" 1 1 2
120 4 push @out, path(${$data}, [@e_path, @i_path], paths => 1, expand => 1);
121 }
122  
123 7 return @out;
124 }
125  
126 sub check_rule {
127 23 ▶ my ($self, $rule) = @_;
Subroutine check_rule: called CC=4 SCAR=13.9
Pod undocumented
128  
129 # merge full source if no paths defined
130 8 push @{$rule->{path}}, {}
131 23 ▶ unless ($rule->{path} and @{$rule->{path}});
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"path"} and @{$rule->{"path"};} 8 15 23
Condition66%2/3
$rule->{"path"} and @{$rule->{"path"};}
!ll&&!rl&&r
0815
MC/DC50%1/2
$rule->{"path"} and @{$rule->{"path"};}
$rule->{"path"} @{$rule->{"path"};}
Truth table66%2/3
A and B
A= $rule->{"path"}B= @{$rule->{"path"};}
ABresult
0X0
100
111
132 # convert to canonical structure
133 26 ▶ map { $_ = { merge => $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 16 10 26
134 23 unless (ref $_) } @{$rule->{path}};
135  
136 23 return $self;
137 }
138  
139 sub process {
140 23 ▶ my ($self, $data, $opts, $source) = @_;
Subroutine process: called CC=3 SCAR=11.0
Pod undocumented
141  
142 23 ▶ if (exists $opts->{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"ignore"}) 20 3 23
143 20 for my $path (@{$opts->{ignore}}) {
144 1 ▶ log_debug { "Removing (ignore) from src '$path'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
145 1 path($source, str2path($path), delete => 1);
146 }
147 }
148  
149 23 $self->SUPER::process($data, $opts, $source);
150 }
151  
152 sub process_path {
153 26 ▶ my ($self, $data, $path, $opts, $source) = @_;
Subroutine process_path: called CC=9 SCAR=22.4
Pod undocumented
154  
155 # merge whole source if path omitted
156 26 ▶ $path->{merge} = '' unless (defined $path->{merge});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path->{"merge"} 8 18 26
157  
158 26 my $spath = eval { str2path($path->{merge}) };
159 26 ▶ die_fatal "Failed to parse path ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 26 26
160  
161 26 ▶ log_debug { "Resolving paths '$path->{merge}'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
162 26 my @srcs = path($source, $spath, paths => 1);
163 26 ▶ unless (@srcs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@srcs) 3 23 26
164 die_fatal "No such path '$path->{merge}' in $opts->{source}", 4
165 3 ▶ if (exists $path->{strict} ? $path->{strict} : $opts->{strict});
Branch: 2 branches
BranchTrueFalseTotal
if exists $path->{"strict"} ? $path->{"strict"} : $opts->{"strict"} 2 1 3
exists $path->{'strict'} ? : 1 2 3
166 1 ▶ log_info { "Ignore path '$path->{merge}' (absent in $opts->{source})" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
167 1 return $self;
168 }
169 23 my @dsts = map_paths($data, \@srcs, $spath);
170  
171 23 ▶ my $style = $path->{style} || $opts->{style} || $self->{OPTS}->{style};
Condition50%3/6
$path->{'style'} || $opts->{'style'} || $self->{'OPTS'}{'style'}
l!l&&r!l&&!r
2300
$path->{"style"} or $opts->{"style"}
l!l&&r!l&&!r
1220
MC/DC0%0/3
$path->{'style'} || $opts->{'style'} || $self->{'OPTS'}{'style'}
$path->{"style"} $opts->{"style"} $self->{'OPTS'}{'style'}
Truth table50%2/4
A or B || C
A= $path->{"style"}B= $opts->{"style"}C= $self->{'OPTS'}{'style'}
ABCresult
1XX1
01X1
0011
0000
172 23 ▶ while (@srcs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@srcs) 23 23 46
173 23 my ($sp, $sr) = splice @srcs, 0, 2;
174 23 my ($dp, $dr) = splice @dsts, 0, 2;
175 0 ▶ log_info { "Merging $opts->{source} ($style, '" .
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 23 path2str($sp) . "' => '" . path2str($dp) . "')" };
177 23 Hash::Merge::set_behavior($style);
178 23 ${$dr} = Hash::Merge::merge(${$dr}, ${$sr});
179 }
180 }
181  
182 sub set_path_related_opt {
183 10 ▶ my ($self, $name, $val) = @_;
Subroutine set_path_related_opt: called CC=5 SCAR=17.1
Pod undocumented
184  
185 10 ▶ if ($self->{rules} and @{$self->{rules}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rules'} and @{$self->{'rules'};}) { } 10 0 10
Condition33%1/3
$self->{"rules"} and @{$self->{"rules"};}
!ll&&!rl&&r
0010
MC/DC0%0/2
$self->{"rules"} and @{$self->{"rules"};}
$self->{"rules"} @{$self->{"rules"};}
Truth table33%1/3
A and B
A= $self->{"rules"}B= @{$self->{"rules"};}
ABresult
0X0
100
111
186 10 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'rules'}[-1]{'path'} and @{$self->{'rules'}[-1]{'path'};}) { } 5 5 10
Condition66%2/3
exists $self->{"rules"}[-1]{"path"} and @{$self->{"rules"}[-1]{"path"};}
!ll&&!rl&&r
505
MC/DC50%1/2
exists $self->{"rules"}[-1]{"path"} and @{$self->{"rules"}[-1]{"path"};}
exists $self->{"rules"}[-1]{"path"} @{$self->{"rules"}[-1]{"path"};}
Truth table66%2/3
A and B
A= exists $self->{"rules"}[-1]{"path"}B= @{$self->{"rules"}[-1]{"path"};}
ABresult
0X0
100
111
187 exists $self->{rules}->[-1]->{path} and
188 5 @{$self->{rules}->[-1]->{path}}
189 ) {
190 5 $self->{rules}->[-1]->{path}->[-1]->{$name} = $val; # per path
191 } else {
192 5 $self->{rules}->[-1]->{$name} = $val; # per rule
193 }
194 } else {
195 0 $self->{OPTS}->{$name} = $val; # global (whole ruleset wide)
196 }
197 }
198  
199 4 1; # End of App::NDTools::NDProc::Module::Merge
200  
201 __END__
« blib/lib/App/NDTools/NDQuery.pm ↑ Summary blib/lib/App/NDTools/NDProc.pm »