blib/lib/App/RecordStream/Operation/fromjsonarray.pm

filter: Statementstmt 91.3% 53 / 58 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 83.7% 67 / 80 CC 7 SCAR 11.9
CC
7
Cov
88%
stream_done
7.7
get_records_from_handle
4.0
BEGIN
1.0
CRAP 3.3
« blib/lib/App/RecordStream/BaseRegistry.pm ↑ Summary blib/lib/App/RecordStream/Operation/delta.pm »
1 package App::RecordStream::Operation::fromjsonarray;
2  
3 3 our $VERSION = "4.0.25";
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
15 6 my ($this, $args) = @_;
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 10 ▶sub wants_input {
Subroutine wants_input: called CC=1 SCAR=0.0
Pod undocumented
32 10 return 0;
33 }
34  
35 5 ▶sub stream_done {
Subroutine stream_done: called CC=4 SCAR=20.4
Pod undocumented
36 5 my ($this) = @_;
37  
38 5 my $files = $this->{'EXTRA_ARGS'};
39  
40 5 ▶ if ( scalar @$files > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$files > 0) { } 0 5 5
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
BranchTrueFalseTotal
open my $fh, "<", $file or die "Could not open file: $!\n" 0 0 0
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 5 ▶sub get_records_from_handle {
Subroutine get_records_from_handle: called CC=4 SCAR=13.9
Pod undocumented
55 5 my ($this, $fh) = @_;
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
BranchTrueFalseTotal
if ($has_fields) 12 8 20
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 1 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
81 1 my ($this) = @_;
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 3 1;
« blib/lib/App/RecordStream/BaseRegistry.pm ↑ Summary blib/lib/App/RecordStream/Operation/delta.pm »