| 1 | package Data::Transmute; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Log::ger; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Ref::Util qw(is_hashref is_arrayref is_plain_hashref is_plain_arrayref); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||
| 14 | 2 | our $DATE = '2024-07-17'; # DATE | |||||||||||||||||||||||||||||||||
| 15 | 2 | our $DIST = 'Data-Transmute'; # DIST | |||||||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.040'; # VERSION | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 2 | our @EXPORT_OK = qw(transmute_data reverse_rules); | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 63 | ▶ | sub _rule_create_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_create_hash_key: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||
| 21 | 63 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 63 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 24 | 63 | ▶ | return unless ($args{transmute_object} // 1) ? is_hashref($data) : is_plain_hashref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$args{"transmute_object"} // 1
MC/DC100%1/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table100%2/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 46 | my $name = $args{name}; | |||||||||||||||||||||||||||||||||
| 27 | 46 | ▶ | die "Rule create_hash_key: Please specify 'name'" unless defined $name; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 45 | ▶ | if (exists $data->{$name}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 30 | 7 | ▶ | return if $args{ignore}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | die "Rule create_hash_key: Key '$name' already exists" unless $args{replace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | die "Rule create_hash_key: Please specify 'value' or 'value_code'" | ||||||||||||||||||||||||||||||||||
| 34 | 43 | ▶ | unless exists $args{value} || $args{value_code}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"value"} or $args{"value_code"}
MC/DC100%2/2
exists $args{"value"} or $args{"value_code"}
exists $args{"value"}
$args{"value_code"}
Truth table100%3/3
A or B
A= exists $args{"value"}B= $args{"value_code"}
| |||||||||||||||||||||||||||||||||||
| 35 | 42 | ▶ | $data->{$name} = $args{value_code} ? $args{value_code}->($data->{$name}) : $args{value}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 30 | ▶ | sub _rulereverse_create_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_create_hash_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 39 | 30 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 40 | 30 | ▶ | die "Cannot generate reverse rule create_hash_key with value_code" if $args{value_code}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 41 | 29 | ▶ | die "Cannot generate reverse rule create_hash_key with ignore=1" if $args{ignore}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 28 | ▶ | die "Cannot generate reverse rule create_hash_key with replace=1" if $args{replace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | 27 | [delete_hash_key => {name=>$args{name}, transmute_object=>$args{transmute_object}}]; | |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 23 | ▶ | sub _rule_rename_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_rename_hash_key: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||
| 47 | 23 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 23 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 50 | 23 | ▶ | return unless ($args{transmute_object} // 1) ? is_hashref($data) : is_plain_hashref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$args{"transmute_object"} // 1
MC/DC0%0/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table50%1/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 21 | my $from = $args{from}; | |||||||||||||||||||||||||||||||||
| 53 | 21 | ▶ | die "Rule rename_hash_key: Please specify 'from'" unless defined $from; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | 20 | my $to = $args{to}; | |||||||||||||||||||||||||||||||||
| 55 | 20 | ▶ | die "Rule rename_hash_key: Please specify 'to'" unless defined $to; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | # noop | ||||||||||||||||||||||||||||||||||
| 58 | 19 | ▶ | return if $from eq $to; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 19 | ▶ | if (!exists($data->{$from})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | die "Rule rename_hash_key: Can't rename '$from' -> '$to': Old key '$from' doesn't exist" unless $args{ignore_missing_from}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | 1 | return; | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | 17 | ▶ | if (exists $data->{$to}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | return if $args{ignore_existing_target}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | die "Rule rename_hash_key: Can't rename '$from' -> '$to': Target key '$from' already exists" unless $args{replace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | 15 | $data->{$to} = delete $data->{$from}; | |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 11 | ▶ | sub _rulereverse_rename_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_rename_hash_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 72 | 11 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 73 | 11 | ▶ | die "Cannot generate reverse rule rename_hash_key with ignore_missing_from=1" if $args{ignore_missing_from}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | die "Cannot generate reverse rule rename_hash_key with ignore_existing_target=1" if $args{ignore_existing_target}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 9 | ▶ | die "Cannot generate reverse rule rename_hash_key with replace=1" if $args{replace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | [rename_hash_key => { | ||||||||||||||||||||||||||||||||||
| 77 | from=>$args{to}, to=>$args{from}, | ||||||||||||||||||||||||||||||||||
| 78 | transmute_object=>$args{transmute_object}, | ||||||||||||||||||||||||||||||||||
| 79 | 8 | }]; | |||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 13 | ▶ | sub _rule_modify_hash_value { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_modify_hash_value: called CC=17 SCAR=28.6
| |||||||||||||||||||||||||||||||||||
| 83 | 13 | require Data::Cmp; | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 13 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 13 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 88 | 13 | ▶ | return unless ($args{transmute_object} // 1) ? is_hashref($data) : is_plain_hashref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$args{"transmute_object"} // 1
MC/DC100%1/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table100%2/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 9 | my $name = $args{name}; | |||||||||||||||||||||||||||||||||
| 91 | 9 | ▶ | die "Rule modify_hash_value: Please specify 'name' (key)" unless defined $name; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 8 | my $from = $args{from}; | |||||||||||||||||||||||||||||||||
| 93 | 8 | my $from_exists = exists $args{from}; | |||||||||||||||||||||||||||||||||
| 94 | 8 | my $to = $args{to}; | |||||||||||||||||||||||||||||||||
| 95 | die "Rule rename_hash_key: Please specify 'to' or 'to_code'" | ||||||||||||||||||||||||||||||||||
| 96 | 8 | ▶ | unless exists $args{to} || $args{to_code}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"to"} or $args{"to_code"}
MC/DC0%0/2
exists $args{"to"} or $args{"to_code"}
exists $args{"to"}
$args{"to_code"}
Truth table66%2/3
A or B
A= exists $args{"to"}B= $args{"to_code"}
| |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | my $errprefix = "Rule modify_hash_value: Can't modify key '$name'". | ||||||||||||||||||||||||||||||||||
| 99 | ($from_exists ? " from '".($from // '<undef>') : ""). | ||||||||||||||||||||||||||||||||||
| 100 | 8 | ▶ | ($args{to_code} ? "' using to_code" : "' to '".($to // '<undef>')."'"); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%2/4
$from // "<undef>"
$to // "<undef>"
MC/DC0%0/1
$from // "<undef>"
$from
MC/DC0%0/1
$to // "<undef>"
$to
Truth table50%1/2
A
A= $from
Truth table50%1/2
A
A= $to
| |||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | unless (exists $data->{$name}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 1 | die "$errprefix: key does not exist"; | |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 7 | my $cur = $data->{$name}; | |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 7 | ▶ | $to = $args{to_code}->($cur) if $args{to_code}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 7 | ▶ | if ($from_exists) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | # noop | ||||||||||||||||||||||||||||||||||
| 112 | 6 | ▶ | return unless Data::Cmp::cmp_data($from, $to); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | if (Data::Cmp::cmp_data($cur, $from)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | die "$errprefix: current value is not '".($cur // '<undef>')."'"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cur // "<undef>"
MC/DC0%0/1
$cur // "<undef>"
$cur
Truth table50%1/2
A
A= $cur
| |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 6 | $data->{$name} = $to; | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | sub _rulereverse_modify_hash_value { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_modify_hash_value: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 123 | 6 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 124 | 6 | ▶ | die "Cannot generate reverse rule modify_hash_value without from" unless exists $args{from}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 125 | 5 | ▶ | die "Cannot generate reverse rule modify_hash_value with to_code" if $args{to_code}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | [modify_hash_value => { | ||||||||||||||||||||||||||||||||||
| 127 | name => $args{name}, from => $args{to}, to => $args{from}, | ||||||||||||||||||||||||||||||||||
| 128 | transmute_object=>$args{transmute_object}, | ||||||||||||||||||||||||||||||||||
| 129 | 4 | }]; | |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 43 | ▶ | sub _rule_delete_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_delete_hash_key: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 133 | 43 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | 43 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 136 | 43 | ▶ | return unless ($args{transmute_object} // 1) ? is_hashref($data) : is_plain_hashref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$args{"transmute_object"} // 1
MC/DC100%1/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table100%2/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 39 | my $name = $args{name}; | |||||||||||||||||||||||||||||||||
| 139 | 39 | ▶ | die "Rule delete_hash_key: Please specify 'name'" unless defined $name; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 38 | delete $data->{$name}; | |||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 5 | ▶ | sub _rulereverse_delete_hash_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_delete_hash_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 145 | 5 | die "Can't create reverse rule for delete_hash_key"; | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 19 | ▶ | sub _rule_transmute_array_elems { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_transmute_array_elems: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||
| 149 | 19 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 19 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 152 | 19 | ▶ | return unless ($args{transmute_object} //1) ? is_arrayref($data) : is_plain_arrayref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$args{"transmute_object"} // 1
MC/DC100%1/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table100%2/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | die "Rule transmute_array_elems: Please specify 'rules' or 'rules_module'" | ||||||||||||||||||||||||||||||||||
| 155 | 15 | ▶ | unless defined($args{rules}) || defined($args{rules_module}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"rules"} or defined $args{"rules_module"}
MC/DC100%2/2
defined $args{"rules"} or defined $args{"rules_module"}
defined $args{"rules"}
defined $args{"rules_module"}
Truth table100%3/3
A or B
A= defined $args{"rules"}B= defined $args{"rules_module"}
| |||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 14 | my $idx = -1; | |||||||||||||||||||||||||||||||||
| 158 | ELEM: | ||||||||||||||||||||||||||||||||||
| 159 | 14 | for my $el (@$data) { | |||||||||||||||||||||||||||||||||
| 160 | 38 | $idx++; | |||||||||||||||||||||||||||||||||
| 161 | 38 | ▶ | if (defined $args{index_is}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 162 | 6 | ▶ | next ELEM unless $idx == $args{index_is}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||
| 164 | 34 | ▶ | if (defined $args{index_in}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | 6 | ▶ | next ELEM unless grep { $idx == $_ } @{ $args{index_in} }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||
| 167 | 32 | ▶ | if (defined $args{index_match}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 168 | 6 | ▶ | next ELEM unless $idx =~ $args{index_match}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||
| 170 | 30 | ▶ | if (defined $args{index_filter}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 171 | 6 | ▶ | next ELEM unless $args{index_filter}->(index=>$idx, array=>$data, rules=>$args{rules}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | $el = transmute_data( | ||||||||||||||||||||||||||||||||||
| 174 | data => $el, | ||||||||||||||||||||||||||||||||||
| 175 | (rules => $args{rules}) x !!(exists $args{rules}), | ||||||||||||||||||||||||||||||||||
| 176 | 28 | (rules_module => $args{rules_module}) x !!(exists $args{rules_module}), | |||||||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||
| 179 | 14 | $data; | |||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | 9 | ▶ | sub _rulereverse_transmute_array_elems { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_transmute_array_elems: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 183 | 9 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | [transmute_array_elems => { | ||||||||||||||||||||||||||||||||||
| 186 | rules => reverse_rules( | ||||||||||||||||||||||||||||||||||
| 187 | (rules => $args{rules}) x !!(exists $args{rules}), | ||||||||||||||||||||||||||||||||||
| 188 | (rules_module => $args{rules_module}) x !!(exists $args{rules_module}), | ||||||||||||||||||||||||||||||||||
| 189 | ), | ||||||||||||||||||||||||||||||||||
| 190 | (index_is => $args{index_is}) x !!(exists $args{index_is}), | ||||||||||||||||||||||||||||||||||
| 191 | (index_in => $args{index_in}) x !!(exists $args{index_in}), | ||||||||||||||||||||||||||||||||||
| 192 | (index_match => $args{index_match}) x !!(exists $args{index_match}), | ||||||||||||||||||||||||||||||||||
| 193 | (index_filter => $args{index_filter}) x !!(exists $args{index_filter}), | ||||||||||||||||||||||||||||||||||
| 194 | transmute_object=>$args{transmute_object}, | ||||||||||||||||||||||||||||||||||
| 195 | 9 | }]; | |||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | 19 | ▶ | sub _rule_transmute_hash_values { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_transmute_hash_values: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||
| 199 | 19 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 19 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 202 | 19 | ▶ | return unless ($args{transmute_object} //1) ? is_hashref($data) : is_plain_hashref($data); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$args{"transmute_object"} // 1
MC/DC100%1/1
$args{"transmute_object"} // 1
$args{"transmute_object"}
Truth table100%2/2
A
A= $args{"transmute_object"}
| |||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | die "Rule transmute_hash_values: Please specify 'rules' or 'rules_module'" | ||||||||||||||||||||||||||||||||||
| 205 | 15 | ▶ | unless defined($args{rules}) || defined($args{rules_module}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"rules"} or defined $args{"rules_module"}
MC/DC100%2/2
defined $args{"rules"} or defined $args{"rules_module"}
defined $args{"rules"}
defined $args{"rules_module"}
Truth table100%3/3
A or B
A= defined $args{"rules"}B= defined $args{"rules_module"}
| |||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||
| 207 | KEY: | ||||||||||||||||||||||||||||||||||
| 208 | 14 | for my $key (keys %$data) { | |||||||||||||||||||||||||||||||||
| 209 | 38 | ▶ | if (defined $args{key_is}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 210 | 6 | ▶ | next KEY unless $key eq $args{key_is}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||
| 212 | 34 | ▶ | if (defined $args{key_in}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 213 | 6 | ▶ | next KEY unless grep { $key eq $_ } @{ $args{key_in} }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||
| 215 | 32 | ▶ | if (defined $args{key_match}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 216 | 6 | ▶ | next KEY unless $key =~ $args{key_match}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||
| 218 | 30 | ▶ | if (defined $args{key_filter}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 219 | 6 | ▶ | next KEY unless $args{key_filter}->(key=>$key, hash=>$data, rules=>$args{rules}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||
| 221 | $data->{$key} = transmute_data( | ||||||||||||||||||||||||||||||||||
| 222 | data => $data->{$key}, | ||||||||||||||||||||||||||||||||||
| 223 | (rules => $args{rules}) x !!(exists $args{rules}), | ||||||||||||||||||||||||||||||||||
| 224 | 28 | (rules_module => $args{rules_module}) x !!(exists $args{rules_module}), | |||||||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | 14 | $data; | |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | 9 | ▶ | sub _rulereverse_transmute_hash_values { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_transmute_hash_values: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 231 | 9 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | [transmute_hash_values => { | ||||||||||||||||||||||||||||||||||
| 234 | rules => reverse_rules( | ||||||||||||||||||||||||||||||||||
| 235 | (rules => $args{rules}) x !!(exists $args{rules}), | ||||||||||||||||||||||||||||||||||
| 236 | (rules_module => $args{rules_module}) x !!(exists $args{rules_module}), | ||||||||||||||||||||||||||||||||||
| 237 | ), | ||||||||||||||||||||||||||||||||||
| 238 | (key_is => $args{key_is}) x !!(exists $args{key_is}), | ||||||||||||||||||||||||||||||||||
| 239 | (key_in => $args{key_in}) x !!(exists $args{key_in}), | ||||||||||||||||||||||||||||||||||
| 240 | (key_match => $args{key_match}) x !!(exists $args{key_match}), | ||||||||||||||||||||||||||||||||||
| 241 | (key_filter => $args{key_filter}) x !!(exists $args{key_filter}), | ||||||||||||||||||||||||||||||||||
| 242 | transmute_object=>$args{transmute_object}, | ||||||||||||||||||||||||||||||||||
| 243 | 9 | }]; | |||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | 20 | ▶ | sub _walk { | ||||||||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||
| 247 | 20 | my ($data, $rule_args, $seen) = @_; | |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | # transmute the node itself | ||||||||||||||||||||||||||||||||||
| 250 | transmute_data( | ||||||||||||||||||||||||||||||||||
| 251 | data => $data, | ||||||||||||||||||||||||||||||||||
| 252 | (rules => $rule_args->{rules}) x !!(exists $rule_args->{rules}), | ||||||||||||||||||||||||||||||||||
| 253 | 20 | (rules_module => $rule_args->{rules_module}) x !!(exists $rule_args->{rules_module}), | |||||||||||||||||||||||||||||||||
| 254 | ); | ||||||||||||||||||||||||||||||||||
| 255 | 20 | my $refaddr = refaddr($data); | |||||||||||||||||||||||||||||||||
| 256 | 20 | ▶ | return unless $refaddr; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 257 | 11 | ▶ | return if $seen->{$refaddr}++; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||
| 259 | 11 | ▶ | if ($rule_args->{recurse_object} ? | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 260 | is_arrayref($data) : is_plain_arrayref($data)) { | ||||||||||||||||||||||||||||||||||
| 261 | 5 | for my $elem (@$data) { | |||||||||||||||||||||||||||||||||
| 262 | 4 | _walk($elem, $rule_args, $seen); | |||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||
| 264 | ▶ | } elsif ($rule_args->{recurse_object} ? | |||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 265 | is_hashref($data) : is_plain_hashref($data)) { | ||||||||||||||||||||||||||||||||||
| 266 | 5 | for my $key (sort keys %$data) { | |||||||||||||||||||||||||||||||||
| 267 | 13 | _walk($data->{$key}, $rule_args, $seen); | |||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | 3 | ▶ | sub _rule_transmute_nodes { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rule_transmute_nodes: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||||||||
| 273 | 3 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | 3 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||
| 277 | die "Rule transmute_nodes: Please specify 'rules' or 'rules_module'" | ||||||||||||||||||||||||||||||||||
| 278 | 3 | ▶ | unless defined($args{rules}) || defined($args{rules_module}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args{"rules"} or defined $args{"rules_module"}
MC/DC0%0/2
defined $args{"rules"} or defined $args{"rules_module"}
defined $args{"rules"}
defined $args{"rules_module"}
Truth table33%1/3
A or B
A= defined $args{"rules"}B= defined $args{"rules_module"}
| |||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||
| 280 | 3 | my $seen = {}; | |||||||||||||||||||||||||||||||||
| 281 | 3 | _walk($data, \%args, $seen); | |||||||||||||||||||||||||||||||||
| 282 | 3 | $data; | |||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | sub _rulereverse_transmute_nodes { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rulereverse_transmute_nodes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 286 | 3 | die "Rule transmute_nodes is not reversible"; | |||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||
| 289 | 237 | ▶ | sub _rules_or_rules_module { | ||||||||||||||||||||||||||||||||
|
Subroutine
_rules_or_rules_module: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 290 | 237 | my $args = shift; | |||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | 237 | my $rules = $args->{rules}; | |||||||||||||||||||||||||||||||||
| 293 | 237 | ▶ | if (!$rules) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 294 | 7 | ▶ | if (defined $args->{rules_module}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 295 | 6 | my $mod = "Data::Transmute::Rules::$args->{rules_module}"; | |||||||||||||||||||||||||||||||||
| 296 | 6 | (my $mod_pm = "$mod.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||||||||
| 297 | 6 | require $mod_pm; | |||||||||||||||||||||||||||||||||
| 298 | 6 | $rules = \@{"$mod\::RULES"}; | |||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||
| 301 | 237 | ▶ | $rules or die "Please specify rules (or rules_module)"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 302 | 236 | $rules; | |||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | 172 | ▶ | sub transmute_data { | ||||||||||||||||||||||||||||||||
|
Subroutine
transmute_data: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 306 | 172 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 172 | ▶ | exists $args{data} or die "Please specify data"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 309 | 171 | my $data = $args{data}; | |||||||||||||||||||||||||||||||||
| 310 | 171 | my $rules = _rules_or_rules_module(\%args); | |||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||
| 312 | 170 | my $rulenum = 0; | |||||||||||||||||||||||||||||||||
| 313 | 170 | for my $rule (@$rules) { | |||||||||||||||||||||||||||||||||
| 314 | 184 | $rulenum++; | |||||||||||||||||||||||||||||||||
| 315 | 184 | ▶ | if ($ENV{LOG_DATA_TRANSMUTE_STEP}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 316 | 0 | log_trace "transmute_data #%d/%d: %s", | |||||||||||||||||||||||||||||||||
| 317 | $rulenum, scalar(@$rules), $rule; | ||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||
| 319 | 184 | my $funcname = "_rule_$rule->[0]"; | |||||||||||||||||||||||||||||||||
| 320 | die "rule #$rulenum: Unknown function '$rule->[0]'" | ||||||||||||||||||||||||||||||||||
| 321 | 184 | ▶ | unless defined &{$funcname}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 322 | 183 | my $func = \&{$funcname}; | |||||||||||||||||||||||||||||||||
| 323 | $func->( | ||||||||||||||||||||||||||||||||||
| 324 | 183 | ▶ | %{$rule->[1] // {}}, | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rule->[1] // {}
MC/DC0%0/1
$rule->[1] // {}
$rule->[1]
Truth table50%1/2
A
A= $rule->[1]
| |||||||||||||||||||||||||||||||||||
| 325 | data => $data, | ||||||||||||||||||||||||||||||||||
| 326 | ); | ||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||
| 328 | 156 | $data; | |||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||
| 331 | 66 | ▶ | sub reverse_rules { | ||||||||||||||||||||||||||||||||
|
Subroutine
reverse_rules: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 332 | 66 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 66 | my $rules = _rules_or_rules_module(\%args); | |||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||
| 336 | 66 | my @rev_rules; | |||||||||||||||||||||||||||||||||
| 337 | 66 | for my $rule (@$rules) { | |||||||||||||||||||||||||||||||||
| 338 | 73 | my $funcname = "_rulereverse_$rule->[0]"; | |||||||||||||||||||||||||||||||||
| 339 | 73 | my $func = \&{$funcname}; | |||||||||||||||||||||||||||||||||
| 340 | unshift @rev_rules, $func->( | ||||||||||||||||||||||||||||||||||
| 341 | 73 | ▶ | %{$rule->[1] // {}}, | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rule->[1] // {}
MC/DC0%0/1
$rule->[1] // {}
$rule->[1]
Truth table50%1/2
A
A= $rule->[1]
| |||||||||||||||||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||
| 344 | 50 | \@rev_rules; | |||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||
| 347 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 348 | # ABSTRACT: Transmute (transform) data structure using rules data | ||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||
| 350 | __END__ | ||||||||||||||||||||||||||||||||||