| 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 | our $VERSION = '1.4'; | ||
| 71 | |||
| 72 | use strict; | ||
| 73 | |||
| 74 | no warnings qw(uninitialized); | ||
| 75 | |||
| 76 | use Config::Neat; | ||
| 77 | 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 | read_file); | ||
| 81 | use File::Spec::Functions qw(rel2abs); | ||
| 82 | use File::Basename qw(dirname); | ||
| 83 | use Storable qw(dclone); | ||
| 84 | use Tie::IxHash; | ||
| 85 | |||
| 86 | # | ||
| 87 | # Initialize object | ||
| 88 | # | ||
| 89 | sub new { | ||
| 90 | my ($class) = @_; | ||
| 91 | |||
| 92 | my $self = { | ||
| 93 | cfg => Config::Neat->new(), | ||
| 94 | }; | ||
| 95 | |||
| 96 | bless $self, $class; | ||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub init { | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | $self->{cache} = {}; | ||
| 104 | $self->{saved_context} = []; | ||
| 105 | $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 | my ($self, $filename, $binmode) = @_; | ||
| 112 | |||
| 113 | $self->init; | ||
| 114 | $self->{binmode} = $binmode; | ||
| 115 | |||
| 116 | 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 | my ($self, $nconf_text, $filename) = @_; | ||
| 123 | $self->init; | ||
| 124 | return $self->_parse($nconf_text, $filename); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _parse_file { | ||
| 128 | my ($self, $filename) = @_; | ||
| 129 | |||
| 130 | $filename = rel2abs($filename); | ||
| 131 | return $self->_parse(read_file($filename, $self->{binmode}), $filename); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _parse { | ||
| 135 | my ($self, $nconf_text, $filename) = @_; | ||
| 136 | |||
| 137 | # preserve current context | ||
| 138 | push @{$self->{saved_context}}, { | ||
| 139 | orig_data => $self->{orig_data}, | ||
| 140 | fullpath => $self->{fullpath} | ||
| 141 | }; | ||
| 142 | |||
| 143 | # parse the file | ||
| 144 | my $data = $self->{cfg}->parse($nconf_text); | ||
| 145 | |||
| 146 | # generate the local context for expand_data() | ||
| 147 | $self->{orig_data} = _clone($data); | ||
| 148 | $self->{fullpath} = rel2abs($filename); | ||
| 149 | |||
| 150 | # process @inherit rules | ||
| 151 | $data = $self->expand_data($data, $data, dirname($self->{fullpath})); | ||
| 152 | |||
| 153 | # restore the context | ||
| 154 | my $context = pop @{$self->{saved_context}}; | ||
| 155 | $self->{orig_data} = $context->{orig_data}; | ||
| 156 | $self->{fullpath} = $context->{fullpath}; | ||
| 157 | |||
| 158 | return $data; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub find_next_node_to_expand { | ||
| 162 | my ($self, $node) = @_; | ||
| 163 | if (is_hash($node)) { | ||
| 164 | map { | ||
| 165 | my ($subnode, $key) = $self->find_next_node_to_expand($node->{$_}); | ||
| 166 | return ($subnode, $key) if defined $subnode; | ||
| 167 | return ($node, $_) if is_hash($node->{$_}) && exists $node->{$_}->{'@inherit'}; | ||
| 168 | } keys %$node; | ||
| 169 | } | ||
| 170 | return undef; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub expand_data { | ||
| 174 | my ($self, $base_node, $node, $dir) = @_; | ||
| 175 | if (is_hash($node)) { | ||
| 176 | |||
| 177 | # expand child nodes | ||
| 178 | while (1) { | ||
| 179 | my ($subnode, $key) = $self->find_next_node_to_expand($node); | ||
| 180 | last unless $subnode; | ||
| 181 | $subnode->{$key} = $self->expand_data($base_node, $subnode->{$key}, $dir); | ||
| 182 | } | ||
| 183 | |||
| 184 | if (exists $node->{'@inherit'}) { | ||
| 185 | die "The value of '\@inherit' must be a string or array" unless ref($node->{'@inherit'}) eq 'Config::Neat::Array'; | ||
| 186 | |||
| 187 | my @a = @{$node->{'@inherit'}}; | ||
| 188 | |||
| 189 | my $intermediate = new_ixhash; | ||
| 190 | |||
| 191 | foreach my $from (@a) { | ||
| 192 | my $orig_from = $from; | ||
| 193 | 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 | $filename = '' if $filename eq '.'; | ||
| 197 | die "Neither filename nor selector are specified" unless $filename or $selector; | ||
| 198 | |||
| 199 | # normalize path and selector | ||
| 200 | my $fullpath = $filename eq '' ? $self->{fullpath} : rel2abs($filename, $dir); # make path absolute based on current context dir | ||
| 201 | $selector =~ s/^\///; # remove leading slash, if any | ||
| 202 | |||
| 203 | $from = $fullpath.'#'.$selector; | ||
| 204 | |||
| 205 | # make sure we don't have any infinite loops | ||
| 206 | map { | ||
| 207 | if ($from eq $_) { | ||
| 208 | my $err = | ||
| 209 | "Infinite loop detected in $self->{fullpath} at `\@inherit $orig_from`\n". | ||
| 210 | "\@include stack:\n", join("\n", @{$self->{include_stack}}), "\n\n"; | ||
| 211 | die $err; | ||
| 212 | } | ||
| 213 | } @{$self->{include_stack}}; | ||
| 214 | |||
| 215 | push @{$self->{include_stack}}, $from; | ||
| 216 | |||
| 217 | my $merge_node; | ||
| 218 | if (exists $self->{cache}->{$from}) { | ||
| 219 | $merge_node = _clone($self->{cache}->{$from}); | ||
| 220 | } else { | ||
| 221 | my $merge_cfg; | ||
| 222 | my $merge_dir = $dir; | ||
| 223 | if ($filename) { | ||
| 224 | $merge_dir = dirname($fullpath); | ||
| 225 | |||
| 226 | if (!exists $self->{cache}->{$fullpath}) { | ||
| 227 | $self->{cache}->{$fullpath} = $self->_parse_file($fullpath); | ||
| 228 | } | ||
| 229 | $merge_cfg = _clone($self->{cache}->{$fullpath}); | ||
| 230 | } else { | ||
| 231 | $merge_cfg = _clone($base_node); | ||
| 232 | } | ||
| 233 | |||
| 234 | $merge_node = $self->select_subnode($merge_cfg, $selector, $dir); | ||
| 235 | $merge_node = $self->expand_data($base_node, $merge_node, $merge_dir); | ||
| 236 | $self->{cache}->{$from} = _clone($merge_node); | ||
| 237 | } | ||
| 238 | |||
| 239 | $intermediate = $self->merge_data($merge_node, $intermediate, $dir); | ||
| 240 | pop @{$self->{include_stack}}; | ||
| 241 | } | ||
| 242 | |||
| 243 | delete $node->{'@inherit'}; | ||
| 244 | |||
| 245 | $node = $self->merge_data($node, $intermediate, $dir); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | return $node; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub select_subnode { | ||
| 253 | my ($self, $node, $selector, $dir) = @_; | ||
| 254 | |||
| 255 | die "Bad selector syntax (double slash) in '$selector'" if $selector =~ m/\/{2,}/; | ||
| 256 | die "Bad selector syntax (leading slash) in '$selector'" if $selector =~ m/^\//; | ||
| 257 | |||
| 258 | return _clone($node) if $selector eq ''; | ||
| 259 | |||
| 260 | my @a = split('/', $selector); | ||
| 261 | |||
| 262 | my $result = $node; | ||
| 263 | foreach (@a) { | ||
| 264 | next if ($_ eq ''); | ||
| 265 | if (is_hash($result) && exists $result->{$_}) { | ||
| 266 | $result = $result->{$_}; | ||
| 267 | } else { | ||
| 268 | die "Can't find key '$_' in node (selector: '$selector')"; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | return _clone($result); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _clone { | ||
| 275 | my $data = shift; | ||
| 276 | return ref($data) ? dclone($data) : $data; | ||
| 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 | my ($self, $data1, $data2, $dir) = @_; | ||
| 283 | |||
| 284 | if (is_hash($data1) && is_hash($data2)) { | ||
| 285 | my @keys = get_keys_in_order($data2, $data1); | ||
| 286 | |||
| 287 | foreach my $key (keys %$data1) { | ||
| 288 | my $base_key = $key =~ m/^[\+\-](.*)$/ ? $1 : $key; | ||
| 289 | |||
| 290 | if ($key =~ m/^-(.*)$/) { | ||
| 291 | die "Key '$key' contains bogus data; expected an empty or true value" unless $data1->{$key}->as_boolean; | ||
| 292 | delete $data1->{$key}; | ||
| 293 | delete $data2->{$1}; | ||
| 294 | next; | ||
| 295 | } | ||
| 296 | |||
| 297 | # arrays are NOT merged by default; use `+key` to merge arrays | ||
| 298 | if (is_neat_array($data1->{$key})) { | ||
| 299 | if ($key =~ m/^\+(.*)$/) { | ||
| 300 | if ((!exists $data2->{$base_key} || is_neat_array($data2->{$base_key}))) { | ||
| 301 | $data1 = rename_ixhash_key($data1, $key, $base_key); | ||
| 302 | $key = $base_key; | ||
| 303 | } | ||
| 304 | |||
| 305 | if (is_simple_array($data1->{$key}) && 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 | $data1->{$key} = Config::Neat::Array->new([$data1->{$key}]); | ||
| 311 | } | ||
| 312 | } else { | ||
| 313 | delete $data2->{$key}; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | # hashes are merged by default; `+key { }` is the same as `key { }` | ||
| 318 | if (is_hash($data1->{$key}) && ($key =~ m/^\+(.*)$/)) { | ||
| 319 | $data1 = rename_ixhash_key($data1, $key, $base_key); | ||
| 320 | $key = $base_key; | ||
| 321 | } | ||
| 322 | |||
| 323 | if (is_hash($data1->{$key}) && is_hash($data2->{$key})) { | ||
| 324 | my $offset = get_next_auto_key($data2->{$key}); | ||
| 325 | $data1->{$key} = offset_keys($data1->{$key}, $offset); | ||
| 326 | } | ||
| 327 | $data1->{$key} = $self->merge_data($data1->{$key}, $data2->{$key}, $dir); | ||
| 328 | } | ||
| 329 | |||
| 330 | foreach my $key (keys %$data2) { | ||
| 331 | if (exists $data2->{$key} && !exists $data1->{$key}) { | ||
| 332 | $data1->{$key} = $data2->{$key}; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | $data1 = to_ixhash($data1) unless is_ixhash($data1); | ||
| 337 | $data1 = reorder_ixhash($data1, \@keys); | ||
| 338 | } elsif (is_neat_array($data1) && is_neat_array($data2)) { | ||
| 339 | # always push data as array ref, not as individual items | ||
| 340 | # to maintain 'array of arrays' structure | ||
| 341 | if (is_simple_array($data2)) { | ||
| 342 | unshift(@$data1, $data2); | ||
| 343 | } else { | ||
| 344 | unshift(@$data1, @$data2); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | return $data1; | ||
| 349 | } | ||
| 350 | |||
| 351 | 1; |