| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::fromjsonarray; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = "4.0.25"; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1092 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 39 |  | 
| 6 | 2 |  |  | 2 |  | 6 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 38 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 6 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 127 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 8 | use App::RecordStream::Record; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 35 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 2 |  |  | 2 |  | 8 | use JSON::MaybeXS; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 766 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub init { | 
| 15 | 5 |  |  | 5 | 0 | 12 | my ($this, $args) = @_; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 5 |  |  |  |  | 5 | my @fields; | 
| 18 | 5 |  |  |  |  | 7 | my $preserve_empty = undef; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my $spec = { | 
| 21 | 4 |  |  | 4 |  | 946 | 'key|k=s'        => sub { push @fields, split(/,/, $_[1]); }, | 
| 22 | 5 |  |  |  |  | 17 | }; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 5 |  |  |  |  | 14 | $this->parse_options($args, $spec); | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 5 |  |  |  |  | 9 | $this->{'EXTRA_ARGS'}     = $args; | 
| 27 | 5 |  |  |  |  | 7 | $this->{'FIELDS'}         = \@fields; | 
| 28 | 5 |  |  |  |  | 13 | $this->{'JSON'}           = JSON->new(); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub wants_input { | 
| 32 | 10 |  |  | 10 | 0 | 23 | return 0; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub stream_done { | 
| 36 | 5 |  |  | 5 | 0 | 28 | my ($this) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 5 |  |  |  |  | 11 | my $files = $this->{'EXTRA_ARGS'}; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 | 50 |  |  |  | 25 | 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 |  |  |  |  | 10 | $this->get_records_from_handle(\*STDIN); | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub get_records_from_handle { | 
| 55 | 5 |  |  | 5 | 0 | 8 | my ($this, $fh) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 5 |  |  |  |  | 6 | my $json = $this->{'JSON'}; | 
| 58 | 5 |  |  |  |  | 6 | my $fields = $this->{'FIELDS'}; | 
| 59 | 5 |  |  |  |  | 5 | my $has_fields = scalar @$fields; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 5 |  |  |  |  | 6 | my $contents = do { local $/; <$fh> }; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 34 |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 5 |  |  |  |  | 62 | my @arrays = $json->incr_parse($contents); | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 5 |  |  |  |  | 10 | for my $item (map { @$_ } @arrays) { | 
|  | 5 |  |  |  |  | 11 |  | 
| 66 | 20 |  |  |  |  | 49 | $item = App::RecordStream::Record->new($item); | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 20 |  |  |  |  | 21 | my $record = $item; | 
| 69 | 20 | 100 |  |  |  | 29 | if ($has_fields) { | 
| 70 | 12 |  |  |  |  | 15 | $record = App::RecordStream::Record->new(); | 
| 71 | 12 |  |  |  |  | 16 | for my $field (@$fields) { | 
| 72 | 20 |  |  |  |  | 23 | $record->set($field, ${$item->guess_key_from_spec($field)}); | 
|  | 20 |  |  |  |  | 33 |  | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 20 |  |  |  |  | 37 | $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; |