« blib/lib/App/RecordStream/BaseRegistry.pm
↑ Summary
blib/lib/App/RecordStream/Operation/delta.pm »
| 1 | package App::RecordStream::Operation::fromjsonarray; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $VERSION = "4.0.23"; | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use App::RecordStream::Record; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | sub init { | ||||||||||
| 15 | 6 | ▶ | my ($this, $args) = @_; | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | |||||||||||
| 17 | 6 | my @fields; | |||||||||
| 18 | 6 | my $preserve_empty = undef; | |||||||||
| 19 | |||||||||||
| 20 | my $spec = { | ||||||||||
| 21 | 4 | ▶ | 'key|k=s' => sub { push @fields, split(/,/, $_[1]); }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 6 | }; | |||||||||
| 23 | |||||||||||
| 24 | 6 | $this->parse_options($args, $spec); | |||||||||
| 25 | |||||||||||
| 26 | 6 | $this->{'EXTRA_ARGS'} = $args; | |||||||||
| 27 | 6 | $this->{'FIELDS'} = \@fields; | |||||||||
| 28 | 6 | $this->{'JSON'} = JSON->new(); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub wants_input { | ||||||||||
| 32 | 10 | ▶ | return 0; | ||||||||
|
Subroutine
wants_input: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub stream_done { | ||||||||||
| 36 | 5 | ▶ | my ($this) = @_; | ||||||||
|
Subroutine
stream_done: called CC=4 SCAR=20.4
Pod
undocumented
| |||||||||||
| 37 | |||||||||||
| 38 | 5 | my $files = $this->{'EXTRA_ARGS'}; | |||||||||
| 39 | |||||||||||
| 40 | 5 | ▶ | if ( scalar @$files > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | foreach my $file ( @$files ) { | |||||||||
| 42 | 0 | $this->update_current_filename($file); | |||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | open(my $fh, '<', $file) or die "Could not open file: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | $this->get_records_from_handle($fh); | |||||||||
| 46 | 0 | close $fh; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | else { | ||||||||||
| 50 | 5 | $this->get_records_from_handle(\*STDIN); | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub get_records_from_handle { | ||||||||||
| 55 | 5 | ▶ | my ($this, $fh) = @_; | ||||||||
|
Subroutine
get_records_from_handle: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 56 | |||||||||||
| 57 | 5 | my $json = $this->{'JSON'}; | |||||||||
| 58 | 5 | my $fields = $this->{'FIELDS'}; | |||||||||
| 59 | 5 | my $has_fields = scalar @$fields; | |||||||||
| 60 | |||||||||||
| 61 | 5 | my $contents = do { local $/; <$fh> }; | |||||||||
| 62 | |||||||||||
| 63 | 5 | my @arrays = $json->incr_parse($contents); | |||||||||
| 64 | |||||||||||
| 65 | 5 | for my $item (map { @$_ } @arrays) { | |||||||||
| 66 | 20 | $item = App::RecordStream::Record->new($item); | |||||||||
| 67 | |||||||||||
| 68 | 20 | my $record = $item; | |||||||||
| 69 | 20 | ▶ | if ($has_fields) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 12 | $record = App::RecordStream::Record->new(); | |||||||||
| 71 | 12 | for my $field (@$fields) { | |||||||||
| 72 | 20 | $record->set($field, ${$item->guess_key_from_spec($field)}); | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 20 | $this->push_record($record); | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub usage { | ||||||||||
| 81 | 1 | ▶ | my ($this) = @_; | ||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 82 | |||||||||||
| 83 | 1 | my $options = [ | |||||||||
| 84 | [ 'key|k <keys>', '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 | 1 | my $args_string = $this->options_string($options); | |||||||||
| 88 | |||||||||||
| 89 | 1 | return <<USAGE; | |||||||||
| 90 | Usage: recs-fromjsonarray <args> [<files>] | ||||||||||
| 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 | 2 | 1; | |||||||||