File Coverage

blib/lib/App/RecordStream/Operation/stream2table.pm
Criterion Covered Total %
statement 43 47 91.4
branch 6 8 75.0
condition 2 2 100.0
subroutine 7 8 87.5
pod 0 5 0.0
total 58 70 82.8


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::stream2table;
2              
3 2     2   838 use strict;
  2         4  
  2         47  
4 2     2   8 use warnings;
  2         5  
  2         44  
5              
6 2     2   8 use base qw(App::RecordStream::Operation);
  2         5  
  2         751  
7              
8             sub init {
9 1     1 0 2 my $this = shift;
10 1         2 my $args = shift;
11              
12 1         2 my $field;
13 1         3 my $spec = {
14             "field|f=s" => \$field,
15             };
16              
17 1         6 $this->parse_options($args, $spec);
18              
19 1 50       4 die "You must specify a --field option\n" unless defined($field);
20              
21 1         3 $this->{'FIELD'} = $field;
22 1         9 $this->{'REMOVE_FIELD'} = (not ($field =~ m![/@]!));
23             }
24              
25             sub accept_record {
26 4     4 0 6 my $this = shift;
27 4         8 my $record = shift;
28              
29 4         4 my $key = ${$record->guess_key_from_spec($this->{'FIELD'})};
  4         14  
30              
31 4 50       14 if ( $this->{'REMOVE_FIELD'} ) {
32 4         13 $record->remove($this->{'FIELD'});
33             }
34              
35 4   100     18 $this->{'HASH'}->{$key} ||= [];
36 4         7 push @{$this->{'HASH'}->{$key}}, $record;
  4         9  
37              
38 4         19 return 1;
39             }
40              
41             sub stream_done {
42 1     1 0 2 my $this = shift;
43              
44 1         2 while(scalar keys %{$this->{'HASH'}} > 0) {
  4         21  
45 3         10 my $record = App::RecordStream::Record->new();
46 3         4 my $found = 0;
47 3         7 foreach my $key (keys %{$this->{'HASH'}}) {
  3         7  
48 6         9 my $old_record = shift @{$this->{'HASH'}->{$key}};
  6         12  
49 6 100       11 if ( $old_record ) {
50 4         12 $record->set($key, $old_record);
51 4         8 $found = 1;
52             }
53             else {
54 2         4 delete $this->{'HASH'}->{$key};
55             }
56             }
57 3 100       14 $this->push_record($record) if ( $found );
58             }
59             }
60              
61             sub add_help_types {
62 1     1 0 2 my $this = shift;
63 1         6 $this->use_help_type('keyspecs');
64             }
65              
66             sub usage {
67 0     0 0   my $this = shift;
68              
69 0           my $options = [
70             ['field ', 'Field to use as the column key, may be a keyspec'],
71             ];
72              
73 0           my $args_string = $this->options_string($options);
74              
75 0           my $usage = <
76             Usage: recs-stream2table []
77             __FORMAT_TEXT__
78             Transforms a list of records, combinging records based on a column, FIELD.
79             In order, the values of the column will be added to the output records.
80              
81             Note: This script spools the stream into memory
82             __FORMAT_TEXT__
83              
84             This stream:
85             { "column": "foo", "data": "foo1" }
86             { "column": "foo", "data": "foo2" }
87             { "column": "boo", "data": "boo1" }
88             { "column": "boo", "data": "boo2" }
89              
90             with recs-stream2table --field column becomes:
91             {"boo":{"data":"boo1"},"foo":{"data":"foo1"}}
92             {"boo":{"data":"boo2"},"foo":{"data":"foo2"}}
93              
94             __FORMAT_TEXT__
95             Hint: use recs-flatten if you want those values to be in the top level of
96             the record
97              
98             The full input record will be associated with the value of the FIELD. The
99             field itself will be removed from the nested record if the passed field is
100             not a key spec.
101             __FORMAT_TEXT__
102              
103             Arguments:
104             $args_string
105              
106             Examples:
107             # Transform a record stream with each stat on one line to a stream with one
108             # value for each stat present on one line
109             ... | recs-stream2table --field stat
110             USAGE
111             }
112              
113             1;