blib/lib/Config/Model/Iterator.pm

filter: Statementstmt 95.9% 118 / 123 Branchbran 78.8% 41 / 52 Conditioncond 68.4% 26 / 38 MC/DCmcdc 39.1% 9 / 23 Subroutinesub 100% 22 / 22 Podpod 66.6% 6 / 9 totaltotal 83.1% 222 / 267 CC 44 SCAR 22.9
CC
44
Cov
87%
hash_element_cb
15.4
leaf_cb
15.1
new
13.6
CRAP 9.9
« blib/lib/Config/Model/BackendMgr.pm ↑ Summary blib/lib/Config/Model/Instance.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Iterator 2.169;
11  
12 59 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 ▶use Config::Model::ObjTreeScanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 59 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 59 my $logger = get_logger("Iterator");
25  
26 1 ▶sub new ($type, %args){
Subroutine new: called CC=12 SCAR=26.1
Pod undocumented
27  
28 1 my $self = {
29 call_back_on_important => 0,
30 forward => 1,
31 status => 'standard',
32 };
33  
34 1 foreach my $p (qw/root/) {
35 1 ▶ $self->{$p} = delete $args{$p}
Branch: 1 branch
BranchTrueFalseTotal
$self->{$p} = delete $args{$p} or croak("Iterator->new: Missing $p parameter") 1 0 1
36 or croak "Iterator->new: Missing $p parameter";
37 }
38  
39 1 foreach my $p (qw/call_back_on_important call_back_on_warning status/) {
40 3 ▶ $self->{$p} = delete $args{$p} if defined $args{$p};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$p} 2 1 3
41 }
42  
43 1 bless $self, $type;
44  
45 1 my %cb_hash;
46  
47 # mandatory call-back parameters
48 1 foreach my $item (qw/leaf_cb hash_element_cb/) {
49 2 ▶ $cb_hash{$item} = delete $args{$item}
Branch: 1 branch
BranchTrueFalseTotal
$cb_hash{$item} = delete $args{$item} or croak("Iterator->new: Missing $item parameter") 2 0 2
50 or croak "Iterator->new: Missing $item parameter";
51 }
52  
53 # handle optional list_element_cb parameter
54 $cb_hash{list_element_cb} = delete $args{list_element_cb}
55 1 ▶ || $cb_hash{hash_element_cb};
Condition33%1/3
delete $args{'list_element_cb'} || $cb_hash{'hash_element_cb'}
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $args{'list_element_cb'} || $cb_hash{'hash_element_cb'}
delete $args{'list_element_cb'} $cb_hash{'hash_element_cb'}
Truth table33%1/3
A || B
A= delete $args{'list_element_cb'}B= $cb_hash{'hash_element_cb'}
ABresult
1X1
011
000
56  
57 # optional call-back parameter
58 $cb_hash{check_list_element_cb} =
59 1 ▶ delete $args{check_list_element_cb} || $cb_hash{leaf_cb};
Condition33%1/3
delete $args{'check_list_element_cb'} || $cb_hash{'leaf_cb'}
l!l&&r!l&&!r
010
MC/DC0%0/2
delete $args{'check_list_element_cb'} || $cb_hash{'leaf_cb'}
delete $args{'check_list_element_cb'} $cb_hash{'leaf_cb'}
Truth table33%1/3
A || B
A= delete $args{'check_list_element_cb'}B= $cb_hash{'leaf_cb'}
ABresult
1X1
011
000
60  
61 # optional call-back parameters
62 1 foreach my $p (
63 qw/enum_value reference_value
64 integer_value number_value
65 boolean_value string_value uniline_value/
66 ) {
67 7 my $item = $p . '_cb';
68 7 ▶ $cb_hash{$item} = delete $args{$item} || $cb_hash{leaf_cb};
Condition66%2/3
delete $args{$item} || $cb_hash{'leaf_cb'}
l!l&&r!l&&!r
160
MC/DC0%0/2
delete $args{$item} || $cb_hash{'leaf_cb'}
delete $args{$item} $cb_hash{'leaf_cb'}
Truth table66%2/3
A || B
A= delete $args{$item}B= $cb_hash{'leaf_cb'}
ABresult
1X1
011
000
69 }
70  
71 1 $self->{dispatch_cb} = \%cb_hash;
72  
73 1 ▶ if (%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 0 1 1
74 0 die "Iterator->new: unexpected parameters: ", join( ' ', keys %args ), "\n";
75 }
76  
77 # user call-back are *not* passed to ObjTreeScanner. They will be
78 # called indirectly through wizard-helper own call-backs
79  
80 $self->{scanner} = Config::Model::ObjTreeScanner->new(
81 15 ▶ hash_element_cb => sub { $self->hash_element_cb(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 5 ▶ list_element_cb => sub { $self->hash_element_cb(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 36 ▶ node_content_cb => sub { $self->node_content_cb(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 175 ▶ leaf_cb => sub { $self->leaf_cb(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 );
86  
87 1 return $self;
88 }
89  
90 1 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
91 1 my $self = shift;
92 1 $self->{bail_out} = 0;
93 1 $self->{scanner}->scan_node( undef, $self->{root} );
94 1 return;
95 }
96  
97 1 ▶sub bail_out {
Subroutine bail_out: called CC=1 SCAR=0.0
Pod documented
98 1 my $self = shift;
99 1 $self->{bail_out} = 1;
100 1 return;
101 }
102  
103 # internal. This call-back is passed to ObjTreeScanner. It will call
104 # scan_element in an order which depends on $self->{forward}.
105 36 ▶sub node_content_cb {
Subroutine node_content_cb: called CC=3 SCAR=11.0
Pod undocumented
106 36 my ( $self, $scanner, $data_r, $node, @element ) = @_;
107  
108 36 $logger->info( "node_content_cb called on '", $node->name, "' element: @element" );
109  
110 36 my $element;
111  
112 36 while (1) {
113  
114 # @element from ObjTreeScanner is not used as user actions may
115 # change the element list due to warping
116 $element = $node->next_element(
117 name => $element,
118 status => $self->{status},
119 232 reverse => 1 - $self->{forward} );
120  
121 232 ▶ last unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 35 197 232
122  
123 197 $logger->info( "node_content_cb calls scan_element ", "on element $element" );
124  
125 197 $self->{scanner}->scan_element( $data_r, $node, $element );
126 197 ▶ return if $self->{bail_out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bail_out"} 1 196 197
127 }
128 35 return;
129 }
130  
131 # internal. Used to find which user call-back to use for a given
132 # element type.
133 195 ▶sub get_cb {
Subroutine get_cb: called CC=2 SCAR=7.1
Pod undocumented
134 195 my $self = shift;
135 195 my $elt_type = shift;
136 195 ▶ return $self->{dispatch_cb}{ $elt_type . '_cb' }
Condition50%1/2
$self->{'dispatch_cb'}{$elt_type . '_cb'} || croak("wizard get_cb: unexpected type $elt_type")
l!l
1950
MC/DC0%0/1
$self->{'dispatch_cb'}{$elt_type . '_cb'} || croak("wizard get_cb: unexpected type $elt_type")
$self->{'dispatch_cb'}{$elt_type . '_cb'}
Truth table50%1/2
A
A= $self->{'dispatch_cb'}{$elt_type . '_cb'}
Aresult
11
00
137 || croak "wizard get_cb: unexpected type $elt_type";
138 }
139  
140 # internal. This call-back is passed to ObjTreeScanner. It will call
141 # scan_hash in an order which depends on $self->{forward}. it will
142 # also check if the hash (or list) element is flagged as 'important'
143 # and call user's hash or list call-back if needed
144 20 ▶sub hash_element_cb ( $self, $scanner, $data_r, $node, $element, @raw_keys ) {
Subroutine hash_element_cb: called CC=15 SCAR=27.4
Pod documented
145 20 my @keys = sort @raw_keys;
146  
147 20 my $level = $node->get_element_property( element => $element, property => 'level' );
148  
149 20 $logger->info( "hash_element_cb (element $element) called on '",
150 $node->location, "' level $level, keys: '@keys'" );
151  
152 # get the call-back to use
153 20 my $cb = $self->get_cb( $node->element_type($element) . '_element' );
154  
155 # use the same algorithm for check_important and
156 # scan_element pseudo elements
157 20 ▶ my $i = $self->{forward} == 1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'forward'} == 1 ? : 14 6 20
158  
159 20 ▶ while ( $i >= 0 and $i < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and $i < 2) 41 20 61
Condition100%3/3
$i >= 0 and $i < 2
!ll&&!rl&&r
51541
MC/DC100%2/2
$i >= 0 and $i < 2
$i >= 0 $i < 2
Truth table100%3/3
A and B
A= $i >= 0B= $i < 2
ABresult
0X0
100
111
160 41 ▶ if ( $self->{call_back_on_important} and $i == 0 and $level eq 'important' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"call_back_on_important"} and $i == 0 and $level eq "important") 3 38 41
Condition83%5/6
$self->{"call_back_on_important"} and $i == 0 and $level eq "important"
!ll&&!rl&&r
21173
$self->{"call_back_on_important"} and $i == 0
!ll&&!rl&&r
02120
MC/DC66%2/3
$self->{"call_back_on_important"} and $i == 0 and $level eq "important"
$self->{"call_back_on_important"} $i == 0 $level eq "important"
Truth table75%3/4
A and B and C
A= $self->{"call_back_on_important"}B= $i == 0C= $level eq "important"
ABCresult
0XX0
10X0
1100
1111
161 3 $cb->( $self, $data_r, $node, $element, @keys );
162 3 ▶ return if $self->{bail_out}; # may be modified in callback
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bail_out"} 0 3 3
163 # recompute keys as they may have been modified during call-back
164 3 @keys = $self->{scanner}->get_keys( $node, $element );
165 }
166  
167 41 ▶ if ( $self->{call_back_on_warning} and $i == 0 and $node->fetch_element($element)->has_warning ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"call_back_on_warning"} and $i == 0 and $node->fetch_element($element)->has_warning) 0 41 41
Condition66%4/6
$self->{"call_back_on_warning"} and $i == 0 and $node->fetch_element($element)->has_warning
!ll&&!rl&&r
21200
$self->{"call_back_on_warning"} and $i == 0
!ll&&!rl&&r
02120
MC/DC0%0/3
$self->{"call_back_on_warning"} and $i == 0 and $node->fetch_element($element)->has_warning
$self->{"call_back_on_warning"} $i == 0 $node->fetch_element($element)->has_warning
Truth table50%2/4
A and B and C
A= $self->{"call_back_on_warning"}B= $i == 0C= $node->fetch_element($element)->has_warning
ABCresult
0XX0
10X0
1100
1111
168 0 $logger->info("hash_element_cb found elt with warning: '", $node->name, "' element $element");
169 0 $cb->( $self, $data_r, $node, $element, @keys );
170 }
171  
172 41 ▶ if ( $i == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 21 20 41
173 21 ▶ my $j = $self->{forward} == 1 ? 0 : $#keys;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'forward'} == 1 ? : 15 6 21
174 21 ▶ while ( $j >= 0 and $j < @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0 and $j < @keys) 33 21 54
Condition100%3/3
$j >= 0 and $j < @keys
!ll&&!rl&&r
61533
MC/DC100%2/2
$j >= 0 and $j < @keys
$j >= 0 $j < @keys
Truth table100%3/3
A and B
A= $j >= 0B= $j < @keys
ABresult
0X0
100
111
175 33 my $k = $keys[$j];
176 33 $logger->info( "hash_element_cb (element $element) calls ", "scan_hash on key $k" );
177 33 $self->{scanner}->scan_hash( $data_r, $node, $element, $k );
178 33 $j += $self->{forward};
179 }
180 }
181 41 $i += $self->{forward};
182 }
183 20 return;
184 }
185  
186 # internal. This call-back is passed to ObjTreeScanner. It will also
187 # check if the leaf element is flagged as 'important' or if the leaf
188 # element contains an error (mostly undefined mandatory values) and
189 # call user's call-back if needed
190  
191 175 ▶sub leaf_cb {
Subroutine leaf_cb: called CC=14 SCAR=27.1
Pod documented
192 175 my ( $self, $scanner, $data_r, $node, $element, $index, $value_obj ) = @_;
193  
194 175 ▶ $logger->info(
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 22 153 175
195 "leaf_cb called on '",
196 $node->name,
197 "' element '$element'",
198 defined $index ? ", index $index" : ''
199 );
200  
201 175 my $elt_type = $node->element_type($element);
202 175 ▶ my $key =
Branch: 1 branch
BranchTrueFalseTotal
$elt_type eq 'check_list' ? : 2 173 175
203 $elt_type eq 'check_list'
204 ? 'check_list_element'
205 : $value_obj->value_type . '_value';
206  
207 175 my $user_leaf_cb = $self->get_cb($key);
208  
209 175 my $level = $node->get_element_property( element => $element, property => 'level' );
210  
211 175 ▶ if ( $self->{call_back_on_important} and $level eq 'important' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"call_back_on_important"} and $level eq "important") 19 156 175
Condition66%2/3
$self->{"call_back_on_important"} and $level eq "important"
!ll&&!rl&&r
015619
MC/DC50%1/2
$self->{"call_back_on_important"} and $level eq "important"
$self->{"call_back_on_important"} $level eq "important"
Truth table66%2/3
A and B
A= $self->{"call_back_on_important"}B= $level eq "important"
ABresult
0X0
100
111
212 19 ▶ $logger->info(
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 13 6 19
213 "leaf_cb found important elt: '",
214 $node->name,
215 "' element $element",
216 defined $index ? ", index $index" : ''
217 );
218 19 $user_leaf_cb->( $self, $data_r, $node, $element, $index, $value_obj );
219 }
220  
221 175 ▶ if ( $self->{call_back_on_warning} and $value_obj->warning_msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"call_back_on_warning"} and $value_obj->warning_msg) 1 174 175
Condition66%2/3
$self->{"call_back_on_warning"} and $value_obj->warning_msg
!ll&&!rl&&r
01741
MC/DC50%1/2
$self->{"call_back_on_warning"} and $value_obj->warning_msg
$self->{"call_back_on_warning"} $value_obj->warning_msg
Truth table66%2/3
A and B
A= $self->{"call_back_on_warning"}B= $value_obj->warning_msg
ABresult
0X0
100
111
222 1 ▶ $logger->info(
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 1 1
223 "leaf_cb found elt with warning: '",
224 $node->name,
225 "' element $element",
226 defined $index ? ", index $index" : ''
227 );
228 1 $user_leaf_cb->( $self, $data_r, $node, $element, $index, $value_obj );
229 }
230  
231 # now need to check for errors...
232 175 my $result;
233 175 eval { $result = $value_obj->fetch(); };
234  
235 175 my $e = $@;
236 175 ▶ if ( ref $e and $e->isa('Config::Model::Exception::User') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $e and $e->isa('Config::Model::Exception::User')) { } 3 172 175
elsif (ref $e) { } 0 172 172
elsif ($e) { } 0 172 172
Condition66%2/3
ref $e and $e->isa("Config::Model::Exception::User")
!ll&&!rl&&r
17203
MC/DC50%1/2
ref $e and $e->isa("Config::Model::Exception::User")
ref $e $e->isa("Config::Model::Exception::User")
Truth table66%2/3
A and B
A= ref $eB= $e->isa("Config::Model::Exception::User")
ABresult
0X0
100
111
237  
238 # ignore errors that has just been catched and call user call-back
239 3 ▶ $logger->info(
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 3 3
240 "leaf_cb oopsed on '",
241 $node->name,
242 "' element $element",
243 defined $index ? ", index $index" : ''
244 );
245 3 $user_leaf_cb->( $self, $data_r, $node, $element, $index, $value_obj, $e->error );
246 }
247 elsif ( ref $e ) {
248 0 $e->rethrow;
249 # does not return ...
250 }
251 elsif ($e) {
252 0 die "Iterator failed on value object: $e";
253 }
254 175 return;
255 }
256  
257 2 ▶sub go_forward {
Subroutine go_forward: called CC=2 SCAR=7.0
Pod documented
258 2 my $self = shift;
259 2 ▶ $logger->info("Going forward") if $self->{forward} == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"forward"} == -1 2 0 2
260 2 $self->{forward} = 1;
261 2 return;
262 }
263  
264 3 ▶sub go_backward {
Subroutine go_backward: called CC=2 SCAR=7.0
Pod documented
265 3 my $self = shift;
266 3 ▶ $logger->info("Going backward") if $self->{forward} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"forward"} == 1 3 0 3
267 3 $self->{forward} = -1;
268 3 return;
269 }
270  
271 59 1;
272  
273 # ABSTRACT: Iterates forward or backward a configuration tree
274  
275 __END__
« blib/lib/Config/Model/BackendMgr.pm ↑ Summary blib/lib/Config/Model/Instance.pm »