blib/lib/Config/Model/Dumper.pm

filter: Statementstmt 97.8% 134 / 137 Branchbran 87.5% 56 / 64 Conditioncond 86.1% 31 / 36 MC/DCmcdc 70.0% 14 / 20 Subroutinesub 100% 20 / 20 Podpod 40.0% 2 / 5 totaltotal 91.1% 257 / 282 CC 51 SCAR 20.3
CC
51
Cov
93%
dump_tree
16.1
__ANON__
8.0
__ANON__
7.0
CRAP 7.6
« blib/lib/Config/Model/AnyThing.pm ↑ Summary blib/lib/Config/Model/Describe.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::Dumper 2.169;
11  
12 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 59 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 59 ▶use Config::Model::ObjTreeScanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 59 ▶use Config::Model::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 154 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 154 return bless {}, shift;
25 }
26  
27 5457 ▶sub quote (@args) {
Subroutine quote: called CC=1 SCAR=0.0
Pod undocumented
28 5457 return _quote( qr/[\s~'"#*]/, @args ); # "
29 }
30  
31 12600 ▶sub id_quote (@args) {
Subroutine id_quote: called CC=1 SCAR=0.0
Pod undocumented
32 12600 return _quote( qr/[\s~'"@*<>.=#]/, @args ); # " ))
33 }
34  
35 18057 ▶sub _quote ($re, @res) {
Subroutine _quote: called CC=6 SCAR=17.9
36 18057 foreach my $r (@res) {
37 18096 ▶ if ( defined $r and ( $r =~ /$re/ or $r eq '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r =~ /$re/ || $r eq "") 343 17753 18096
Condition100%6/6
defined $r and $r =~ /$re/ || $r eq ""
!ll&&!rl&&r
95048249343
$r =~ /$re/ or $r eq ""
l!l&&r!l&&!r
320238249
MC/DC100%3/3
defined $r and $r =~ /$re/ || $r eq ""
defined $r $r =~ /$re/ $r eq ""
Truth table100%4/4
A and B or C
A= defined $rB= $r =~ /$re/C= $r eq ""
ABCresult
0XX0
1000
11X1
1011
38 343 $r =~ s/"/\\"/g; # escape present quotes
39 343 $r = '"' . $r . '"'; # add my quotes
40 }
41 }
42 18057 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 256 17801 18057
43 }
44  
45 6810 ▶sub note_quote (@res) {
Subroutine note_quote: called CC=6 SCAR=18.1
Pod undocumented
46 6810 foreach my $r (@res) {
47 6810 ▶ if ( defined $r and $r and ($r =~ /(\s|"|\*)/) ) { # " )))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r and $r =~ /(\s|"|\*)/) 174 6636 6810
Condition83%5/6
defined $r and $r and $r =~ /(\s|"|\*)/
!ll&&!rl&&r
661224174
defined $r and $r
!ll&&!rl&&r
06612198
MC/DC66%2/3
defined $r and $r and $r =~ /(\s|"|\*)/
defined $r $r $r =~ /(\s|"|\*)/
Truth table75%3/4
A and B and C
A= defined $rB= $rC= $r =~ /(\s|"|\*)/
ABCresult
0XX0
10X0
1100
1111
48 174 $r =~ s/"/\\"/g; # escape present quotes
49 174 $r = '"' . $r . '"'; # add my quotes
50 }
51 }
52 6810 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6810 6810
53 }
54  
55 154 ▶sub dump_tree ($self, %args) {
Subroutine dump_tree: called CC=16 SCAR=27.8
Pod documented
56 154 ▶ my $full = delete $args{full_dump} || 0;
Condition50%1/2
delete $args{'full_dump'} || 0
l!l
0154
MC/DC0%0/1
delete $args{'full_dump'} || 0
delete $args{'full_dump'}
Truth table50%1/2
A
A= delete $args{'full_dump'}
Aresult
11
00
57 154 ▶ my $skip_aw = delete $args{skip_auto_write} || '';
Condition100%2/2
delete $args{'skip_auto_write'} || ''
l!l
2152
MC/DC100%1/1
delete $args{'skip_auto_write'} || ''
delete $args{'skip_auto_write'}
Truth table100%2/2
A
A= delete $args{'skip_auto_write'}
Aresult
11
00
58 154 ▶ my $auto_v = delete $args{auto_vivify} || 0;
Condition100%2/2
delete $args{'auto_vivify'} || 0
l!l
1153
MC/DC100%1/1
delete $args{'auto_vivify'} || 0
delete $args{'auto_vivify'}
Truth table100%2/2
A
A= delete $args{'auto_vivify'}
Aresult
11
00
59 154 ▶ my $mode = delete $args{mode} || '';
Condition100%2/2
delete $args{'mode'} || ''
l!l
5896
MC/DC100%1/1
delete $args{'mode'} || ''
delete $args{'mode'}
Truth table100%2/2
A
A= delete $args{'mode'}
Aresult
11
00
60  
61 154 ▶ if ($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 0 154 154
62 0 carp "dump_tree: full_dump parameter is deprecated, please use mode => 'user'";
63 }
64  
65 154 ▶ my $check = delete $args{check} || 'yes';
Condition100%2/2
delete $args{'check'} || 'yes'
l!l
35119
MC/DC100%1/1
delete $args{'check'} || 'yes'
delete $args{'check'}
Truth table100%2/2
A
A= delete $args{'check'}
Aresult
11
00
66 154 ▶ if ( $check !~ /yes|no|skip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check =~ /yes|no|skip/) 0 154 154
67 0 croak "dump_tree: unexpected 'check' value: $check";
68 }
69  
70 # mode parameter is slightly different from fetch's mode
71 154 ▶ my $fetch_mode =
Branch: 3 branches
BranchTrueFalseTotal
$full ? : 0 154 154
$mode eq 'full' ? : 18 136 154
$mode ? : 40 96 136
72 $full ? 'user'
73 : $mode eq 'full' ? 'user'
74 : $mode ? $mode
75 : 'custom';
76  
77 154 ▶ if ( my $err = Config::Model::Value->is_bad_mode($fetch_mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = "Config::Model::Value"->is_bad_mode($fetch_mode)) 0 154 154
78 0 croak "dump_tree: $err";
79 }
80  
81 my $node = delete $args{node}
82 154 ▶ || croak "dump_tree: missing 'node' parameter";
Condition50%1/2
delete $args{'node'} || croak(q[dump_tree: missing 'node' parameter])
l!l
1540
MC/DC0%0/1
delete $args{'node'} || croak(q[dump_tree: missing 'node' parameter])
delete $args{'node'}
Truth table50%1/2
A
A= delete $args{'node'}
Aresult
11
00
83  
84 6592 ▶ my $compute_pad = sub ($obj) {
Subroutine __ANON__: called CC=2 SCAR=6.9
85 6592 my $depth = 0;
86 6592 ▶ while ( defined $obj->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->parent) 9590 6592 16182
87 9590 $depth++;
88 9590 $obj = $obj->parent;
89 }
90 6592 return ' ' x $depth;
91 154 };
92  
93 5418 ▶ my $leaf_cb = sub ($scanner, $data_r, $node, $element, $index, $value_obj) {
Subroutine __ANON__: called CC=6 SCAR=17.9
94 # get value or only customized value
95 5418 my $value = quote( $value_obj->fetch( mode => $fetch_mode, check => $check ) );
96 5415 $index = id_quote($index);
97 5415 $element = id_quote($element);
98  
99 5415 my $pad = $compute_pad->($node);
100  
101 5415 ▶ my $name =
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 71 5344 5415
102 defined $index
103 ? "$element:$index"
104 : $element;
105  
106 # add annotation for obj contained in hash or list
107 5415 my $note = note_quote( $value_obj->annotation );
108 5415 ▶ $$data_r .= "\n" . $pad . $name if defined $value or $note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value or $note 1303 4112 5415
Condition100%3/3
defined $value or $note
l!l&&r!l&&!r
129764112
MC/DC100%2/2
defined $value or $note
defined $value $note
Truth table100%3/3
A or B
A= defined $valueB= $note
ABresult
1X1
011
000
109 5415 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1297 4118 5415
110 1297 $value =~ s/\\n/\\\\n/g;
111 1297 $$data_r .= '=' . $value;
112 }
113 5415 ▶ $$data_r .= '#' . $note if $note;
Branch: 1 branch
BranchTrueFalseTotal
if $note 28 5387 5415
114 5415 return;
115 154 };
116  
117 42 ▶ my $check_list_cb = sub ($scanner, $data_r, $node, $element, $index, $value_obj) {
Subroutine __ANON__: called CC=6 SCAR=18.0
118 # get value or only customized value
119 42 my $value = $value_obj->fetch( mode => $fetch_mode, check => $check );
120 42 my $qvalue = quote($value);
121 42 $element = id_quote($element);
122 42 $index = id_quote($index);
123 42 my $pad = $compute_pad->($node);
124  
125 42 ▶ my $name =
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 42 42
126 defined $index
127 ? "$element:$index"
128 : $element;
129  
130 # add annotation for obj contained in hash or list
131 42 my $note = note_quote( $value_obj->annotation );
132 42 ▶ $$data_r .= "\n" . $pad . $name if $value or $note;
Branch: 1 branch
BranchTrueFalseTotal
if $value or $note 22 20 42
Condition66%2/3
$value or $note
l!l&&r!l&&!r
22020
MC/DC50%1/2
$value or $note
$value $note
Truth table66%2/3
A or B
A= $valueB= $note
ABresult
1X1
011
000
133 42 ▶ $$data_r .= '=' . $qvalue if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 22 20 42
134 42 ▶ $$data_r .= '#' . $note if $note;
Branch: 1 branch
BranchTrueFalseTotal
if $note 4 38 42
135 42 return;
136 154 };
137  
138 290 ▶ my $list_element_cb = sub ($scanner, $data_r, $node, $element, @keys) {
Subroutine __ANON__: called CC=7 SCAR=19.5
139 290 my $pad = $compute_pad->($node);
140 290 my $list_obj = $node->fetch_element($element);
141 290 my $q_element = id_quote($element);
142  
143 # add annotation for list element
144 290 my $list_note = note_quote( $list_obj->annotation );
145 290 ▶ $$data_r .= "\n$pad$q_element#$list_note" if $list_note;
Branch: 1 branch
BranchTrueFalseTotal
if $list_note 2 288 290
146  
147 290 ▶ if ( $list_obj->cargo_type =~ 'node' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_obj->cargo_type =~ /node/) { } 34 256 290
148 34 foreach my $k (@keys) {
149 36 $scanner->scan_list( $data_r, $node, $element, $k );
150 }
151 }
152 else {
153 # write value comments
154 256 foreach my $idx ( $list_obj->fetch_all_indexes ) {
155 310 my $note = $list_obj->fetch_with_id($idx)->annotation;
156 310 ▶ $$data_r .= "\n$pad$q_element:$idx#" . note_quote($note) if $note;
Branch: 1 branch
BranchTrueFalseTotal
if $note 64 246 310
157 }
158  
159 # skip undef values
160 my @val = id_quote(
161 256 grep { defined $_ }
162 $list_obj->fetch_all_values(mode => $fetch_mode, check => $check)
163 );
164 256 ▶ $$data_r .= "\n$pad$q_element:=" . join( ',', @val ) if @val;
Branch: 1 branch
BranchTrueFalseTotal
if @val 137 119 256
165 }
166 290 return;
167 154 };
168  
169 188 ▶ my $hash_element_cb = sub ($scanner, $data_r, $node, $element, @keys) {
Subroutine __ANON__: called CC=2 SCAR=6.9
170 188 my $pad = $compute_pad->($node);
171 188 my $hash_obj = $node->fetch_element($element);
172 188 my $q_element = id_quote($element);
173  
174 # add annotation for list or hash element
175 188 my $note = note_quote( $hash_obj->annotation );
176 188 ▶ $$data_r .= "\n$pad$q_element#$note" if $note;
Branch: 1 branch
BranchTrueFalseTotal
if $note 2 186 188
177  
178 # resume exploration
179 188 map { $scanner->scan_hash( $data_r, $node, $element, $_ ); } @keys;
180 187 return;
181 154 };
182  
183 # called for nodes contained in nodes (not root).
184 # This node can be held by a plain element or a hash element or a list element
185 657 ▶ my $node_element_cb = sub ($scanner, $data_r, $node, $element, $key, $contained_node) {
Subroutine __ANON__: called CC=8 SCAR=20.8
186 657 my $type = $node->element_type($element);
187  
188 657 ▶ return if $skip_aw and $contained_node->is_auto_write_for_type($skip_aw);
Branch: 1 branch
BranchTrueFalseTotal
if $skip_aw and $contained_node->is_auto_write_for_type($skip_aw) 0 657 657
Condition66%2/3
$skip_aw and $contained_node->is_auto_write_for_type($skip_aw)
!ll&&!rl&&r
642150
MC/DC0%0/2
$skip_aw and $contained_node->is_auto_write_for_type($skip_aw)
$skip_aw $contained_node->is_auto_write_for_type($skip_aw)
Truth table66%2/3
A and B
A= $skip_awB= $contained_node->is_auto_write_for_type($skip_aw)
ABresult
0X0
100
111
189  
190 657 my $pad = $compute_pad->($node);
191 657 my $elt = $node->fetch_element($element);
192  
193 # load string can feature only one comment per element_type
194 # ie foo#comment foo:bar#comment foo:bar=val#comment are fine
195 # but foo#comment:bar if not valid -> foo#commaent foo:bar
196  
197 657 my $q_element = id_quote($element);
198 657 my $head = "\n$pad$q_element";
199 657 my $node_note = note_quote( $contained_node->annotation );
200  
201 657 ▶ if ( $type eq 'list' or $type eq 'hash' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'list' or $type eq 'hash') { } 295 362 657
Condition100%3/3
$type eq "list" or $type eq "hash"
l!l&&r!l&&!r
36259362
MC/DC100%2/2
$type eq "list" or $type eq "hash"
$type eq "list" $type eq "hash"
Truth table100%3/3
A or B
A= $type eq "list"B= $type eq "hash"
ABresult
1X1
011
000
202 295 $head .= ':' . id_quote($key);
203 295 ▶ $head .= '#' . $node_note if $node_note;
Branch: 1 branch
BranchTrueFalseTotal
if $node_note 64 231 295
204 295 my $sub_data = '';
205 295 $scanner->scan_node( \$sub_data, $contained_node );
206 294 $$data_r .= $head . $sub_data . ' -';
207 }
208 else {
209 362 ▶ $head .= '#' . $node_note if $node_note;
Branch: 1 branch
BranchTrueFalseTotal
if $node_note 12 350 362
210 362 my $sub_data = '';
211 362 $scanner->scan_node( \$sub_data, $contained_node );
212  
213 # skip simple nodes that do not bring data
214 362 ▶ $$data_r .= $head . $sub_data . ' -' if $sub_data;
Branch: 1 branch
BranchTrueFalseTotal
if $sub_data 170 192 362
215 }
216 656 return;
217 154 };
218  
219 154 my @scan_args = (
220 auto_vivify => $auto_v,
221 list_element_cb => $list_element_cb,
222 hash_element_cb => $hash_element_cb,
223 leaf_cb => $leaf_cb,
224 node_element_cb => $node_element_cb,
225 check_list_element_cb => $check_list_cb,
226 check => $check,
227 );
228  
229 154 my @left = keys %args;
230 154 ▶ croak "Dumper: unknown parameter:@left" if @left;
Branch: 1 branch
BranchTrueFalseTotal
if @left 0 154 154
231  
232 # perform the scan
233 154 my $view_scanner = Config::Model::ObjTreeScanner->new(@scan_args);
234  
235 154 my $ret = '';
236 154 my $root_note = note_quote( $node->annotation );
237 154 ▶ $ret .= "\n#$root_note" if $root_note;
Branch: 1 branch
BranchTrueFalseTotal
if $root_note 22 132 154
238 154 $view_scanner->scan_node( \$ret, $node );
239  
240 151 substr( $ret, 0, 1, '' ); # remove leading \n
241 151 ▶ $ret .= ' -' if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 129 22 151
242 151 return $ret . "\n";
243 }
244  
245 59 1;
246  
247 # ABSTRACT: Serialize data of config tree
248  
249 __END__
« blib/lib/Config/Model/AnyThing.pm ↑ Summary blib/lib/Config/Model/Describe.pm »