« blib/lib/App/RecordStream/Operation/chain.pm
↑ Summary
blib/lib/App/RecordStream/Operation/normalizetime.pm »
| 1 | package App::RecordStream::Operation::flatten; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = "4.0.23"; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | my $INVALID_REF_TYPES = [qw( | |||||||||||||||||||||||||||
| 10 | SCALAR | ||||||||||||||||||||||||||||
| 11 | ARRAY | ||||||||||||||||||||||||||||
| 12 | CODE | ||||||||||||||||||||||||||||
| 13 | REF | ||||||||||||||||||||||||||||
| 14 | GLOB | ||||||||||||||||||||||||||||
| 15 | LVALUE | ||||||||||||||||||||||||||||
| 16 | FORMAT | ||||||||||||||||||||||||||||
| 17 | IO | ||||||||||||||||||||||||||||
| 18 | VSTRING | ||||||||||||||||||||||||||||
| 19 | Regexp | ||||||||||||||||||||||||||||
| 20 | )]; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub init { | ||||||||||||||||||||||||||||
| 23 | 4 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 4 | my $args = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | my @fields; | |||||||||||||||||||||||||||
| 28 | 4 | my $default_depth = 1; | |||||||||||||||||||||||||||
| 29 | 4 | my $separator = '-'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $add_field = sub { | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | my ($depth, $field_names) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | my $key_groups = App::RecordStream::KeyGroups->new(); | |||||||||||||||||||||||||||
| 36 | 3 | $key_groups->add_groups($field_names); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | push @fields, [$depth, $key_groups]; | |||||||||||||||||||||||||||
| 39 | 4 | }; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $spec = { | ||||||||||||||||||||||||||||
| 42 | 36 | (map { ($_ . "=s") => $add_field } (1..9)), | |||||||||||||||||||||||||||
| 43 | "depth=i" => \$default_depth, | ||||||||||||||||||||||||||||
| 44 | 2 | ▶ | "key|k|field|f=s" => sub { $add_field->($default_depth, $_[1]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | "deep=s" => sub { $add_field->(-1, $_[1]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 4 | "separator=s" => \$separator, | |||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | $this->parse_options($args, $spec); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | $this->{'FIELDS'} = \@fields; | |||||||||||||||||||||||||||
| 52 | 3 | $this->{'SEPARATOR'} = $separator; | |||||||||||||||||||||||||||
| 53 | 3 | $this->{'DEFAULT_DEPTH'} = $default_depth; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub accept_record { | ||||||||||||||||||||||||||||
| 57 | 9 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
accept_record: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 9 | my $record = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | my $fields = $this->{'FIELDS'}; | |||||||||||||||||||||||||||
| 61 | 9 | my $separator = $this->{'SEPARATOR'}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 9 | foreach my $pair (@$fields) { | |||||||||||||||||||||||||||
| 64 | 9 | my ($depth, $key_groups) = @$pair; | |||||||||||||||||||||||||||
| 65 | 9 | foreach my $spec (@{$key_groups->get_keyspecs($record)}) { | |||||||||||||||||||||||||||
| 66 | 9 | eval { | |||||||||||||||||||||||||||
| 67 | 9 | my $value = $this->remove_spec($record, $spec); | |||||||||||||||||||||||||||
| 68 | 9 | $this->split_field($record, $spec, $depth, $value); | |||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 9 | ▶ | if ( $@ =~ m/Cannot flatten into/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 0 | warn $@; | |||||||||||||||||||||||||||
| 73 | 0 | undef $@; | |||||||||||||||||||||||||||
| 74 | 0 | next; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | elsif ( $@ ) { | ||||||||||||||||||||||||||||
| 77 | 0 | die $@; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 9 | $this->push_record($record); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | return 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub remove_spec { | ||||||||||||||||||||||||||||
| 88 | 9 | ▶ | my ($this, $record, $spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_spec: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 9 | my $key_list = $record->get_key_list_for_spec($spec); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 9 | my $last_key = pop @$key_list; | |||||||||||||||||||||||||||
| 92 | 9 | my $new_spec = join('/', @$key_list); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 9 | my $data = $record; | |||||||||||||||||||||||||||
| 95 | 9 | ▶ | if ($new_spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $data = ${$record->guess_key_from_spec($new_spec, 1)}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 9 | my $ref_type = ref($data); | |||||||||||||||||||||||||||
| 100 | 9 | ▶ | if ( ! grep { $_ eq $ref_type } @$INVALID_REF_TYPES ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 9 | return delete $data->{$last_key}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 0 | die "Cannot flatten into ref type: '$ref_type', must be a hash! skipping spec $spec!\n"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub split_field { | ||||||||||||||||||||||||||||
| 109 | 24 | ▶ | my ($this, $record, $name, $depth, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
split_field: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 24 | my $separator = $this->{'SEPARATOR'}; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 24 | ▶ | if($depth != 0 && ref($value) eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$depth != 0 and ref $value eq "ARRAY"
MC/DC100%2/2
$depth != 0 and ref $value eq "ARRAY"
$depth != 0
ref $value eq "ARRAY"
Truth table100%3/3
A and B
A= $depth != 0B= ref $value eq "ARRAY"
| |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | for(my $i = 0; $i < @$value; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 6 | $this->split_field($record, $name . $separator . $i, $depth - 1, $value->[$i]); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 3 | return; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 21 | ▶ | if($depth != 0 && ref($value) eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$depth != 0 and ref $value eq "HASH"
MC/DC100%2/2
$depth != 0 and ref $value eq "HASH"
$depth != 0
ref $value eq "HASH"
Truth table100%3/3
A and B
A= $depth != 0B= ref $value eq "HASH"
| |||||||||||||||||||||||||||||
| 121 | 9 | for my $key (keys(%$value)) { | |||||||||||||||||||||||||||
| 122 | 9 | $this->split_field($record, $name . $separator . $key, $depth - 1, $value->{$key}); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 9 | return; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # either depth is 0 or it wasn't expandable anyway | ||||||||||||||||||||||||||||
| 128 | 12 | ${$record->guess_key_from_spec($name)} = $value; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub add_help_types { | ||||||||||||||||||||||||||||
| 132 | 4 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 4 | $this->use_help_type('keyspecs'); | |||||||||||||||||||||||||||
| 134 | 4 | $this->use_help_type('keygroups'); | |||||||||||||||||||||||||||
| 135 | 4 | $this->use_help_type('keys'); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub usage { | ||||||||||||||||||||||||||||
| 139 | 1 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | my $options = [ | |||||||||||||||||||||||||||
| 142 | [ '<n> <fields>', 'For this comma-separated list of fields flatten to depth n (1-9).'], | ||||||||||||||||||||||||||||
| 143 | [ 'depth <nbr>', 'Change the default depth, negative being arbitrary depth (defaults to 1).'], | ||||||||||||||||||||||||||||
| 144 | [ 'key <fields>', 'For this comma-separated list of fields flatten to the default depth (may NOT be a a key spec).'], | ||||||||||||||||||||||||||||
| 145 | [ 'deep <fields>', 'For this comma-separated list of fields flatten to arbitrary depth.'], | ||||||||||||||||||||||||||||
| 146 | [ 'separator <string>', 'Use this string to separate joined field names (defaults to "-").'], | ||||||||||||||||||||||||||||
| 147 | ]; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | my $args_string = $this->options_string($options); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | return <<USAGE; | |||||||||||||||||||||||||||
| 152 | Usage: recs-flatten <args> [<files>] | ||||||||||||||||||||||||||||
| 153 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 154 | Flatten nested structures in records. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | NOTE: This script implements a strategy for dealing with nested structures | ||||||||||||||||||||||||||||
| 157 | that is almost always better handled by using keyspecs or keygroups. It | ||||||||||||||||||||||||||||
| 158 | should, in general, be as easy or easier to use those concepts with the data | ||||||||||||||||||||||||||||
| 159 | manipulations you actually want to accomplish. | ||||||||||||||||||||||||||||
| 160 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Arguments: | ||||||||||||||||||||||||||||
| 163 | $args_string | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 166 | All field values may be keyspecs or keygroups, value of keyspec must not be | ||||||||||||||||||||||||||||
| 167 | an array element | ||||||||||||||||||||||||||||
| 168 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Examples: | ||||||||||||||||||||||||||||
| 171 | Under | ||||||||||||||||||||||||||||
| 172 | recs-flatten -1 field | ||||||||||||||||||||||||||||
| 173 | We see | ||||||||||||||||||||||||||||
| 174 | {"field" => "value"} becomes {"field" => "value"} | ||||||||||||||||||||||||||||
| 175 | {"field" => {"subfield" => "value"}} becomes {"field-subfield" => "value"} | ||||||||||||||||||||||||||||
| 176 | {"field" => ["value1", "value2"]} becomes {"field-0" => "value1", "field-1" => "value2"} | ||||||||||||||||||||||||||||
| 177 | {"field" => {"subfield" => [0, 1]}} becomes {"field-subfield" => [0, 1]}} | ||||||||||||||||||||||||||||
| 178 | Under | ||||||||||||||||||||||||||||
| 179 | recs-flatten --deep x | ||||||||||||||||||||||||||||
| 180 | We see | ||||||||||||||||||||||||||||
| 181 | {"x" => {"y" => [{"z" = "v"}]}} becomes {"x-y-0-z" => "v"} | ||||||||||||||||||||||||||||
| 182 | USAGE | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | 1; | |||||||||||||||||||||||||||