| 1 | package Data::Tree::Describe; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Data::Tree::Describe - Create annotated versions of complex data trees | ||
| 6 | |||
| 7 | =head1 WARNING | ||
| 8 | |||
| 9 | This module is in active development and has been uploaded simply as part of a | ||
| 10 | standard and automated release procedure. | ||
| 11 | |||
| 12 | If you have any ideas for what would be helpful to implement, please contact the | ||
| 13 | author! | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | =for comment Small howto | ||
| 18 | |||
| 19 | use Data::Tree::Describe; | ||
| 20 | |||
| 21 | my $data_object = {test=>['some','stuff']}; | ||
| 22 | |||
| 23 | my $described_tree = Data::Tree::Describe->new($data_object); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | =for comment The module's description. | ||
| 28 | |||
| 29 | This module was originally developed for data trees or objects created from | ||
| 30 | json::maybexs, though it technically will work on any perl data tree. | ||
| 31 | |||
| 32 | The module is fairly heavy processing wise and recursively iterates through a | ||
| 33 | tree determining the type for every node as well as other handy attributes such | ||
| 34 | as how many children are in any HASH or ARRAY type. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | # Internal perl | ||
| 39 | use v5.30.0; | ||
| 40 | |||
| 41 | # Internal perl modules (core) | ||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | |||
| 45 | # Internal perl modules (core,recommended) | ||
| 46 | use utf8; | ||
| 47 | use experimental qw(signatures); | ||
| 48 | |||
| 49 | # External modules | ||
| 50 | use Carp qw(cluck croak longmess shortmess); | ||
| 51 | |||
| 52 | # Version of this software | ||
| 53 | our $VERSION = '0.009'; | ||
| 54 | |||
| 55 | =head1 METHODS | ||
| 56 | |||
| 57 | Callable methods | ||
| 58 | |||
| 59 | =head2 new | ||
| 60 | |||
| 61 | Create a new annotated data tree | ||
| 62 | |||
| 63 | Takes 1 argument an $object | ||
| 64 | |||
| 65 | BEWARE large objects will take some time to be processed. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | # Primary code block | ||
| 70 | sub new($class,$input = {}) { | ||
| 71 | |||
| 72 | my $self = bless { | ||
| 73 | source => $input, | ||
| 74 | paths => [] | ||
| 75 | }, $class; | ||
| 76 | |||
| 77 | $self->{tree} = $self->_digest($input); | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 paths_list | ||
| 82 | |||
| 83 | Return a list of all paths in the format: [element,element,element],[element,element] | ||
| 84 | |||
| 85 | Of the list returned, the first element [0] will always have a blank 'child name' | ||
| 86 | as it is representative of the parent node. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | # Reverse this as due to the way the recursion works, the base element is | ||
| 91 | # returned first. | ||
| 92 | sub paths_list($self) { | ||
| 93 | return reverse @{$self->{paths}}; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 extract | ||
| 97 | |||
| 98 | Return a block of data starting from the point specified. | ||
| 99 | |||
| 100 | Accepts 1 argument; the path in the format of a list ['some_node',3,'element'] | ||
| 101 | |||
| 102 | If you pass a blank or undefined list to the function it will return the entire | ||
| 103 | tree, as it will presume you are selecting the root/base node. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub extract($self,$path = []) { | ||
| 108 | if (ref($path) ne 'ARRAY') { croak 'Passed non-array value to extract' } | ||
| 109 | croak 'Not implemented'; | ||
| 110 | } | ||
| 111 | |||
| 112 | # This is the main worker routing that loops through the data structure | ||
| 113 | sub _digest($self,$tree,$stash = { depth=>0 }) { | ||
| 114 | my $type = ref($tree) ? ref($tree) : 'ELEMENT'; | ||
| 115 | |||
| 116 | my @json_path = (); | ||
| 117 | if ($stash->{path}) { | ||
| 118 | @json_path = @{delete $stash->{path}}; | ||
| 119 | } | ||
| 120 | |||
| 121 | # Handle booleans more nicely, I imagine there are more of these to deal with | ||
| 122 | if ($type =~ m/Bool/i) { | ||
| 123 | $type = 'BOOLEAN'; | ||
| 124 | } | ||
| 125 | |||
| 126 | # Dependant on the type of item recurse over them correctly | ||
| 127 | if ($type eq 'HASH') { | ||
| 128 | $stash->{_count} = scalar(keys %{$tree}); | ||
| 129 | foreach my $child (keys %{$tree}) { | ||
| 130 | my @passed_path = (@json_path,$child); | ||
| 131 | my $depth = $stash->{depth}; | ||
| 132 | $stash->{_data}->{$child} = | ||
| 133 | $self->_digest( | ||
| 134 | $tree->{$child}, | ||
| 135 | { | ||
| 136 | path => [@passed_path], | ||
| 137 | depth => scalar(@passed_path) | ||
| 138 | } | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | elsif ($type eq 'ARRAY') { | ||
| 143 | my $index = 0; | ||
| 144 | $stash->{_count} = scalar(@{$tree}); | ||
| 145 | foreach my $child (@{$tree}) { | ||
| 146 | my @passed_path = (@json_path,$index++); | ||
| 147 | # You can have unnamed HASH elements within an ARRAY | ||
| 148 | # For this we will change the hash-memory-reference to simply HASH | ||
| 149 | if (ref($child) eq 'HASH') { | ||
| 150 | $stash->{_data}->{HASH} = | ||
| 151 | $self->_digest( | ||
| 152 | $child, | ||
| 153 | { | ||
| 154 | path => [@passed_path], | ||
| 155 | depth => scalar(@passed_path) | ||
| 156 | } | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | $stash->{_data}->{$child} = | ||
| 161 | $self->_digest( | ||
| 162 | $child, | ||
| 163 | { | ||
| 164 | path => [@passed_path], | ||
| 165 | depth => scalar(@passed_path) | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # Add in additional details about this node | ||
| 173 | $stash->{_type} = $type; | ||
| 174 | $stash->{_key} = $json_path[-1]; | ||
| 175 | $stash->{_path} = [@json_path]; | ||
| 176 | $stash->{_depth} = delete $stash->{depth}; | ||
| 177 | |||
| 178 | push(@{$self->{paths}},$stash->{_path}); | ||
| 179 | |||
| 180 | return $stash; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | =head1 CORROSPONDANCE | ||
| 185 | |||
| 186 | Regarding bugs, featuire requests, patches, forks or other please view | ||
| 187 | the project on github here L<https://github.com/PaulGWebster/p5-Data-Tree-Describe> | ||
| 188 | |||
| 189 | =head1 AUTHOR | ||
| 190 | |||
| 191 | Paul G Webster <daemon@cpan.org> | ||
| 192 | |||
| 193 | =head1 COPYRIGHT AND LICENSE | ||
| 194 | |||
| 195 | This software is copyright (c) 2020 by Paul G Webster. | ||
| 196 | |||
| 197 | This is free software; you can redistribute it and/or modify it under | ||
| 198 | the same terms as the Perl 5 programming language system itself. | ||
| 199 | |||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | 1; |