| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 167 | 814 | ▶ | return ($node, $_) if is_hash($node->{$_}) && exists $node->{$_}->{'@inherit'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_hash($node->{$_}) and exists $node->{$_}{"\@inherit"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 181 | 37 | $subnode->{$key} = $self->expand_data($base_node, $subnode->{$key}, $dir); | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 179 | ▶ | if (exists $node->{'@inherit'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | 65 | ▶ | die "The value of '\@inherit' must be a string or array" unless ref($node->{'@inherit'}) eq 'Config::Neat::Array'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 197 | 70 | ▶ | die "Neither filename nor selector are specified" unless $filename or $selector; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filename or $selector
MC/DC0%0/2
$filename or $selector
$filename
$selector
Truth table66%2/3
A or B
A= $filenameB= $selector
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 224 | 31 | $merge_dir = dirname($fullpath); | |||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | 31 | ▶ | if (!exists $self->{cache}->{$fullpath}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 256 | 57 | ▶ | die "Bad selector syntax (leading slash) in '$selector'" if $selector =~ m/^\//; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 57 | ▶ | return _clone($node) if $selector eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 265 | 53 | ▶ | if (is_hash($result) && exists $result->{$_}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
is_hash($result) and exists $result->{$_}
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->{$_}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
is_hash($data1) and is_hash($data2)
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)
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | 120 | ▶ | if ($key =~ m/^-(.*)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 291 | 11 | ▶ | die "Key '$key' contains bogus data; expected an empty or true value" unless $data1->{$key}->as_boolean; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 299 | 73 | ▶ | if ($key =~ m/^\+(.*)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 300 | 13 | ▶ | if ((!exists $data2->{$base_key} || is_neat_array($data2->{$base_key}))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not exists $data2->{$base_key} or is_neat_array($data2->{$base_key})
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})
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
is_simple_array($data1->{$key}) and exists $data2->{$base_key}
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}
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
is_hash($data1->{$key}) and $key =~ /^\+(.*)$/
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 =~ /^\+(.*)$/
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
is_hash($data1->{$key}) and is_hash($data2->{$key})
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})
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $data2->{$key} and not exists $data1->{$key}
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}
| |||||||||||||||||||||||||||||||||
| 332 | 42 | $data1->{$key} = $data2->{$key}; | |||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | 128 | ▶ | $data1 = to_ixhash($data1) unless is_ixhash($data1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 342 | 8 | unshift(@$data1, $data2); | |||||||||||||||||||||||||||||||
| 343 | } else { | ||||||||||||||||||||||||||||||||
| 344 | 2 | unshift(@$data1, @$data2); | |||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 226 | return $data1; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 2 | 1; | |||||||||||||||||||||||||||||||