lib/Config/Neat/Inheritable.pm

filter: Statementstmt 98.1% 155 / 158 Branchbran 82.3% 56 / 68 Conditioncond 86.6% 26 / 30 MC/DCmcdc 70.0% 14 / 20 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 8 totaltotal 88.7% 269 / 303 CC 49 SCAR 25.8
CC
49
Cov
93%
merge_data
24.1
expand_data
14.1
select_subnode
8.3
CRAP 13.2
« lib/Config/Neat/Render.pm ↑ Summary lib/Config/Neat/Array.pm »
1 =head1 NAME
2  
3 Config::Neat::Inheritable - Config::Neat files with inheritance
4  
5 =head1 SYNOPSIS
6  
7 File 01.nconf:
8  
9 foo {
10 bar baz
11  
12 etc {
13 pwd 1
14 }
15 }
16  
17 abc def
18  
19 File 02.nconf:
20  
21 @inherit 01.nconf
22  
23 foo {
24 bar replace
25 }
26  
27 -abc
28  
29 Resulting data structure will be equivalent to:
30  
31 foo {
32 bar replace
33  
34 etc {
35 pwd 1
36 }
37 }
38  
39 Within C<@inherit>, you can use selectors as slash-delimited paths to the node
40 within a target file, for example:
41  
42 whatever {
43 @inherit 01.nconf#foo/etc
44 bar replace
45 }
46  
47 Resulting data structure will be equivalent to:
48  
49 whatever {
50 pwd 1
51 bar replace
52 }
53  
54 Multiple inheritance is supported; use '.' do denote the the current file:
55  
56 @inherit 01.nconf#foo 02.nconf#bar .#baz
57  
58 =head1 COPYRIGHT
59  
60 Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>
61  
62 =head1 SEE ALSO
63  
64 L<https://github.com/iafan/Config-Neat>
65  
66 =cut
67  
68 package Config::Neat::Inheritable;
69  
70 2 our $VERSION = '1.4';
71  
72 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 2 ▶use Config::Neat;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 use Config::Neat::Util qw(new_ixhash is_hash is_ixhash to_ixhash is_neat_array
78 is_simple_array get_next_auto_key offset_keys
79 get_keys_in_order reorder_ixhash rename_ixhash_key
80 2 ▶ read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 #
87 # Initialize object
88 #
89 sub new {
90 3 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
91  
92 3 my $self = {
93 cfg => Config::Neat->new(),
94 };
95  
96 3 bless $self, $class;
97 3 return $self;
98 }
99  
100 sub init {
101 61 ▶ my ($self) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
102  
103 61 $self->{cache} = {};
104 61 $self->{saved_context} = [];
105 61 $self->{include_stack} = [];
106 }
107  
108 # Given a file name, will read this file in the specified mode (UTF-8 by default),
109 # parse it and expand '@inherit' blocks
110 sub parse_file {
111 61 ▶ my ($self, $filename, $binmode) = @_;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod undocumented
112  
113 61 $self->init;
114 61 $self->{binmode} = $binmode;
115  
116 61 return $self->_parse_file($filename);
117 }
118  
119 # Given a string representation of the config, returns a parsed tree
120 # with expanded '@inherit' blocks
121 sub parse {
122 0 ▶ my ($self, $nconf_text, $filename) = @_;
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
123 0 $self->init;
124 0 return $self->_parse($nconf_text, $filename);
125 }
126  
127 sub _parse_file {
128 92 ▶ my ($self, $filename) = @_;
Subroutine _parse_file: called CC=1 SCAR=0.0
129  
130 92 $filename = rel2abs($filename);
131 92 return $self->_parse(read_file($filename, $self->{binmode}), $filename);
132 }
133  
134 sub _parse {
135 92 ▶ my ($self, $nconf_text, $filename) = @_;
Subroutine _parse: called CC=1 SCAR=0.0
136  
137 # preserve current context
138 92 push @{$self->{saved_context}}, {
139 orig_data => $self->{orig_data},
140 fullpath => $self->{fullpath}
141 92 };
142  
143 # parse the file
144 92 my $data = $self->{cfg}->parse($nconf_text);
145  
146 # generate the local context for expand_data()
147 92 $self->{orig_data} = _clone($data);
148 92 $self->{fullpath} = rel2abs($filename);
149  
150 # process @inherit rules
151 92 $data = $self->expand_data($data, $data, dirname($self->{fullpath}));
152  
153 # restore the context
154 86 my $context = pop @{$self->{saved_context}};
155 86 $self->{orig_data} = $context->{orig_data};
156 86 $self->{fullpath} = $context->{fullpath};
157  
158 86 return $data;
159 }
160  
161 sub find_next_node_to_expand {
162 1030 ▶ my ($self, $node) = @_;
Subroutine find_next_node_to_expand: called CC=5 SCAR=16.1
Pod undocumented
163 1030 ▶ if (is_hash($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($node)) 575 455 1030
164 map {
165 575 my ($subnode, $key) = $self->find_next_node_to_expand($node->{$_});
166 814 ▶ return ($subnode, $key) if defined $subnode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $subnode 0 814 814
167 814 ▶ return ($node, $_) if is_hash($node->{$_}) && exists $node->{$_}->{'@inherit'};
Branch: 1 branch
BranchTrueFalseTotal
if is_hash($node->{$_}) and exists $node->{$_}{"\@inherit"} 37 777 814
Condition100%3/3
is_hash($node->{$_}) and exists $node->{$_}{"\@inherit"}
!ll&&!rl&&r
45532237
MC/DC100%2/2
is_hash($node->{$_}) and exists $node->{$_}{"\@inherit"}
is_hash($node->{$_}) exists $node->{$_}{"\@inherit"}
Truth table100%3/3
A and B
A= is_hash($node->{$_})B= exists $node->{$_}{"\@inherit"}
ABresult
0X0
100
111
168 } keys %$node;
169 }
170 993 return undef;
171 }
172  
173 sub expand_data {
174 185 ▶ my ($self, $base_node, $node, $dir) = @_;
Subroutine expand_data: called CC=14 SCAR=26.4
Pod undocumented
175 185 ▶ if (is_hash($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($node)) 185 0 185
176  
177 # expand child nodes
178 185 while (1) {
179 216 my ($subnode, $key) = $self->find_next_node_to_expand($node);
180 216 ▶ last unless $subnode;
Branch: 1 branch
BranchTrueFalseTotal
unless $subnode 179 37 216
181 37 $subnode->{$key} = $self->expand_data($base_node, $subnode->{$key}, $dir);
182 }
183  
184 179 ▶ if (exists $node->{'@inherit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"\@inherit"}) 65 114 179
185 65 ▶ die "The value of '\@inherit' must be a string or array" unless ref($node->{'@inherit'}) eq 'Config::Neat::Array';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node->{"\@inherit"} eq "Config::Neat::Array" 0 65 65
186  
187 65 my @a = @{$node->{'@inherit'}};
188  
189 65 my $intermediate = new_ixhash;
190  
191 65 foreach my $from (@a) {
192 70 my $orig_from = $from;
193 70 my ($filename, $selector) = split('#', $from, 2);
194 # allow .#selector style to indicate the current file, since #selector
195 # without the leading symbol will be treated as a comment line
196 70 ▶ $filename = '' if $filename eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "." 31 39 70
197 70 ▶ die "Neither filename nor selector are specified" unless $filename or $selector;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename or $selector 0 70 70
Condition66%2/3
$filename or $selector
l!l&&r!l&&!r
33370
MC/DC0%0/2
$filename or $selector
$filename $selector
Truth table66%2/3
A or B
A= $filenameB= $selector
ABresult
1X1
011
000
198  
199 # normalize path and selector
200 70 ▶ my $fullpath = $filename eq '' ? $self->{fullpath} : rel2abs($filename, $dir); # make path absolute based on current context dir
Branch: 1 branch
BranchTrueFalseTotal
$filename eq '' ? : 37 33 70
201 70 $selector =~ s/^\///; # remove leading slash, if any
202  
203 70 $from = $fullpath.'#'.$selector;
204  
205 # make sure we don't have any infinite loops
206 map {
207 19 ▶ if ($from eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq $_) 3 16 19
208 my $err =
209 "Infinite loop detected in $self->{fullpath} at `\@inherit $orig_from`\n".
210 3 "\@include stack:\n", join("\n", @{$self->{include_stack}}), "\n\n";
211 3 die $err;
212 }
213 70 } @{$self->{include_stack}};
214  
215 67 push @{$self->{include_stack}}, $from;
216  
217 67 my $merge_node;
218 67 ▶ if (exists $self->{cache}->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cache'}{$from}) { } 8 59 67
219 8 $merge_node = _clone($self->{cache}->{$from});
220 } else {
221 59 my $merge_cfg;
222 59 my $merge_dir = $dir;
223 59 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 31 28 59
224 31 $merge_dir = dirname($fullpath);
225  
226 31 ▶ if (!exists $self->{cache}->{$fullpath}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"cache"}{$fullpath}) 31 0 31
227 31 $self->{cache}->{$fullpath} = $self->_parse_file($fullpath);
228 }
229 29 $merge_cfg = _clone($self->{cache}->{$fullpath});
230 } else {
231 28 $merge_cfg = _clone($base_node);
232 }
233  
234 57 $merge_node = $self->select_subnode($merge_cfg, $selector, $dir);
235 56 $merge_node = $self->expand_data($base_node, $merge_node, $merge_dir);
236 53 $self->{cache}->{$from} = _clone($merge_node);
237 }
238  
239 61 $intermediate = $self->merge_data($merge_node, $intermediate, $dir);
240 61 pop @{$self->{include_stack}};
241 }
242  
243 56 delete $node->{'@inherit'};
244  
245 56 $node = $self->merge_data($node, $intermediate, $dir);
246 }
247 }
248  
249 170 return $node;
250 }
251  
252 sub select_subnode {
253 57 ▶ my ($self, $node, $selector, $dir) = @_;
Subroutine select_subnode: called CC=8 SCAR=21.1
Pod undocumented
254  
255 57 ▶ die "Bad selector syntax (double slash) in '$selector'" if $selector =~ m/\/{2,}/;
Branch: 1 branch
BranchTrueFalseTotal
if $selector =~ m[/{2,}] 0 57 57
256 57 ▶ die "Bad selector syntax (leading slash) in '$selector'" if $selector =~ m/^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $selector =~ m[^/] 0 57 57
257  
258 57 ▶ return _clone($node) if $selector eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $selector eq "" 23 34 57
259  
260 34 my @a = split('/', $selector);
261  
262 34 my $result = $node;
263 34 foreach (@a) {
264 53 ▶ next if ($_ eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 53 53
265 53 ▶ if (is_hash($result) && exists $result->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($result) and exists $result->{$_}) { } 52 1 53
Condition66%2/3
is_hash($result) and exists $result->{$_}
!ll&&!rl&&r
0152
MC/DC50%1/2
is_hash($result) and exists $result->{$_}
is_hash($result) exists $result->{$_}
Truth table66%2/3
A and B
A= is_hash($result)B= exists $result->{$_}
ABresult
0X0
100
111
266 52 $result = $result->{$_};
267 } else {
268 1 die "Can't find key '$_' in node (selector: '$selector')";
269 }
270 }
271 33 return _clone($result);
272 }
273  
274 sub _clone {
275 266 ▶ my $data = shift;
Subroutine _clone: called CC=2 SCAR=7.2
276 266 ▶ return ref($data) ? dclone($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 266 0 266
277 }
278  
279 # merge tree structure from data2 into data1
280 # data1 is the one that may contain `-key` and `+key` entries
281 sub merge_data {
282 226 ▶ my ($self, $data1, $data2, $dir) = @_;
Subroutine merge_data: called CC=24 SCAR=31.8
Pod undocumented
283  
284 226 ▶ if (is_hash($data1) && is_hash($data2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash($data1) and is_hash($data2)) { } 128 98 226
elsif (is_neat_array($data1) and is_neat_array($data2)) { } 10 113 123
Condition100%3/3
is_hash($data1) and is_hash($data2)
!ll&&!rl&&r
7325128
MC/DC100%2/2
is_hash($data1) and is_hash($data2)
is_hash($data1) is_hash($data2)
Truth table100%3/3
A and B
A= is_hash($data1)B= is_hash($data2)
ABresult
0X0
100
111
285 128 my @keys = get_keys_in_order($data2, $data1);
286  
287 128 foreach my $key (keys %$data1) {
288 120 ▶ my $base_key = $key =~ m/^[\+\-](.*)$/ ? $1 : $key;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[\+\-](.*)$/ ? : 29 91 120
289  
290 120 ▶ if ($key =~ m/^-(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^-(.*)$/) 11 109 120
291 11 ▶ die "Key '$key' contains bogus data; expected an empty or true value" unless $data1->{$key}->as_boolean;
Branch: 1 branch
BranchTrueFalseTotal
unless $data1->{$key}->as_boolean 0 11 11
292 11 delete $data1->{$key};
293 11 delete $data2->{$1};
294 11 next;
295 }
296  
297 # arrays are NOT merged by default; use `+key` to merge arrays
298 109 ▶ if (is_neat_array($data1->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_neat_array($data1->{$key})) 73 36 109
299 73 ▶ if ($key =~ m/^\+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\+(.*)$/) { } 13 60 73
300 13 ▶ if ((!exists $data2->{$base_key} || is_neat_array($data2->{$base_key}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $data2->{$base_key} or is_neat_array($data2->{$base_key})) 13 0 13
Condition66%2/3
not exists $data2->{$base_key} or is_neat_array($data2->{$base_key})
l!l&&r!l&&!r
3100
MC/DC0%0/2
not exists $data2->{$base_key} or is_neat_array($data2->{$base_key})
not exists $data2->{$base_key} is_neat_array($data2->{$base_key})
Truth table66%2/3
not(A) or B
A= not exists $data2->{$base_key}B= is_neat_array($data2->{$base_key})
ABresult
1X1
011
000
301 13 $data1 = rename_ixhash_key($data1, $key, $base_key);
302 13 $key = $base_key;
303 }
304  
305 13 ▶ if (is_simple_array($data1->{$key}) && exists $data2->{$base_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_simple_array($data1->{$key}) and exists $data2->{$base_key}) 8 5 13
Condition100%3/3
is_simple_array($data1->{$key}) and exists $data2->{$base_key}
!ll&&!rl&&r
238
MC/DC100%2/2
is_simple_array($data1->{$key}) and exists $data2->{$base_key}
is_simple_array($data1->{$key}) exists $data2->{$base_key}
Truth table100%3/3
A and B
A= is_simple_array($data1->{$key})B= exists $data2->{$base_key}
ABresult
0X0
100
111
306 # we are about to merge two arrays; before doing so,
307 # if the accumulator array is a simple one, we need to convert it
308 # into an array, containing a single array ref, to maintain the
309 # 'array of arrays' structure
310 8 $data1->{$key} = Config::Neat::Array->new([$data1->{$key}]);
311 }
312 } else {
313 60 delete $data2->{$key};
314 }
315 }
316  
317 # hashes are merged by default; `+key { }` is the same as `key { }`
318 109 ▶ if (is_hash($data1->{$key}) && ($key =~ m/^\+(.*)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($data1->{$key}) and $key =~ /^\+(.*)$/) 5 104 109
Condition100%3/3
is_hash($data1->{$key}) and $key =~ /^\+(.*)$/
!ll&&!rl&&r
73315
MC/DC100%2/2
is_hash($data1->{$key}) and $key =~ /^\+(.*)$/
is_hash($data1->{$key}) $key =~ /^\+(.*)$/
Truth table100%3/3
A and B
A= is_hash($data1->{$key})B= $key =~ /^\+(.*)$/
ABresult
0X0
100
111
319 5 $data1 = rename_ixhash_key($data1, $key, $base_key);
320 5 $key = $base_key;
321 }
322  
323 109 ▶ if (is_hash($data1->{$key}) && is_hash($data2->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($data1->{$key}) and is_hash($data2->{$key})) 11 98 109
Condition100%3/3
is_hash($data1->{$key}) and is_hash($data2->{$key})
!ll&&!rl&&r
732511
MC/DC100%2/2
is_hash($data1->{$key}) and is_hash($data2->{$key})
is_hash($data1->{$key}) is_hash($data2->{$key})
Truth table100%3/3
A and B
A= is_hash($data1->{$key})B= is_hash($data2->{$key})
ABresult
0X0
100
111
324 11 my $offset = get_next_auto_key($data2->{$key});
325 11 $data1->{$key} = offset_keys($data1->{$key}, $offset);
326 }
327 109 $data1->{$key} = $self->merge_data($data1->{$key}, $data2->{$key}, $dir);
328 }
329  
330 128 foreach my $key (keys %$data2) {
331 63 ▶ if (exists $data2->{$key} && !exists $data1->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data2->{$key} and not exists $data1->{$key}) 42 21 63
Condition66%2/3
exists $data2->{$key} and not exists $data1->{$key}
!ll&&!rl&&r
02142
MC/DC50%1/2
exists $data2->{$key} and not exists $data1->{$key}
exists $data2->{$key} not exists $data1->{$key}
Truth table66%2/3
A and not(B)
A= exists $data2->{$key}B= not exists $data1->{$key}
ABresult
0X0
100
111
332 42 $data1->{$key} = $data2->{$key};
333 }
334 }
335  
336 128 ▶ $data1 = to_ixhash($data1) unless is_ixhash($data1);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ixhash($data1) 0 128 128
337 128 $data1 = reorder_ixhash($data1, \@keys);
338 ▶ } elsif (is_neat_array($data1) && is_neat_array($data2)) {
Condition100%3/3
is_neat_array($data1) and is_neat_array($data2)
!ll&&!rl&&r
256310
MC/DC100%2/2
is_neat_array($data1) and is_neat_array($data2)
is_neat_array($data1) is_neat_array($data2)
Truth table100%3/3
A and B
A= is_neat_array($data1)B= is_neat_array($data2)
ABresult
0X0
100
111
339 # always push data as array ref, not as individual items
340 # to maintain 'array of arrays' structure
341 10 ▶ if (is_simple_array($data2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_simple_array($data2)) { } 8 2 10
342 8 unshift(@$data1, $data2);
343 } else {
344 2 unshift(@$data1, @$data2);
345 }
346 }
347  
348 226 return $data1;
349 }
350  
351 2 1;
« lib/Config/Neat/Render.pm ↑ Summary lib/Config/Neat/Array.pm »