| 1 | package Data::Transfigure::Tree::Merge 1.03; | ||
| 2 | use v5.26; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: merges marked hashrefs into their parent hash | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Data::Transfigure::Tree::Merge - merges marked hashrefs into their parent hash | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | C<Data::Transfigure::Tree::Merge> allows you to mark a hashref key with a | ||
| 14 | trailing C<%> to indicate that you want the HashRef pointed to by that key to be | ||
| 15 | merged into the HashRef containing it. Common keys are overwritten by the merged | ||
| 16 | data. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | use Object::Pad; | ||
| 21 | |||
| 22 | use Data::Transfigure::Tree; | ||
| 23 | class Data::Transfigure::Tree::Merge : does(Data::Transfigure::Tree) { | ||
| 24 | |||
| 25 | =head1 FIELDS | ||
| 26 | |||
| 27 | =head2 I<none> | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | my sub tree_merge($entity) { | ||
| 32 | if (ref($entity) eq 'HASH') { | ||
| 33 | foreach (grep {/%$/} keys($entity->%*)) { | ||
| 34 | next unless (ref($entity->{$_}) eq 'HASH'); | ||
| 35 | my $h = delete($entity->{$_}); | ||
| 36 | $entity->{$_} = $h->{$_} foreach (keys($h->%*)); | ||
| 37 | __SUB__->($entity); | ||
| 38 | } | ||
| 39 | __SUB__->($entity->{$_}) foreach (keys($entity->%*)); | ||
| 40 | } elsif (ref($entity) eq 'ARRAY') { | ||
| 41 | __SUB__->($_) foreach ($entity->@*); | ||
| 42 | } | ||
| 43 | return $entity; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub BUILDARGS ($class, %args) { | ||
| 47 | $class->SUPER::BUILDARGS( | ||
| 48 | handler => sub ($entity) { | ||
| 49 | return tree_merge($entity); | ||
| 50 | } | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | =pod | ||
| 57 | |||
| 58 | =head1 AUTHOR | ||
| 59 | |||
| 60 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 61 | |||
| 62 | =head1 LICENSE | ||
| 63 | |||
| 64 | Copyright (c) 2024 Mark Tyrrell | ||
| 65 | |||
| 66 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 67 | of this software and associated documentation files (the "Software"), to deal | ||
| 68 | in the Software without restriction, including without limitation the rights | ||
| 69 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 70 | copies of the Software, and to permit persons to whom the Software is | ||
| 71 | furnished to do so, subject to the following conditions: | ||
| 72 | |||
| 73 | The above copyright notice and this permission notice shall be included in all | ||
| 74 | copies or substantial portions of the Software. | ||
| 75 | |||
| 76 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 77 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 78 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 79 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 80 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 81 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 82 | SOFTWARE. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ |