| 1 | package Data::CSel::WrapStruct; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-04-15'; # DATE | |||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'Data-CSel-WrapStruct'; # DIST | |||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.008'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 14 | wrap_struct | ||||||||||||||||||||||||||||
| 15 | unwrap_tree | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # convenience | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Data::CSel (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | unshift @Data::CSel::CLASS_PREFIXES, __PACKAGE__ | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | unless grep { $_ eq __PACKAGE__ } @Data::CSel::CLASS_PREFIXES; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 50 | ▶ | sub _wrap { | ||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||
| 24 | 50 | my ($data, $parent, $key_in_parent) = @_; | |||||||||||||||||||||||||||
| 25 | 50 | my $ref = ref($data); | |||||||||||||||||||||||||||
| 26 | 50 | ▶ | if (!$ref) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 27 | 31 | return Data::CSel::WrapStruct::Scalar->new($data, $parent, $key_in_parent); | |||||||||||||||||||||||||||
| 28 | #} elsif (blessed $data) { | ||||||||||||||||||||||||||||
| 29 | } elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 30 | 15 | my $node = Data::CSel::WrapStruct::Array->new($data, $parent); | |||||||||||||||||||||||||||
| 31 | 15 | $node->children([ map { _wrap($data->[$_], $node, $_) } 0..$#{$data}]); | |||||||||||||||||||||||||||
| 32 | 15 | return $node; | |||||||||||||||||||||||||||
| 33 | } elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||
| 34 | 4 | my $node = Data::CSel::WrapStruct::Hash->new($data, $parent); | |||||||||||||||||||||||||||
| 35 | 4 | my @keys = sort keys %$data; | |||||||||||||||||||||||||||
| 36 | 4 | $node->_keys(\@keys); | |||||||||||||||||||||||||||
| 37 | 4 | $node->children([ map { _wrap($data->{$_}, $node, $_) } @keys]); | |||||||||||||||||||||||||||
| 38 | 4 | return $node; | |||||||||||||||||||||||||||
| 39 | } elsif ($ref eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 40 | 0 | return Data::CSel::WrapStruct::ScalarRef->new($data, $parent, undef); | |||||||||||||||||||||||||||
| 41 | } elsif ($ref eq 'JSON::PP::Boolean') { | ||||||||||||||||||||||||||||
| 42 | 0 | return Data::CSel::WrapStruct::Scalar->new($$data, $parent, undef); | |||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | 0 | die "Sorry, currently can't handle ref=$ref"; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | sub wrap_struct { | ||||||||||||||||||||||||||
|
Subroutine
wrap_struct: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 50 | 3 | _wrap($data, undef, undef); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | sub unwrap_tree { | ||||||||||||||||||||||||||
|
Subroutine
unwrap_tree: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 8 | my $tree = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 8 | state $cleaner = do { | |||||||||||||||||||||||||||
| 57 | 1 | require Data::Clean; | |||||||||||||||||||||||||||
| 58 | 1 | Data::Clean->new( | |||||||||||||||||||||||||||
| 59 | '!recurse_obj' => 1, | ||||||||||||||||||||||||||||
| 60 | 'Data::CSel::WrapStruct::Scalar' => [call_method=>'value'], | ||||||||||||||||||||||||||||
| 61 | 'Data::CSel::WrapStruct::ScalarRef' => [call_method=>'value'], | ||||||||||||||||||||||||||||
| 62 | 'Data::CSel::WrapStruct::Array' => [call_method=>'value'], | ||||||||||||||||||||||||||||
| 63 | 'Data::CSel::WrapStruct::Hash' => [call_method=>'value'], | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | $cleaner->clean_in_place($tree); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | package | ||||||||||||||||||||||||||||
| 71 | Data::CSel::WrapStruct::Base; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 50 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 50 | my ($class, $data_ref, $parent, $key_in_parent) = @_; | |||||||||||||||||||||||||||
| 75 | 50 | bless [$data_ref, $parent, $key_in_parent], $class; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 44 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||
| 79 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 44 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | my ($parent, $key_in_parent) = ($self->[1], $self->[2]); | |||||||||||||||||||||||||||
| 82 | 2 | my $new_value = shift; | |||||||||||||||||||||||||||
| 83 | 2 | my $orig_value = $self->[0]; | |||||||||||||||||||||||||||
| 84 | 2 | $self->[0] = $new_value; | |||||||||||||||||||||||||||
| 85 | 2 | ▶ | if (defined $key_in_parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | my $ref_parent = ref $parent->[0]; | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ($ref_parent eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 2 | $parent->[0][$key_in_parent] = $new_value; | |||||||||||||||||||||||||||
| 89 | } elsif ($ref_parent eq 'HASH') { | ||||||||||||||||||||||||||||
| 90 | 0 | $parent->[0]{$key_in_parent} = $new_value; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 0 | warn "Cannot replace value in parent: not array/hash"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 2 | return $new_value; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 42 | $self->[0]; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=7 SCAR=21.8
| |||||||||||||||||||||||||||||
| 101 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 5 | my ($parent, $key_in_parent) = ($self->[1], $self->[2]); | |||||||||||||||||||||||||||
| 103 | 5 | ▶ | if (defined $parent && defined $key_in_parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $parent and defined $key_in_parent
MC/DC0%0/2
defined $parent and defined $key_in_parent
defined $parent
defined $key_in_parent
Truth table33%1/3
A and B
A= defined $parentB= defined $key_in_parent
| |||||||||||||||||||||||||||||
| 104 | 5 | my $ref_parent = ref $parent->[0]; | |||||||||||||||||||||||||||
| 105 | 5 | ▶ | if ($ref_parent eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | 5 | splice @{ $parent->[0] }, $key_in_parent, 1; | |||||||||||||||||||||||||||
| 107 | # shift larger indexes by 1 | ||||||||||||||||||||||||||||
| 108 | 5 | for my $chld (@{ $parent->children }) { | |||||||||||||||||||||||||||
| 109 | 18 | ▶ | $chld->[2]-- if $chld->[2] >= $key_in_parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } elsif ($ref_parent eq 'HASH') { | ||||||||||||||||||||||||||||
| 112 | 0 | delete $parent->[0]{$key_in_parent}; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | warn "Cannot remove node from parent: not array/hash"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 5 | undef; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub parent { | ||||||||||||||||||||||||||
|
Subroutine
parent: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 121 | 0 | $_[0][1]; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | package | ||||||||||||||||||||||||||||
| 125 | Data::CSel::WrapStruct::Scalar; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | our @ISA = qw(Data::CSel::WrapStruct::Base); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 70 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 70 | []; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | package | ||||||||||||||||||||||||||||
| 134 | Data::CSel::WrapStruct::ScalarRef; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | our @ISA = qw(Data::CSel::WrapStruct::Base); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | []; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | package | ||||||||||||||||||||||||||||
| 143 | Data::CSel::WrapStruct::Array; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | our @ISA = qw(Data::CSel::WrapStruct::Base); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 56 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 148 | 56 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 15 | $_[0][2] = $_[1]; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 56 | $_[0][2]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub length { | ||||||||||||||||||||||||||
|
Subroutine
length: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 0 | scalar @{ $_[0][0] }; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | package | ||||||||||||||||||||||||||||
| 159 | Data::CSel::WrapStruct::Hash; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | our @ISA = qw(Data::CSel::WrapStruct::Base); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 4 | ▶ | sub _keys { | ||||||||||||||||||||||||||
|
Subroutine
_keys: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 164 | 4 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 4 | $_[0][2] = $_[1]; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 4 | $_[0][2]; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 14 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 171 | 14 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 4 | $_[0][3] = $_[1]; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 14 | $_[0][3]; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 4 | ▶ | sub length { | ||||||||||||||||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 4 | scalar @{ $_[0][2] }; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub has_key { | ||||||||||||||||||||||||||
|
Subroutine
has_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 182 | 0 | exists $_[0][0]{$_[1]}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub key { | ||||||||||||||||||||||||||
|
Subroutine
key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | $_[0][0]{$_[1]}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 2 | 1; | |||||||||||||||||||||||||||
| 190 | # ABSTRACT: Wrap data structure into a tree of objects suitable for use with Data::CSel | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||