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

filter: Statementstmt 94.7% 161 / 170 Branchbran 90.7% 49 / 54 Conditioncond 63.3% 19 / 30 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 87.1% 27 / 31 Podpod 0.0% 0 / 10 totaltotal 84.0% 264 / 314 CC 40 SCAR 18.1
CC
40
Cov
90%
map_paths
13.3
process_path
10.3
configure
8.0
CRAP 6.1
« blib/lib/App/NDTools/NDProc.pm ↑ Summary blib/lib/App/NDTools/NDQuery.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 ▶use App::NDTools::Slurp qw(s_decode s_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 our $VERSION = '0.20';
18  
19 1 ▶sub MODINFO { "Merge structures according provided rules" }
Subroutine MODINFO: called CC=1 SCAR=0.0
Pod undocumented
20  
21 24 ▶sub arg_opts {
Subroutine arg_opts: called CC=1 SCAR=0.0
Pod undocumented
22 24 my $self = shift;
23  
24 24 my %opts = $self->SUPER::arg_opts();
25 24 delete $opts{'path=s@'};
26  
27 return (
28 %opts,
29 'ignore=s@' => \$self->{OPTS}->{ignore},
30 20 ▶ 'merge|path=s' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
31 20 ▶ if ($self->{rules} and @{$self->{rules}}) {
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
32 19 push @{$self->{rules}->[-1]->{path}}, { merge => $_[1] };
33 } else {
34 1 push @{$self->{OPTS}->{path}}, { merge => $_[1] };
35 }
36 },
37 21 ▶ 'source=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 21 push @{$self->{rules}}, { source => $_[1] };
39 },
40 5 ▶ 'strict!' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 5 $self->set_path_related_opt($_[0], $_[1]),
42 },
43 1 ▶ 'structure=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 push @{$self->{rules}}, { structure => $_[1] };
45 },
46 5 ▶ 'style=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 5 $self->set_path_related_opt($_[0], $_[1])
48 },
49 )
50 24 }
51  
52 70 ▶sub configure {
Subroutine configure: called CC=8 SCAR=20.8
Pod undocumented
53 70 my $self = shift;
54  
55 70 ▶ $self->{rules} = [] unless ($self->{rules});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rules"} 49 21 70
56  
57 # resolve rules
58 70 for my $rule (@{$self->{rules}}) {
59  
60 # merge with global wide opts
61 22 my $globals = dclone($self->{OPTS});
62 2 unshift @{$rule->{path}}, @{delete $globals->{path}}
63 22 ▶ if ($globals->{path} and @{$globals->{path}});
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{"path"} and @{$globals->{"path"};} 2 20 22
Condition66%2/3
$globals->{"path"} and @{$globals->{"path"};}
!ll&&!rl&&r
0202
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
64 22 $rule = { %{$globals}, %{$rule} };
65  
66 # path as simple string if no no specific opts defined
67 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
68 22 @{$rule->{path}};
69  
70 $rule->{structure} = s_decode($rule->{structure}, 'JSON')
71 22 ▶ if (exists $rule->{structure});
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"structure"} 1 21 22
72 }
73 }
74  
75 49 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
76 49 my $self = shift;
77  
78 return {
79 49 %{$self->SUPER::defaults()},
80 'strict' => 1,
81 'style' => 'R_OVERRIDE',
82 };
83 }
84  
85 21 ▶sub get_opts {
Subroutine get_opts: called CC=1 SCAR=0.0
Pod undocumented
86 21 return @{$_[0]->{rules}};
87 }
88  
89 24 ▶sub map_paths {
Subroutine map_paths: called CC=13 SCAR=25.8
Pod undocumented
90 24 my ($data, $srcs, $spath) = @_;
91  
92 24 ▶ my @explicit = before { implicit_step($_) } @{$spath};
Subroutine __ANON__: called CC=1 SCAR=0.0
93 17 return path(${$data}, $spath, paths => 1, expand => 1)
94 24 ▶ if (@explicit == @{$spath}); # fully qualified path
Branch: 1 branch
BranchTrueFalseTotal
if @explicit == @{$spath;} 17 7 24
95  
96 7 my @out;
97 7 my @dsts = path(${$data}, $spath, paths => 1);
98  
99 7 $srcs = [ @{$srcs} ];
100 7 ▶ while (@{$srcs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$srcs;}) 7 7 14
101 7 my ($sp, $sr) = splice @{$srcs}, 0, 2;
102  
103 7 ▶ if (@dsts) { # destination struct may match - use this paths beforehand
Branch: 1 branch
BranchTrueFalseTotal
if (@dsts) 3 4 7
104 3 push @out, splice @dsts, 0, 2;
105 3 next;
106 }
107  
108 4 my @e_path = @{$spath};
109 4 ▶ while (my $step = pop @e_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = pop @e_path) 18 0 18
110 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
111 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
112 # expand last existed array, addressed by implicit step
113 4 @e_path = ( @{$tmp[0]}, [ scalar @{$tmp[1]} ] );
114 4 last;
115 }
116 ▶ } 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
117 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
118 0 @e_path = @{$tmp[0]};
119 0 last;
120 }
121 }
122 }
123  
124 0 ▶ @e_path = @{$sp}[0 .. $#explicit] unless (@e_path);
Branch: 1 branch
BranchTrueFalseTotal
unless @e_path 0 4 4
125 4 my @i_path = @{$sp}[@e_path .. $#{$sp}];
126  
127 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
128 4 push @out, path(${$data}, [@e_path, @i_path], paths => 1, expand => 1);
129 }
130  
131 7 return @out;
132 }
133  
134 24 ▶sub check_rule {
Subroutine check_rule: called CC=4 SCAR=13.9
Pod undocumented
135 24 my ($self, $rule) = @_;
136  
137 # merge full source if no paths defined
138 9 push @{$rule->{path}}, {}
139 24 ▶ unless ($rule->{path} and @{$rule->{path}});
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"path"} and @{$rule->{"path"};} 9 15 24
Condition66%2/3
$rule->{"path"} and @{$rule->{"path"};}
!ll&&!rl&&r
0915
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
140 # convert to canonical structure
141 27 ▶ map { $_ = { merge => $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 16 11 27
142 24 unless (ref $_) } @{$rule->{path}};
143  
144 24 return $self;
145 }
146  
147 24 ▶sub process {
Subroutine process: called CC=3 SCAR=11.0
Pod undocumented
148 24 my ($self, $data, $opts, $source) = @_;
149  
150 24 ▶ if (exists $opts->{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"ignore"}) 21 3 24
151 21 for my $path (@{$opts->{ignore}}) {
152 0 ▶ log_debug { "Removing (ignore) from src '$path'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
153 1 path($source, str2path($path), delete => 1);
154 }
155 }
156  
157 24 $self->SUPER::process($data, $opts, $source);
158 }
159  
160 27 ▶sub process_path {
Subroutine process_path: called CC=10 SCAR=23.3
Pod undocumented
161 27 my ($self, $data, $path, undef, $opts, $source) = @_;
162  
163 # merge whole source if path omitted
164 27 ▶ $path->{merge} = '' unless (defined $path->{merge});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path->{"merge"} 9 18 27
165  
166 27 ▶ if (exists $opts->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"structure"}) 1 26 27
167 1 $opts->{source} = s_encode($opts->{structure}, 'JSON', {pretty => 0});
168 $source = $opts->{structure}
169 1 }
170  
171 27 my $spath = eval { str2path($path->{merge}) };
172 27 ▶ die_fatal "Failed to parse path ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 27 27
173  
174 0 ▶ log_debug { "Resolving paths '$path->{merge}'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
175 27 my @srcs = path($source, $spath, paths => 1);
176 27 ▶ unless (@srcs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@srcs) 3 24 27
177 die_fatal "No such path '$path->{merge}' in $opts->{source}", 4
178 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
179 0 ▶ log_info { "Ignore path '$path->{merge}' (absent in $opts->{source})" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
180 1 return $self;
181 }
182 24 my @dsts = map_paths($data, \@srcs, $spath);
183  
184 24 ▶ my $style = $path->{style} || $opts->{style} || $self->{OPTS}->{style};
Condition50%3/6
$path->{'style'} || $opts->{'style'} || $self->{'OPTS'}{'style'}
l!l&&r!l&&!r
2400
$path->{"style"} or $opts->{"style"}
l!l&&r!l&&!r
1230
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
185 24 ▶ while (@srcs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@srcs) 24 24 48
186 24 my ($sp, $sr) = splice @srcs, 0, 2;
187 24 my ($dp, $dr) = splice @dsts, 0, 2;
188 0 ▶ log_info { "Merging $opts->{source} ($style, '" .
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 24 path2str($sp) . "' => '" . path2str($dp) . "')" };
190 24 Hash::Merge::set_behavior($style);
191 24 ${$dr} = Hash::Merge::merge(${$dr}, ${$sr});
192 }
193 }
194  
195 10 ▶sub set_path_related_opt {
Subroutine set_path_related_opt: called CC=5 SCAR=17.1
Pod undocumented
196 10 my ($self, $name, $val) = @_;
197  
198 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
199 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
200 exists $self->{rules}->[-1]->{path} and
201 5 @{$self->{rules}->[-1]->{path}}
202 ) {
203 5 $self->{rules}->[-1]->{path}->[-1]->{$name} = $val; # per path
204 } else {
205 5 $self->{rules}->[-1]->{$name} = $val; # per rule
206 }
207 } else {
208 0 $self->{OPTS}->{$name} = $val; # global (whole ruleset wide)
209 }
210 }
211  
212 4 1; # End of App::NDTools::NDProc::Module::Merge
213  
214 __END__
« blib/lib/App/NDTools/NDProc.pm ↑ Summary blib/lib/App/NDTools/NDQuery.pm »