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