| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::flatten; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = "4.0.24"; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 840 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 56 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 9 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 1422 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | 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 | 3 |  |  | 3 | 0 | 7 | my $this = shift; | 
| 24 | 3 |  |  |  |  | 6 | my $args = shift; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 3 |  |  |  |  | 5 | my @fields; | 
| 28 | 3 |  |  |  |  | 4 | my $default_depth = 1; | 
| 29 | 3 |  |  |  |  | 6 | my $separator     = '-'; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | my $add_field = sub { | 
| 33 | 3 |  |  | 3 |  | 9 | my ($depth, $field_names) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 3 |  |  |  |  | 23 | my $key_groups = App::RecordStream::KeyGroups->new(); | 
| 36 | 3 |  |  |  |  | 11 | $key_groups->add_groups($field_names); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 3 |  |  |  |  | 12 | push @fields, [$depth, $key_groups]; | 
| 39 | 3 |  |  |  |  | 20 | }; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | my $spec = { | 
| 42 | 27 |  |  |  |  | 95 | (map { ($_ . "=s") => $add_field } (1..9)), | 
| 43 |  |  |  |  |  |  | "depth=i"          => \$default_depth, | 
| 44 | 2 |  |  | 2 |  | 2851 | "key|k|field|f=s"  => sub { $add_field->($default_depth, $_[1]); }, | 
| 45 | 1 |  |  | 1 |  | 1253 | "deep=s"           => sub { $add_field->(-1, $_[1]); }, | 
| 46 | 3 |  |  |  |  | 8 | "separator=s"      => \$separator, | 
| 47 |  |  |  |  |  |  | }; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 3 |  |  |  |  | 22 | $this->parse_options($args, $spec); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 3 |  |  |  |  | 9 | $this->{'FIELDS'}        = \@fields; | 
| 52 | 3 |  |  |  |  | 7 | $this->{'SEPARATOR'}     = $separator; | 
| 53 | 3 |  |  |  |  | 41 | $this->{'DEFAULT_DEPTH'} = $default_depth; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub accept_record { | 
| 57 | 9 |  |  | 9 | 0 | 14 | my $this   = shift; | 
| 58 | 9 |  |  |  |  | 12 | my $record = shift; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 9 |  |  |  |  | 15 | my $fields    = $this->{'FIELDS'}; | 
| 61 | 9 |  |  |  |  | 13 | my $separator = $this->{'SEPARATOR'}; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 9 |  |  |  |  | 28 | foreach my $pair (@$fields) { | 
| 64 | 9 |  |  |  |  | 68 | my ($depth, $key_groups) = @$pair; | 
| 65 | 9 |  |  |  |  | 11 | foreach my $spec (@{$key_groups->get_keyspecs($record)}) { | 
|  | 9 |  |  |  |  | 23 |  | 
| 66 | 9 |  |  |  |  | 15 | eval { | 
| 67 | 9 |  |  |  |  | 21 | my $value = $this->remove_spec($record, $spec); | 
| 68 | 9 |  |  |  |  | 22 | $this->split_field($record, $spec, $depth, $value); | 
| 69 |  |  |  |  |  |  | }; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 9 | 50 |  |  |  | 30 | if ( $@ =~ m/Cannot flatten into/ ) { | 
|  |  | 50 |  |  |  |  |  | 
| 72 | 0 |  |  |  |  | 0 | warn $@; | 
| 73 | 0 |  |  |  |  | 0 | undef $@; | 
| 74 | 0 |  |  |  |  | 0 | next; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | elsif ( $@ ) { | 
| 77 | 0 |  |  |  |  | 0 | die $@; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 9 |  |  |  |  | 38 | $this->push_record($record); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 9 |  |  |  |  | 30 | return 1; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub remove_spec { | 
| 88 | 9 |  |  | 9 | 0 | 22 | my ($this, $record, $spec) = @_; | 
| 89 | 9 |  |  |  |  | 21 | my $key_list = $record->get_key_list_for_spec($spec); | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 9 |  |  |  |  | 17 | my $last_key = pop @$key_list; | 
| 92 | 9 |  |  |  |  | 21 | my $new_spec = join('/', @$key_list); | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 9 |  |  |  |  | 12 | my $data = $record; | 
| 95 | 9 | 50 |  |  |  | 19 | if ($new_spec) { | 
| 96 | 0 |  |  |  |  | 0 | $data = ${$record->guess_key_from_spec($new_spec, 1)}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 9 |  |  |  |  | 17 | my $ref_type = ref($data); | 
| 100 | 9 | 50 |  |  |  | 17 | if ( ! grep { $_ eq $ref_type } @$INVALID_REF_TYPES ) { | 
|  | 90 |  |  |  |  | 153 |  | 
| 101 | 9 |  |  |  |  | 25 | return delete $data->{$last_key}; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | else { | 
| 104 | 0 |  |  |  |  | 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 |  |  | 24 | 0 | 46 | my ($this, $record, $name, $depth, $value) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 24 |  |  |  |  | 34 | my $separator = $this->{'SEPARATOR'}; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 24 | 100 | 100 |  |  | 79 | if($depth != 0 && ref($value) eq "ARRAY") { | 
| 114 | 3 |  |  |  |  | 11 | for(my $i = 0; $i < @$value; ++$i) { | 
| 115 | 6 |  |  |  |  | 22 | $this->split_field($record, $name . $separator . $i, $depth - 1, $value->[$i]); | 
| 116 |  |  |  |  |  |  | } | 
| 117 | 3 |  |  |  |  | 10 | return; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 21 | 100 | 100 |  |  | 59 | if($depth != 0 && ref($value) eq "HASH") { | 
| 121 | 9 |  |  |  |  | 23 | for my $key (keys(%$value)) { | 
| 122 | 9 |  |  |  |  | 32 | $this->split_field($record, $name . $separator . $key, $depth - 1, $value->{$key}); | 
| 123 |  |  |  |  |  |  | } | 
| 124 | 9 |  |  |  |  | 25 | return; | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | # either depth is 0 or it wasn't expandable anyway | 
| 128 | 12 |  |  |  |  | 17 | ${$record->guess_key_from_spec($name)} = $value; | 
|  | 12 |  |  |  |  | 31 |  | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub add_help_types { | 
| 132 | 3 |  |  | 3 | 0 | 6 | my $this = shift; | 
| 133 | 3 |  |  |  |  | 14 | $this->use_help_type('keyspecs'); | 
| 134 | 3 |  |  |  |  | 10 | $this->use_help_type('keygroups'); | 
| 135 | 3 |  |  |  |  | 7 | $this->use_help_type('keys'); | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub usage { | 
| 139 | 0 |  |  | 0 | 0 |  | my $this = shift; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 |  |  |  |  |  | my $options = [ | 
| 142 |  |  |  |  |  |  | [ ' ', 'For this comma-separated list of fields flatten to depth n (1-9).'], | 
| 143 |  |  |  |  |  |  | [ 'depth ', 'Change the default depth, negative being arbitrary depth (defaults to 1).'], | 
| 144 |  |  |  |  |  |  | [ 'key ', 'For this comma-separated list of fields flatten to the default depth (may NOT be a a key spec).'], | 
| 145 |  |  |  |  |  |  | [ 'deep ', 'For this comma-separated list of fields flatten to arbitrary depth.'], | 
| 146 |  |  |  |  |  |  | [ 'separator ', 'Use this string to separate joined field names (defaults to "-").'], | 
| 147 |  |  |  |  |  |  | ]; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | my $args_string = $this->options_string($options); | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | return < | 
| 152 |  |  |  |  |  |  | Usage: recs-flatten  [] | 
| 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 |  |  |  |  |  |  | 1; |