File Coverage

blib/lib/App/RecordStream/Operation/fromjsonarray.pm
Criterion Covered Total %
statement 47 56 83.9
branch 3 6 50.0
condition n/a
subroutine 10 11 90.9
pod 0 5 0.0
total 60 78 76.9


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromjsonarray;
2              
3             our $VERSION = "4.0.24";
4              
5 2     2   1123 use strict;
  2         5  
  2         49  
6 2     2   8 use warnings;
  2         4  
  2         46  
7              
8 2     2   9 use base qw(App::RecordStream::Operation);
  2         4  
  2         112  
9              
10 2     2   11 use App::RecordStream::Record;
  2         5  
  2         33  
11              
12 2     2   9 use JSON::MaybeXS;
  2         4  
  2         882  
13              
14             sub init {
15 5     5 0 13 my ($this, $args) = @_;
16              
17 5         8 my @fields;
18 5         8 my $preserve_empty = undef;
19              
20             my $spec = {
21 4     4   1213 'key|k=s' => sub { push @fields, split(/,/, $_[1]); },
22 5         26 };
23              
24 5         23 $this->parse_options($args, $spec);
25              
26 5         12 $this->{'EXTRA_ARGS'} = $args;
27 5         12 $this->{'FIELDS'} = \@fields;
28 5         20 $this->{'JSON'} = JSON->new();
29             }
30              
31             sub wants_input {
32 10     10 0 34 return 0;
33             }
34              
35             sub stream_done {
36 5     5 0 12 my ($this) = @_;
37              
38 5         9 my $files = $this->{'EXTRA_ARGS'};
39              
40 5 50       15 if ( scalar @$files > 0 ) {
41 0         0 foreach my $file ( @$files ) {
42 0         0 $this->update_current_filename($file);
43              
44 0 0       0 open(my $fh, '<', $file) or die "Could not open file: $!\n";
45 0         0 $this->get_records_from_handle($fh);
46 0         0 close $fh;
47             }
48             }
49             else {
50 5         16 $this->get_records_from_handle(\*STDIN);
51             }
52             }
53              
54             sub get_records_from_handle {
55 5     5 0 11 my ($this, $fh) = @_;
56              
57 5         7 my $json = $this->{'JSON'};
58 5         9 my $fields = $this->{'FIELDS'};
59 5         8 my $has_fields = scalar @$fields;
60              
61 5         9 my $contents = do { local $/; <$fh> };
  5         15  
  5         26  
62              
63 5         59 my @arrays = $json->incr_parse($contents);
64              
65 5         14 for my $item (map { @$_ } @arrays) {
  5         16  
66 20         58 $item = App::RecordStream::Record->new($item);
67              
68 20         29 my $record = $item;
69 20 100       36 if ($has_fields) {
70 12         25 $record = App::RecordStream::Record->new();
71 12         22 for my $field (@$fields) {
72 20         26 $record->set($field, ${$item->guess_key_from_spec($field)});
  20         41  
73             }
74             }
75              
76 20         81 $this->push_record($record);
77             }
78             }
79              
80             sub usage {
81 0     0 0   my ($this) = @_;
82              
83 0           my $options = [
84             [ 'key|k ', 'Optional Comma separated list of field names. If none specified, use all keys. May be specified multiple times, may be key specs' ],
85             ];
86              
87 0           my $args_string = $this->options_string($options);
88              
89 0           return <
90             Usage: recs-fromjsonarray []
91             __FORMAT_TEXT__
92             Import JSON objects from within a JSON array.
93             __FORMAT_TEXT__
94             Arguments:
95             $args_string
96              
97             USAGE
98             }
99              
100             1;