| 1 | 3 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::JSON; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use experimental qw(builtin try); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use builtin qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use JSON::MaybeXS qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Ref::Util qw(is_plain_arrayref is_plain_hashref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Ekorn::Pipeline::Factories qw(gather sink_one source stage); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | our @EXPORT_OK = qw(from_json from_jsonl to_json to_jsonl); | |||||||||||||||||||||||||||
| 17 | 3 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||
| 20 | 3 | our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # How much a source reads at a time, before reading on to the end of the | ||||||||||||||||||||||||||||
| 23 | # line, so that no number is cut in two; the tests make it small. | ||||||||||||||||||||||||||||
| 24 | 3 | our $BLOCK = 65_536; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # A source of one JSON document, read from the file or filehandle: its | ||||||||||||||||||||||||||||
| 27 | # elements, if it's an array, or itself. Given neither, a stage decoding | ||||||||||||||||||||||||||||
| 28 | # each value, one document, the same way. | ||||||||||||||||||||||||||||
| 29 | 40 | ▶ | sub from_json (@options) { | ||||||||||||||||||||||||||
|
Subroutine
from_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 40 | my %option = _options(@options); | |||||||||||||||||||||||||||
| 31 | 32 | ▶ | return _reader(\%option, 'json') if _has_input(\%option); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 5 | my $json = $option{new}->(); | |||||||||||||||||||||||||||
| 33 | 17 | ▶ | return stage { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 17 | my $document; | |||||||||||||||||||||||||||
| 35 | 17 | try { | |||||||||||||||||||||||||||
| 36 | 17 | $document = $json->decode($_); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | catch ($e) { | ||||||||||||||||||||||||||||
| 39 | 4 | _undecodable($json, 'a value', _unplaced($e)); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 13 | ▶ | return is_plain_arrayref($document) ? @$document : $document; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 5 | }; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # A source of the documents in a file or filehandle of JSON Lines, each | ||||||||||||||||||||||||||||
| 47 | # as it is. Given neither, a stage decoding each value, a text of one or | ||||||||||||||||||||||||||||
| 48 | # more lines, into each line's document. | ||||||||||||||||||||||||||||
| 49 | 42 | ▶ | sub from_jsonl (@options) { | ||||||||||||||||||||||||||
|
Subroutine
from_jsonl: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 42 | my %option = _options(@options); | |||||||||||||||||||||||||||
| 51 | 34 | ▶ | return _reader(\%option, 'lines') if _has_input(\%option); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | my $new = $option{new}; | |||||||||||||||||||||||||||
| 53 | 10 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 10 | my $json = $new->(); # this run's, as its parser holds what it's read | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 57 | 18 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 18 | my @documents; | |||||||||||||||||||||||||||
| 59 | 18 | try { | |||||||||||||||||||||||||||
| 60 | 18 | @documents = $json->incr_parse($_[0]); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | catch ($e) { | ||||||||||||||||||||||||||||
| 63 | 2 | _undecodable($json, 'a value', _unplaced($e)); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 16 | _complete($json, 'a value'); | |||||||||||||||||||||||||||
| 66 | 14 | return @documents; | |||||||||||||||||||||||||||
| 67 | 10 | }; | |||||||||||||||||||||||||||
| 68 | 4 | }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # A sink writing every value to the file or filehandle as one JSON | ||||||||||||||||||||||||||||
| 73 | # document, an array, giving how many. Given neither, a stage passing on | ||||||||||||||||||||||||||||
| 74 | # every value as that array's text, once they've all come. | ||||||||||||||||||||||||||||
| 75 | 27 | ▶ | sub to_json (@options) { | ||||||||||||||||||||||||||
|
Subroutine
to_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 27 | my %option = _options(@options); | |||||||||||||||||||||||||||
| 77 | 19 | my $json = $option{new}->(); | |||||||||||||||||||||||||||
| 78 | 19 | ▶ | return _writer(\%option, 'json', $json) if _has_input(\%option); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 7 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 7 | my @values; # this run's | |||||||||||||||||||||||||||
| 81 | return ( | ||||||||||||||||||||||||||||
| 82 | 12 | ▶ | sub { push @values, $_[0]; return }, # for each value: no signature | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 7 | ▶ | sub () { _encoded($json, \@values) =~ s( \n \z )()xr }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 7 | ); | |||||||||||||||||||||||||||
| 85 | 8 | }; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # A sink writing each value to the file or filehandle as a line of JSON | ||||||||||||||||||||||||||||
| 90 | # Lines, giving how many. Given neither, a stage passing on each value | ||||||||||||||||||||||||||||
| 91 | # as its line, without its newline. | ||||||||||||||||||||||||||||
| 92 | 27 | ▶ | sub to_jsonl (@options) { | ||||||||||||||||||||||||||
|
Subroutine
to_jsonl: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 27 | my %option = _options(@options); | |||||||||||||||||||||||||||
| 94 | 19 | my $json = $option{new}->(); | |||||||||||||||||||||||||||
| 95 | 19 | ▶ | croakf q(Expected an encoder that doesn't indent, to write JSON Lines) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | if _indents($json); | ||||||||||||||||||||||||||||
| 97 | 17 | ▶ | return _writer(\%option, 'lines', $json) if _has_input(\%option); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 12 | ▶ | return stage { _encoded($json, $_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Return the options given, checked, with "new", a sub returning the | ||||||||||||||||||||||||||||
| 103 | # encoder: one made from json's options, over the defaults, or json's | ||||||||||||||||||||||||||||
| 104 | # object, as it is. | ||||||||||||||||||||||||||||
| 105 | 136 | ▶ | sub _options (@given) { | ||||||||||||||||||||||||||
|
Subroutine
_options: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||
| 106 | 136 | ▶ | croakf 'Expected options as name => value pairs' if @given % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 132 | my %option = @given; | |||||||||||||||||||||||||||
| 108 | 132 | my @unknown = sort grep { !m( \A (?: file | fh | json ) \z )x } keys %option; | |||||||||||||||||||||||||||
| 109 | 132 | ▶ | croakf 'Expected the options file, fh and json, not %s', join ', ', @unknown | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | if @unknown; | ||||||||||||||||||||||||||||
| 111 | croakf 'Expected file to be a path' | ||||||||||||||||||||||||||||
| 112 | 124 | ▶ | if exists $option{file} && !defined $option{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $option{"file"} and not defined $option{"file"}
MC/DC100%2/2
exists $option{"file"} and not defined $option{"file"}
exists $option{"file"}
not defined $option{"file"}
Truth table100%3/3
A and not(B)
A= exists $option{"file"}B= not defined $option{"file"}
| |||||||||||||||||||||||||||||
| 113 | croakf 'Expected a file or a filehandle, not both' | ||||||||||||||||||||||||||||
| 114 | 120 | ▶ | if defined $option{file} && exists $option{fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $option{"file"} and exists $option{"fh"}
MC/DC100%2/2
defined $option{"file"} and exists $option{"fh"}
defined $option{"file"}
exists $option{"fh"}
Truth table100%3/3
A and B
A= defined $option{"file"}B= exists $option{"fh"}
| |||||||||||||||||||||||||||||
| 115 | croakf 'Expected an open filehandle' | ||||||||||||||||||||||||||||
| 116 | 116 | ▶ | if exists $option{fh} && !defined openhandle($option{fh}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $option{"fh"} and not defined &openhandle($option{"fh"})
MC/DC100%2/2
exists $option{"fh"} and not defined &openhandle($option{"fh"})
exists $option{"fh"}
not defined &openhandle($option{"fh"})
Truth table100%3/3
A and not(B)
A= exists $option{"fh"}B= not defined &openhandle($option{"fh"})
| |||||||||||||||||||||||||||||
| 117 | 112 | my $json = $option{json}; | |||||||||||||||||||||||||||
| 118 | 112 | ▶ | if (blessed($json)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 43 | ▶ | $option{new} = sub () { $json }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 43 | return %option; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 69 | ▶ | croakf 'Expected json to be a hash reference of options, or an encoder object' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $json and not Ref::Util::XS::is_plain_hashref($json)
MC/DC100%2/2
defined $json and not Ref::Util::XS::is_plain_hashref($json)
defined $json
not Ref::Util::XS::is_plain_hashref($json)
Truth table100%3/3
A and not(B)
A= defined $jsonB= not Ref::Util::XS::is_plain_hashref($json)
| |||||||||||||||||||||||||||||
| 123 | if defined $json && !is_plain_hashref($json); | ||||||||||||||||||||||||||||
| 124 | 65 | ▶ | my %settings = (utf8 => 1, allow_nonref => 1, %{ $json // {} }); | ||||||||||||||||||||||||||
|
Condition100%2/2
$json // {}
MC/DC100%1/1
$json // {}
$json
Truth table100%2/2
A
A= $json
| |||||||||||||||||||||||||||||
| 125 | 65 | try { | |||||||||||||||||||||||||||
| 126 | 65 | JSON::MaybeXS->new(%settings); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | catch ($e) { | ||||||||||||||||||||||||||||
| 129 | 4 | croakf 'Expected json options the encoder knows: %s', _unplaced($e); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 61 | ▶ | $option{new} = sub () { JSON::MaybeXS->new(%settings) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 61 | return %option; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Return true if the options name a file or a filehandle. | ||||||||||||||||||||||||||||
| 137 | 102 | ▶ | sub _has_input ($option) { | ||||||||||||||||||||||||||
|
Subroutine
_has_input: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | 102 | ▶ | return defined $option->{file} || !!$option->{fh}; | ||||||||||||||||||||||||||
|
Condition100%3/3
defined $option->{'file'} || !!$option->{'fh'}
MC/DC100%2/2
defined $option->{'file'} || !!$option->{'fh'}
defined $option->{'file'}
!!$option->{'fh'}
Truth table100%3/3
A || B
A= defined $option->{'file'}B= !!$option->{'fh'}
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Return true if the encoder indents, so writes a document over many | ||||||||||||||||||||||||||||
| 143 | # lines. | ||||||||||||||||||||||||||||
| 144 | 35 | ▶ | sub _indents ($json) { | ||||||||||||||||||||||||||
|
Subroutine
_indents: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 145 | 35 | ▶ | return $json->can('get_indent') && $json->get_indent; | ||||||||||||||||||||||||||
|
Condition100%3/3
$json->can('get_indent') && $json->get_indent
MC/DC100%2/2
$json->can('get_indent') && $json->get_indent
$json->can('get_indent')
$json->get_indent
Truth table100%3/3
A && B
A= $json->can('get_indent')B= $json->get_indent
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Return an error's message without the place it was raised, as a | ||||||||||||||||||||||||||||
| 150 | # message of ours says where. | ||||||||||||||||||||||||||||
| 151 | 13 | ▶ | sub _unplaced ($error) { | ||||||||||||||||||||||||||
|
Subroutine
_unplaced: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 13 | return "$error" =~ s( ,? \s+ at \s+ \S+ \s+ line \s+ \d+ \.? \s* \z )()xr; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Croak that what's read from $from can't be decoded, and why. The | ||||||||||||||||||||||||||||
| 157 | # decoder is reset, for the next run, if it's the caller's. | ||||||||||||||||||||||||||||
| 158 | 15 | ▶ | sub _undecodable ($json, $from, $why) { | ||||||||||||||||||||||||||
|
Subroutine
_undecodable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 15 | $json->incr_reset; | |||||||||||||||||||||||||||
| 160 | 15 | return croakf q(Can't decode JSON from %s: %s), $from, $why; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Croak unless the decoder has a whole number of documents: it croaks, | ||||||||||||||||||||||||||||
| 165 | # asked for the text it holds, if it's inside one. | ||||||||||||||||||||||||||||
| 166 | 66 | ▶ | sub _complete ($json, $from) { | ||||||||||||||||||||||||||
|
Subroutine
_complete: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 66 | try { | |||||||||||||||||||||||||||
| 168 | 66 | $json->incr_text; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | catch ($e) { | ||||||||||||||||||||||||||||
| 171 | 6 | _undecodable($json, $from, 'it ends inside a document'); | |||||||||||||||||||||||||||
| 172 | }; # the ; ends the statement for Perl::Critic, which doesn't know try | ||||||||||||||||||||||||||||
| 173 | 60 | return; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # Return a source of the documents in the file or filehandle: as they | ||||||||||||||||||||||||||||
| 178 | # come, for JSON Lines; or, for one document, its elements, if it's an | ||||||||||||||||||||||||||||
| 179 | # array, or itself, once the input has ended. | ||||||||||||||||||||||||||||
| 180 | 57 | ▶ | sub _reader ($option, $format) { | ||||||||||||||||||||||||||
|
Subroutine
_reader: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 181 | 57 | my($path, $given, $new) = @{ $option }{qw(file fh new)}; | |||||||||||||||||||||||||||
| 182 | 57 | ▶ | my $from = $path // 'the filehandle'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$path // "the filehandle"
MC/DC100%1/1
$path // "the filehandle"
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 183 | 59 | ▶ | return source { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 184 | 59 | my $fh = $given; | |||||||||||||||||||||||||||
| 185 | 59 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | # Open for the run, and closed by its end. | ||||||||||||||||||||||||||||
| 187 | 53 | ▶ | open $fh, '<:raw', $path ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | or croakf q(Can't read %s: %s), $path, $!; | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 58 | my $json = $new->(); # this run's, or the caller's, reset | |||||||||||||||||||||||||||
| 191 | 58 | $json->incr_reset; | |||||||||||||||||||||||||||
| 192 | 58 | my(@documents, $ended); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 195 | 138 | ▶ | my $step = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 196 | 138 | while (1) { | |||||||||||||||||||||||||||
| 197 | 436 | ▶ | return splice @documents if @documents && $format eq 'lines'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@documents and $format eq "lines"
MC/DC100%2/2
@documents and $format eq "lines"
@documents
$format eq "lines"
Truth table100%3/3
A and B
A= @documentsB= $format eq "lines"
| |||||||||||||||||||||||||||||
| 198 | 371 | ▶ | if ($ended) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 65 | ▶ | return unless @documents; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 21 | my($only) = splice @documents; | |||||||||||||||||||||||||||
| 201 | 21 | ▶ | return is_plain_arrayref($only) ? @$only : $only; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 306 | my $chunk = _chunk($fh, $from); | |||||||||||||||||||||||||||
| 204 | 305 | ▶ | unless (defined $chunk) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 50 | _complete($json, $from); | |||||||||||||||||||||||||||
| 206 | 46 | $ended = 1; | |||||||||||||||||||||||||||
| 207 | 46 | next; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 255 | my @read; | |||||||||||||||||||||||||||
| 210 | 255 | try { | |||||||||||||||||||||||||||
| 211 | 255 | @read = $json->incr_parse($chunk); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | catch ($e) { | ||||||||||||||||||||||||||||
| 214 | 1 | _undecodable($json, $from, _unplaced($e)); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 254 | push @documents, @read; | |||||||||||||||||||||||||||
| 217 | 254 | ▶ | _undecodable( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$format eq "json" and @documents > 1
MC/DC100%2/2
$format eq "json" and @documents > 1
$format eq "json"
@documents > 1
Truth table100%3/3
A and B
A= $format eq "json"B= @documents > 1
| |||||||||||||||||||||||||||||
| 218 | $json, $from, | ||||||||||||||||||||||||||||
| 219 | 'it holds more than one document; read JSON Lines with from_jsonl', | ||||||||||||||||||||||||||||
| 220 | ) if $format eq 'json' && @documents > 1; | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 58 | }; | |||||||||||||||||||||||||||
| 223 | 58 | ▶ | return {step => $step, end => $path ? sub ($how) { close $fh } : undef}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 57 | }; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Return the next chunk of $fh, read from $from: up to $BLOCK, and on to | ||||||||||||||||||||||||||||
| 229 | # the end of its line; nothing at its end. | ||||||||||||||||||||||||||||
| 230 | 306 | ▶ | sub _chunk ($fh, $from) { | ||||||||||||||||||||||||||
|
Subroutine
_chunk: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 231 | 306 | my $chunk; | |||||||||||||||||||||||||||
| 232 | 306 | my $read = read $fh, $chunk, $BLOCK; | |||||||||||||||||||||||||||
| 233 | 306 | ▶ | croakf q(Can't read %s: %s), $from, $! unless defined $read; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 305 | ▶ | return unless $read; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 255 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 236 | 255 | ▶ | if (substr($chunk, -1) ne "\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 219 | my $rest = readline $fh; | |||||||||||||||||||||||||||
| 238 | 219 | ▶ | $chunk .= $rest if defined $rest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 255 | return $chunk; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # Return $value encoded. | ||||||||||||||||||||||||||||
| 245 | 53 | ▶ | sub _encoded ($json, $value) { | ||||||||||||||||||||||||||
|
Subroutine
_encoded: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 53 | my $text; | |||||||||||||||||||||||||||
| 247 | 53 | try { | |||||||||||||||||||||||||||
| 248 | 53 | $text = $json->encode($value); | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | catch ($e) { | ||||||||||||||||||||||||||||
| 251 | 2 | croakf q(Can't encode a value as JSON: %s), _unplaced($e); | |||||||||||||||||||||||||||
| 252 | }; # the ; ends the statement for Perl::Critic, which doesn't know try | ||||||||||||||||||||||||||||
| 253 | 51 | return $text; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # Return a sink writing each value, encoded, to the file or filehandle, | ||||||||||||||||||||||||||||
| 258 | # as the format says, giving how many. One document is an array, | ||||||||||||||||||||||||||||
| 259 | # written as the values come: an encoder that indents has its values' | ||||||||||||||||||||||||||||
| 260 | # lines indented in it. | ||||||||||||||||||||||||||||
| 261 | 16 | ▶ | sub _writer ($option, $format, $json) { | ||||||||||||||||||||||||||
|
Subroutine
_writer: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 262 | 16 | my($path, $given) = @{ $option }{qw(file fh)}; | |||||||||||||||||||||||||||
| 263 | 16 | ▶ | my $to = $path // 'the filehandle'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$path // "the filehandle"
MC/DC100%1/1
$path // "the filehandle"
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 264 | 16 | my $indent = _indents($json); | |||||||||||||||||||||||||||
| 265 | 16 | ▶ | my($before, $between, $after) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | = $indent ? ("[\n", ",\n", "\n]\n") : ('[', ',', "]\n"); | ||||||||||||||||||||||||||||
| 267 | 16 | ▶ | return sink_one { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 268 | 16 | my $fh = $given; | |||||||||||||||||||||||||||
| 269 | 16 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | # Open for the run, and closed by its end. | ||||||||||||||||||||||||||||
| 271 | 11 | ▶ | open $fh, '>:raw', $path ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | or croakf q(Can't write %s: %s), $path, $!; | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 15 | my $n = 0; | |||||||||||||||||||||||||||
| 275 | 36 | ▶ | my $write = sub (@text) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 276 | 36 | ▶ | print { $fh } @text or croakf q(Can't write %s: %s), $to, $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 15 | }; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 280 | my $step | ||||||||||||||||||||||||||||
| 281 | = $format eq 'lines' | ||||||||||||||||||||||||||||
| 282 | 11 | ▶ | ? sub { $write->(_encoded($json, $_[0]), "\n"); $n++ } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 283 | 15 | ▶ | : sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 284 | 15 | my $text = _encoded($json, $_[0]) =~ s( \n \z )()xr; | |||||||||||||||||||||||||||
| 285 | 15 | ▶ | $text =~ s( ^ )( )xmg if $indent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 15 | ▶ | $write->($n++ ? $between : $before, $text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 15 | ▶ | }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | return { | ||||||||||||||||||||||||||||
| 289 | step => $step, | ||||||||||||||||||||||||||||
| 290 | 14 | ▶ | flush => sub () { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 291 | 14 | ▶ | $write->($n ? $after : "[]\n") if $format eq 'json'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 292 | 14 | return $n; | |||||||||||||||||||||||||||
| 293 | }, | ||||||||||||||||||||||||||||
| 294 | end => $path | ||||||||||||||||||||||||||||
| 295 | 10 | ▶ | ? sub ($how) { close $fh or croakf q(Can't write %s: %s), $path, $! } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | : undef, | ||||||||||||||||||||||||||||
| 297 | 15 | ▶ | }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 16 | }; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | 3 | 1; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | __END__ | ||||||||||||||||||||||||||||