Base/CLI_JSON.pm

filter: Statementstmt 77.4% 261 / 337 Branchbran 52.0% 76 / 146 Conditioncond 33.3% 19 / 57 MC/DCmcdc 5.7% 2 / 35 Subroutinesub 80.6% 54 / 67 Podpod 0.0% 0 / 43 totaltotal 60.1% 412 / 685 CC 106 SCAR 22.9
CC
106
Cov
66%
cli_create_from_file
42.0
cli_write_fh
30.8
_cli_xargs
27.8
CRAP 9.9
« blib/lib/MOP4Import/Types.pm ↑ Summary Util.pm »
1 #!/usr/bin/env perl
2 package MOP4Import::Base::CLI_JSON;
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 BEGIN {
9 12 ▶ print STDERR "Using (file '" . __FILE__ . "')\n"
Subroutine BEGIN: called CC=1 SCAR=0.0
10 , (DEBUG >= 3 ? Carp::longmess("callstack:") : ())
11 if DEBUG and DEBUG >= 2
12 }
13  
14 12 use MOP4Import::Base::CLI -as_base
15 , [constant => parse_opts__preserve_hyphen => 1]
16 , [fields =>
17 , ['help' => doc => "show this help message", json_type => 'string']
18 , ['quiet' => doc => 'to be (somewhat) quiet', json_type => 'int']
19 , ['scalar' => doc => "evaluate methods in scalar context", json_type => 'bool']
20 , ['output' => default => 'ndjson'
21 , doc => "choose output serializer (ndjson/json/tsv/dump)"
22 , json_type => 'string'
23 ]
24 , ['flatten'
25 , json_type => 'bool'
26 ]
27 , ['undef-as' => default => 'null'
28 , doc => "serialize undef as this value. used in tsv output"
29 , json_type => 'string'
30 ]
31 , ['no-exit-code'
32 , doc => "exit with 0(EXIT_SUCCESS) even when result was falsy/empty"
33 , json_type => 'bool'
34 ]
35 , ['binary' => default => 0, doc => "keep STDIN/OUT/ERR binary friendly"
36 , json_type => 'bool'
37 ]
38 12 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 12 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 12 ▶use MOP4Import::Base::JSON -as_base;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 12 ▶use MOP4Import::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 12 ▶use MOP4Import::Util qw/lexpand globref take_locked_opts_of lock_keys_as/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 12 ▶use open ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 sub cli_precmd {
51 23 ▶ (my MY $self) = @_;
Subroutine cli_precmd: called CC=3 SCAR=12.5
Pod undocumented
52 #
53 # cli_precmd() may be called from $class->cmd_help.
54 #
55 23 ▶ unless (ref $self and $self->{binary}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self and $self->{"binary"}) 23 0 23
Condition33%1/3
ref $self and $self->{"binary"}
!ll&&!rl&&r
0230
MC/DC0%0/2
ref $self and $self->{"binary"}
ref $self $self->{"binary"}
Truth table33%1/3
A and B
A= ref $selfB= $self->{"binary"}
ABresult
0X0
100
111
56 23 'open'->import(qw/:locale :std/);
57 }
58 }
59  
60 #
61 # Replace parse_opts to use parse_json_opts
62 #
63 sub cli_parse_opts {
64 23 ▶ my ($pack, $list, $result, $opt_alias, $converter, %opts) = @_;
Subroutine cli_parse_opts: called CC=1 SCAR=0.0
Pod undocumented
65  
66 23 MOP4Import::Util::parse_json_opts($pack, $list, $result, $opt_alias);
67 }
68  
69 sub cli_eval {
70 1 ▶ (my MY $self, my ($script, @args)) = @_;
Subroutine cli_eval: called CC=2 SCAR=6.9
Pod undocumented
71 1 my $pack = ref $self;
72 1 my $sub = do {
73 1 local $@;
74 1 ▶ my $code = eval qq{package $pack; use strict; sub {my \$self = shift; $script\n}};
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
76 1 $code;
77 };
78 1 $sub->($self, @args);
79 }
80  
81 sub cli_invoke {
82 19 ▶ (my MY $self, my ($method, @args)) = @_;
Subroutine cli_invoke: called CC=3 SCAR=11.0
Pod undocumented
83  
84 19 $self->cli_precmd($method);
85  
86 19 ▶ my $sub = $self->can($method)
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can($method) or Carp::croak("No such method: $method") 0 19 19
87 or Carp::croak "No such method: $method";
88  
89 19 my $list = $self->cli_invoke_sub($sub, $self, @args);
90  
91 19 ▶ $self->cli_exit_for_result($list) unless $self->{'no-exit-code'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no-exit-code"} 5 14 19
92 }
93  
94 sub cli_invoke_sub {
95 19 ▶ (my MY $self, my ($sub, $receiver, @args)) = @_;
Subroutine cli_invoke_sub: called CC=3 SCAR=11.0
Pod undocumented
96  
97 19 my @res;
98 19 ▶ if ($self->{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'scalar'}) { } 5 14 19
99 5 $res[0] = $sub->($receiver, @args);
100 } else {
101 14 @res = $sub->($receiver, @args);
102 }
103  
104 19 ▶ $self->cli_output(\@res) unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 19 0 19
105  
106 19 \@res;
107 }
108  
109 #
110 # cli_output($list) -- Output abstraction (yield).
111 #
112 sub cli_output :method {
113 19 ▶ (my MY $self, my ($list)) = @_;
Subroutine cli_output: called CC=6 SCAR=17.9
Pod undocumented
114  
115 19 ▶ unless ($self->{scalar} ? $list->[0] : @$list) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->{"scalar"} ? $list->[0] : @$list) 2 17 19
$self->{'scalar'} ? : 5 14 19
116 2 return;
117 }
118  
119 my $emitter = ref $self->{output} eq 'CODE' ? $self->{output} : sub {
120 17 ▶ $self->cli_write_fh(\*STDOUT, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
121 17 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'output'} eq 'CODE' ? : 0 17 17
122  
123 17 ▶ if ($self->{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'scalar'}) { } 4 13 17
124 4 $emitter->($_) for @$list;
125 } else {
126 13 $emitter->($list);
127 }
128 }
129  
130 #
131 # Gather output from cli_output
132 #
133 sub cli_capture_output {
134 0 ▶ (my MY $self, my ($subOrArrayOrString, @args)) = @_;
Subroutine cli_capture_output: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my @result;
136 local $self->{output} = sub {
137 0 ▶ push @result, \@_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 };
139 0 $self->cli_apply($subOrArrayOrString, @args);
140 0 @result;
141 }
142  
143 sub cli_examine_result {
144 5 ▶ (my MY $self, my $list) = @_;
Subroutine cli_examine_result: called CC=2 SCAR=6.9
Pod undocumented
145 5 ▶ if ($self->{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'scalar'}) { } 2 3 5
146 2 $list->[0];
147 } else {
148 3 @$list;
149 }
150 }
151  
152 #
153 # exit code handling
154 #
155 sub cli_exit_for_result {
156 5 ▶ (my MY $self, my $list) = @_;
Subroutine cli_exit_for_result: called CC=2 SCAR=6.9
Pod undocumented
157  
158 5 ▶ exit($self->cli_examine_result($list) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->cli_examine_result($list) ? : 3 2 5
159 }
160  
161 #========================================
162  
163 sub cli_array :method {
164 4 ▶ (my MY $self, my @args) = @_;
Subroutine cli_array: called CC=1 SCAR=0.0
Pod undocumented
165 4 \@args;
166 }
167  
168 sub cli_list :method {
169 4 ▶ (my MY $self, my @args) = @_;
Subroutine cli_list: called CC=1 SCAR=0.0
Pod undocumented
170 4 @args;
171 }
172  
173 sub cli_object :method {
174 1 ▶ (my MY $self, my %args) = @_;
Subroutine cli_object: called CC=1 SCAR=0.0
Pod undocumented
175 1 \%args;
176 }
177  
178 sub cli_identity :method {
179 2 ▶ (my MY $self, my ($thing)) = @_;
Subroutine cli_identity: called CC=1 SCAR=0.0
Pod undocumented
180 2 $thing;
181 }
182  
183 sub cli_map_apply :method {
184 0 ▶ (my MY $self, my ($subOrArray, @args)) = @_;
Subroutine cli_map_apply: not called CC=1 SCAR=6.9
Pod undocumented
185 map {
186 0 $self->cli_apply($subOrArray, $_);
187 } @args;
188 }
189  
190 sub cli_grep_apply :method {
191 0 ▶ (my MY $self, my ($subOrArray, @args)) = @_;
Subroutine cli_grep_apply: not called CC=1 SCAR=6.9
Pod undocumented
192 grep {
193 0 $self->cli_apply($subOrArray, $_);
194 } @args;
195 }
196  
197 # XXX: How about cli_reduce_apply?
198  
199 sub cli_apply :method {
200 1 ▶ (my MY $self, my ($subOrArrayOrString, @args)) = @_;
Subroutine cli_apply: called CC=6 SCAR=23.5
Pod undocumented
201 1 ▶ if (not defined $subOrArrayOrString) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $subOrArrayOrString) { } 0 1 1
elsif (ref $subOrArrayOrString eq 'CODE') { } 0 1 1
elsif (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') { } 1 0 1
202 0 Carp::croak "undefined sub for cli_apply";
203 } elsif (ref $subOrArrayOrString eq 'CODE') {
204 0 $subOrArrayOrString->(@args);
205 ▶ } elsif (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') {
Condition33%1/3
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
l!l&&r!l&&!r
100
MC/DC0%0/2
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
not ref $subOrArrayOrString ref $subOrArrayOrString eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $subOrArrayOrStringB= ref $subOrArrayOrString eq "ARRAY"
ABresult
1X1
011
000
206 1 my ($meth, @opts) = lexpand($subOrArrayOrString);
207 1 ▶ if (my $sub = $self->can("cmd_$meth")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->can("cmd_$meth")) { } 0 1 1
208 0 $sub->($self, @opts, @args);
209 } else {
210 1 $self->$meth(@opts, @args);
211 }
212 } else {
213 0 Carp::croak "Invalid argument for cli_apply: "
214 . MOP4Import::Util::terse_dump($subOrArrayOrString);
215 }
216 }
217  
218 sub cli_precheck_apply {
219 1 ▶ (my MY $self, my ($subOrArrayOrString)) = @_;
Subroutine cli_precheck_apply: called CC=7 SCAR=25.7
Pod undocumented
220 1 ▶ if (not defined $subOrArrayOrString) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $subOrArrayOrString) { } 0 1 1
elsif (ref $subOrArrayOrString eq 'CODE') { } 0 1 1
221 0 Carp::croak "undefined sub for cli_apply";
222 } elsif (ref $subOrArrayOrString eq 'CODE') {
223 0 1;
224 } else {
225 1 ▶ if (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY") 1 0 1
Condition33%1/3
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
l!l&&r!l&&!r
100
MC/DC0%0/2
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
not ref $subOrArrayOrString ref $subOrArrayOrString eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $subOrArrayOrStringB= ref $subOrArrayOrString eq "ARRAY"
ABresult
1X1
011
000
226 1 my ($meth, @opts) = lexpand($subOrArrayOrString);
227 1 ▶ return if $self->can("cmd_$meth") || $self->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("cmd_$meth") or $self->can($meth) 1 0 1
Condition33%1/3
$self->can("cmd_$meth") or $self->can($meth)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->can("cmd_$meth") or $self->can($meth)
$self->can("cmd_$meth") $self->can($meth)
Truth table33%1/3
A or B
A= $self->can("cmd_$meth")B= $self->can($meth)
ABresult
1X1
011
000
228 }
229 0 Carp::croak "Invalid argument for cli_apply: "
230 . MOP4Import::Util::terse_dump($subOrArrayOrString);
231 }
232 }
233  
234 use MOP4Import::Types
235 12 ▶ cliopts__xargs => [[fields => qw/null slurp single json decode/]];
Subroutine BEGIN: called CC=1 SCAR=0.0
236  
237 sub cli_xargs_json :method {
238 1 ▶ (my MY $self, my (@args)) = @_;
Subroutine cli_xargs_json: called CC=4 SCAR=15.6
Pod undocumented
239 1 my cliopts__xargs $opts = $self->take_locked_opts_of(
240 cliopts__xargs, \@args, {0 => 'null', input => 'decode'},
241 );
242 1 ▶ $opts->{decode} //= (($opts->{json} //=1) ? 'json' : '');
Branch: 1 branch
BranchTrueFalseTotal
($opts->{'json'} //= 1) ? : 1 0 1
Condition40%2/5
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
l!l&&r!l&&!r
010
$opts->{"json"} //= 1
l!l
01
MC/DC0%0/2
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"} ($opts->{"json"} //= 1) ? "json" : ""
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table33%1/3
A //= B
A= $opts->{"decode"}B= ($opts->{"json"} //= 1) ? "json" : ""
ABresult
1X1
011
000
Truth table50%1/2
A
A= $opts->{"json"}
Aresult
11
00
243 1 $self->_cli_xargs($opts, @args);
244 }
245  
246 BEGIN {
247 12 ▶ my ($packSuffix) = do {
Subroutine BEGIN: called CC=4 SCAR=15.0
248 12 ▶ if ($] >= 5.022) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.022) { } 15 0 15
249 12 'compat_double_diamond';
250 } else {
251 0 'compat_double_diamond_5_20';
252 }
253 };
254 12 (my $dir = __FILE__) =~ s,/?[^/]+\z,,;
255  
256 12 my $fn = File::Spec->rel2abs($dir) . "/../Util/$packSuffix.pm";
257 12 ▶ if (-r __FILE__ and not -r $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r __FILE__ and not -r $fn) 0 12 12
Condition33%1/3
-r __FILE__ and not -r $fn
!ll&&!rl&&r
0120
MC/DC0%0/2
-r __FILE__ and not -r $fn
-r __FILE__ not -r $fn
Truth table33%1/3
A and not(B)
A= -r __FILE__B= not -r $fn
ABresult
0X0
100
111
258 0 die "Can't load $fn";
259 }
260 12 do $fn;
261 12 my $pack = 'MOP4Import::Util::'.$packSuffix;
262 12 $pack->import;
263 12 print STDERR "compat_diamond is loaded from $fn\n" if DEBUG and DEBUG >= 2;
264 }
265  
266 sub _cli_xargs {
267 1 ▶ (my MY $self, my (@args)) = @_;
Subroutine _cli_xargs: called CC=12 SCAR=33.2
268 1 my cliopts__xargs $opts = $self->take_locked_opts_of(
269 cliopts__xargs, \@args, {0 => 'null'},
270 );
271 1 my ($subOrArray, @restPrefix) = @args;
272 1 $self->cli_precheck_apply($subOrArray);
273  
274 1 ▶ local $/ = $opts->{null} ? "\0" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'null'} ? : 0 1 1
275 1 local *ARGV;
276 1 ▶ if ($opts->{slurp} || $opts->{single}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'slurp'} or $opts->{'single'}) { } 0 1 1
Condition33%1/3
$opts->{"slurp"} or $opts->{"single"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts->{"slurp"} or $opts->{"single"}
$opts->{"slurp"} $opts->{"single"}
Truth table33%1/3
A or B
A= $opts->{"slurp"}B= $opts->{"single"}
ABresult
1X1
011
000
277 0 my @all = $self->cli_slurp_xargs($opts);
278 $self->cli_apply(
279 $subOrArray, @restPrefix,
280 0 ▶ ($opts->{single} ? \@all : @all)
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'single'} ? : 0 0 0
281 );
282 } else {
283 my $decoder = defined $opts->{decode}
284 1 ▶ ? $self->cli_decoder_from($opts->{decode}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'decode'} ? : 1 0 1
285 1 local $_;
286 1 ▶ if (not ref $subOrArray and $self->can("cmd_$subOrArray")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $subOrArray and $self->can("cmd_$subOrArray")) { } 0 1 1
Condition33%1/3
not ref $subOrArray and $self->can("cmd_$subOrArray")
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $subOrArray and $self->can("cmd_$subOrArray")
not ref $subOrArray $self->can("cmd_$subOrArray")
Truth table33%1/3
not(A) and B
A= not ref $subOrArrayB= $self->can("cmd_$subOrArray")
ABresult
0X0
100
111
287 0 ▶ while (defined($_ = $self->cli_compat_diamond)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->cli_compat_diamond)) 0 0 0
288 0 chomp;
289 0 ▶ $self->cli_apply(
Branch: 1 branch
BranchTrueFalseTotal
$decoder ? : 0 0 0
290 $subOrArray, @restPrefix,
291 ($decoder ? $decoder->($_) : $_)
292 )
293 }
294 0 $self->{'no-exit-code'} = 1;
295 0 ();
296 } else {
297 1 my @result;
298 1 ▶ while (defined($_ = $self->cli_compat_diamond)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->cli_compat_diamond)) 1 1 2
299 1 chomp;
300 # XXX: yield...
301 1 ▶ push @result, $self->cli_apply(
Branch: 1 branch
BranchTrueFalseTotal
$decoder ? : 1 0 1
302 $subOrArray, @restPrefix,
303 ($decoder ? $decoder->($_) : $_)
304 )
305 }
306 1 @result;
307 }
308 }
309 }
310  
311 sub cli_slurp_xargs_json {
312 0 ▶ (my MY $self, my (@args)) = @_;
Subroutine cli_slurp_xargs_json: not called CC=4 SCAR=30.0
Pod undocumented
313 0 my cliopts__xargs $opts = $self->take_locked_opts_of(
314 cliopts__xargs, \@args, {0 => 'null'},
315 );
316 0 ▶ $opts->{decode} //= (($opts->{json} //=1) ? 'json' : '');
Branch: 1 branch
BranchTrueFalseTotal
($opts->{'json'} //= 1) ? : 0 0 0
Condition0%0/5
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
l!l&&r!l&&!r
000
$opts->{"json"} //= 1
l!l
00
MC/DC0%0/2
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"} ($opts->{"json"} //= 1) ? "json" : ""
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table0%0/3
A //= B
A= $opts->{"decode"}B= ($opts->{"json"} //= 1) ? "json" : ""
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opts->{"json"}
Aresult
11
00
317 0 $self->cli_slurp_xargs($opts, @args);
318 }
319  
320 sub cli_slurp_xargs {
321 0 ▶ (my MY $self, my (@args)) = @_;
Subroutine cli_slurp_xargs: not called CC=3 SCAR=24.8
Pod undocumented
322 0 my cliopts__xargs $opts = $self->take_locked_opts_of(
323 cliopts__xargs, \@args, {0 => 'null'},
324 );
325  
326 0 local @ARGV = @args;
327 my $decoder = defined $opts->{decode}
328 0 ▶ ? $self->cli_decoder_from($opts->{decode}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'decode'} ? : 0 0 0
329  
330 map {
331 0 ▶ $decoder ? $decoder->($_) : $_
Branch: 1 branch
BranchTrueFalseTotal
$decoder ? : 0 0 0
332 } $self->cli_compat_diamond
333 }
334  
335 sub cli_decoder_from {
336 1 ▶ (my MY $self, my ($formatSpec, @rest)) = @_;
Subroutine cli_decoder_from: called CC=2 SCAR=7.0
Pod undocumented
337 1 my ($format, @opts) = lexpand($formatSpec);
338 1 ▶ my $sub = $self->can("cli_decoder_from__$format")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can("cli_decoder_from__$format") or Carp::croak("Unknown decorder is requested: $format") 0 1 1
339 or Carp::croak "Unknown decorder is requested: $format";
340 1 $sub->($self, @opts, @rest);
341 }
342  
343 #
344 # pass-through decoder.
345 #
346 sub cli_decoder_from__ {
347 0 ▶ sub {$_[0]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 0 ▶}
Subroutine cli_decoder_from__: not called CC=1 SCAR=6.9
Pod undocumented
349  
350 #
351 # json decoder
352 #
353 sub cli_decoder_from__json {
354 3 ▶ (my MY $self, my @opts) = @_;
Subroutine cli_decoder_from__json: called CC=1 SCAR=0.0
Pod undocumented
355 3 my $decoder = $self->cli_json_decoder(qw/allow_nonref/, @opts);
356 sub {
357 3 ▶ my ($str) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
358 3 Encode::_utf8_off($str);
359 3 $decoder->decode($str);
360 }
361 3 }
362  
363 #========================================
364  
365 sub declare_output_format {
366 73 ▶ (my $myPack, my Opts $opts, my ($formatName, $sub)) = m4i_args(@_);
Subroutine declare_output_format: called CC=6 SCAR=23.9
Pod undocumented
367 73 my $encoderFuncName = "cli_encoder_to__$formatName";
368 73 my $writeFuncName = "cli_write_fh_as_$formatName";
369 73 my $outputFuncName = "cli_output_as_$formatName";
370 73 ▶ if (ref $sub eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sub eq 'CODE') { } 1 72 73
elsif (not defined $sub) { } 72 0 72
371 1 *{globref($opts->{destpkg}, $writeFuncName)} = $sub;
372 1 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
373 0 ▶ shift->$writeFuncName(\*STDOUT, $_[0]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 1 };
375 } elsif (not defined $sub) {
376 72 ▶ if ($opts->{destpkg}->can($writeFuncName)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'destpkg'}->can($writeFuncName)) { } 72 0 72
elsif ($opts->{'destpkg'}->can($encoderFuncName)) { } 0 0 0
377 72 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
378 0 ▶ shift->$writeFuncName(\*STDOUT, $_[0]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 72 };
380 }
381 elsif ($opts->{destpkg}->can($encoderFuncName)) {
382 0 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
383 0 ▶ shift->$encoderFuncName(\*STDOUT)->($_[0]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
384 0 };
385  
386 0 ▶ unless ($opts->{destpkg}->can($writeFuncName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"destpkg"}->can($writeFuncName)) 0 0 0
387 0 *{globref($opts->{destpkg}, $writeFuncName)} = sub {
388 0 ▶ shift->$encoderFuncName(shift)->(\@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
389 0 };
390 }
391 }
392 else {
393 0 Carp::croak "output_format $formatName doesn't have method '$writeFuncName'";
394 }
395 } else {
396 0 Carp::croak "Invalid argument for output_format: "
397 . MOP4Import::Util::terse_dump($sub);
398 }
399 }
400  
401 # cli_write_fh($outFH, @output) -- Output format abstraction
402 # In cli_run context, cli_write_fh is called from cli_output($list) and
403 # used as cli_write_fh(\*STDOUT, $list).
404 # But in general, cli_write_fh can process multiple arguments at once.
405 #
406  
407 sub cli_write_fh {
408 17 ▶ (my MY $self, my ($outFH, @args)) = @_;
Subroutine cli_write_fh: called CC=10 SCAR=34.3
Pod undocumented
409  
410 17 my ($outputFmt, @opts) = lexpand($self->{'output'});
411  
412 17 ▶ if (my $sub = $self->can("cli_encoder_to__$outputFmt")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("cli_encoder_to__$outputFmt")) { } 0 17 17
elsif ($sub = $self->can('cli_write_fh_as_' . $outputFmt)) { } 17 0 17
413 0 my $encoder = $sub->($self, $outFH, @opts);
414 0 ▶ $encoder->($_) for ($self->{flatten} ? (map {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'flatten'} ? : 0 0 0
415 0 ▶ (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && ref $_ eq 'ARRAY' ? : 0 0 0
Condition0%0/3
defined $_ and ref $_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and ref $_ eq "ARRAY"
defined $_ ref $_ eq "ARRAY"
Truth table0%0/3
A and B
A= defined $_B= ref $_ eq "ARRAY"
ABresult
0X0
100
111
416 } @args) : @args);
417 }
418 elsif ($sub = $self->can("cli_write_fh_as_".$outputFmt)) {
419 $sub->($self, $outFH, $self->{flatten} ? (map {
420 17 ▶ (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_
Branch: 2 branches
BranchTrueFalseTotal
$self->{'flatten'} ? : 1 16 17
defined $_ && ref $_ eq 'ARRAY' ? : 1 0 1
Condition33%1/3
defined $_ and ref $_ eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_ and ref $_ eq "ARRAY"
defined $_ ref $_ eq "ARRAY"
Truth table33%1/3
A and B
A= defined $_B= ref $_ eq "ARRAY"
ABresult
0X0
100
111
421 } @args) : @args);
422 }
423 else {
424 0 Carp::croak("Unknown output format: $self->{'output'}");
425 }
426 }
427  
428 1 ▶sub cli_json { JSON() }
Subroutine cli_json: called CC=1 SCAR=0.0
Pod undocumented
429  
430 sub cli_decode_json {
431 2 ▶ (my MY $self, my $string) = @_;
Subroutine cli_decode_json: called CC=1 SCAR=0.0
Pod undocumented
432 2 $self->cli_decoder_from__json->($string);
433 }
434  
435 sub cli_encode_json {
436 23 ▶ (my MY $self, my ($obj, $json_type)) = @_;
Subroutine cli_encode_json: called CC=2 SCAR=7.0
Pod undocumented
437 23 my $json = $self->SUPER::cli_encode_json($obj, $json_type);
438 23 ▶ Encode::_utf8_on($json) unless $self->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"binary"} 23 0 23
439 23 $json;
440 }
441  
442 sub cli_json_encoder {
443 23 ▶ (my MY $self) = @_;
Subroutine cli_json_encoder: called CC=2 SCAR=7.0
Pod undocumented
444 23 my $js = $self->SUPER::cli_json_encoder;
445 23 ▶ $js->utf8 unless $self->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"binary"} 23 0 23
446 23 $js;
447 }
448  
449 sub cli_json_decoder {
450 5 ▶ (my MY $self, my @opts) = @_;
Subroutine cli_json_decoder: called CC=3 SCAR=11.0
Pod undocumented
451 5 my $js = JSON()->new->relaxed;
452 5 ▶ $js->utf8 unless $self->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"binary"} 5 0 5
453 5 foreach my $opt (@opts) {
454 3 my ($method, @args) = lexpand($opt);
455 3 $js->$method(@args);
456 }
457 5 $js;
458 }
459  
460 #----------------------------------------
461  
462 sub cli_encode_as {
463 2 ▶ (my MY $self, my ($outputSpec, @items)) = @_;
Subroutine cli_encode_as: called CC=8 SCAR=24.6
Pod undocumented
464 2 my ($outputFmt, $layer, @opts) = lexpand($outputSpec);
465 2 ▶ $outputFmt //= '';
Condition50%1/2
$outputFmt //= ""
l!l
20
MC/DC0%0/1
$outputFmt //= ""
$outputFmt
Truth table50%1/2
A
A= $outputFmt
Aresult
11
00
466 # Allow $layer to be a HASH
467 2 ▶ if (defined $layer and ref $layer eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $layer and ref $layer eq "HASH") 0 2 2
Condition66%2/3
defined $layer and ref $layer eq "HASH"
!ll&&!rl&&r
110
MC/DC0%0/2
defined $layer and ref $layer eq "HASH"
defined $layer ref $layer eq "HASH"
Truth table66%2/3
A and B
A= defined $layerB= ref $layer eq "HASH"
ABresult
0X0
100
111
468 0 my %opts = %$layer;
469 0 $layer = delete $opts{layer};
470 0 unshift @opts, %opts;
471 }
472 2 ▶ $layer //= '';
Condition100%2/2
$layer //= ""
l!l
11
MC/DC100%1/1
$layer //= ""
$layer
Truth table100%2/2
A
A= $layer
Aresult
11
00
473 2 my $buffer = "";
474 {
475 2 open my $outFH, ">$layer", \$buffer;
476 2 ▶ if (my $sub = $self->can("cli_encoder_to__$outputFmt")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("cli_encoder_to__$outputFmt")) { } 0 2 2
elsif ($sub = $self->can("cli_write_fh_as_$outputFmt")) { } 2 0 2
477 0 my $encoder = $sub->($self, $outFH, @opts);
478 0 foreach my $item (@items) {
479 0 $encoder->($item);
480 }
481 }
482 elsif ($sub = $self->can("cli_write_fh_as_$outputFmt")) {
483 2 $sub->($self, $outFH, \@items);
484 }
485 else {
486 0 Carp::croak "Unknown output format: '$outputFmt'";
487 }
488 }
489 2 $buffer;
490 }
491  
492 9 MY->declare_output_format(MY, 'ndjson');
493 sub cli_write_fh_as_ndjson {
494 7 ▶ (my MY $self, my ($outFH, @tables)) = @_;
Subroutine cli_write_fh_as_ndjson: called CC=7 SCAR=20.3
Pod undocumented
495 7 foreach my $table (@tables) {
496 7 ▶ foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) {
Branch: 1 branch
BranchTrueFalseTotal
ref $table eq 'ARRAY' ? : 6 1 7
497 12 ▶ print $outFH ((ref $item ? $self->cli_encode_json($item) : $item // $self->{'undef-as'} // ''), "\n");
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 6 6 12
Condition40%2/5
($item // $self->{"undef-as"}) // ""
l!l
60
$item // $self->{"undef-as"}
l!l&&r!l&&!r
600
MC/DC0%0/2
($item // $self->{"undef-as"}) // ""
$item $self->{"undef-as"}
Truth table33%1/3
A // B
A= $itemB= $self->{"undef-as"}
ABresult
1X1
011
000
498 }
499 }
500 }
501  
502 9 MY->declare_output_format(MY, 'json');
503 sub cli_write_fh_as_json {
504 3 ▶ (my MY $self, my ($outFH, @args)) = @_;
Subroutine cli_write_fh_as_json: called CC=2 SCAR=6.9
Pod undocumented
505 3 foreach my $item (@args) {
506 3 print $outFH $self->cli_encode_json($item), "\n";
507 }
508 }
509  
510 9 MY->declare_output_format(MY, 'yaml');
511 sub cli_write_fh_as_yaml {
512 1 ▶ (my MY $self, my ($outFH, @args)) = @_;
Subroutine cli_write_fh_as_yaml: called CC=1 SCAR=0.0
Pod undocumented
513 1 require YAML::Syck;
514 1 print $outFH YAML::Syck::Dump(@args);
515 }
516  
517 9 MY->declare_output_format(MY, 'dump');
518 sub cli_write_fh_as_dump {
519 2 ▶ (my MY $self, my ($outFH, @args)) = @_;
Subroutine cli_write_fh_as_dump: called CC=3 SCAR=11.0
Pod undocumented
520 2 foreach my $item (@args) {
521 2 my $dumper = Data::Dumper->new($item)
522 ->Terse(1)
523 ->Sortkeys(1)
524 ->Indent(1)
525 ->Deparse(1);
526 2 ▶ if (my $sub = $dumper->can("Trailingcomma")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $dumper->can("Trailingcomma")) 2 0 2
527 2 $sub->($dumper, 1);
528 }
529  
530 2 print $outFH $dumper->Dump;
531 }
532 }
533  
534 9 MY->declare_output_format(MY, 'raw');
535 sub cli_write_fh_as_raw {
536 1 ▶ (my MY $self, my ($outFH, @tables)) = @_;
Subroutine cli_write_fh_as_raw: called CC=4 SCAR=14.0
Pod undocumented
537 1 foreach my $table (@tables) {
538 1 ▶ foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) {
Branch: 1 branch
BranchTrueFalseTotal
ref $table eq 'ARRAY' ? : 1 0 1
539 1 print $outFH $item;
540 }
541 }
542 }
543  
544 9 MY->declare_output_format(MY, 'tsv');
545 sub cli_write_fh_as_tsv {
546 4 ▶ (my MY $self, my ($outFH, @tables)) = @_;
Subroutine cli_write_fh_as_tsv: called CC=6 SCAR=17.9
Pod undocumented
547 4 foreach my $table (@tables) {
548 4 foreach my $rec (@$table) {
549 print $outFH join("\t", map {
550 12 ▶ if (not defined $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_) { } 1 26 27
elsif (ref $_) { } 5 21 26
551 1 $self->{'undef-as'};
552 } elsif (ref $_) {
553 5 $self->cli_encode_json($_);
554 } else {
555 21 my $cp = $_;
556 21 $cp =~ s/[\t\n]+/ /g;
557 21 ▶ $cp
Branch: 1 branch
BranchTrueFalseTotal
ref $rec eq 'ARRAY' ? : 9 3 12
558 }
559 } ref $rec eq 'ARRAY' ? @$rec : $rec), "\n";
560 }
561 }
562 }
563  
564 #========================================
565  
566 sub cli_create_from_file :method {
567 0 ▶ my ($class, $configFn, @moreOpts) = @_;
Subroutine cli_create_from_file: not called CC=6 SCAR=37.4
Pod undocumented
568 0 my $realConfigFn = File::Spec->rel2abs($configFn);
569 0 ▶ my $oldcwd = $ENV{PWD} || do {require Cwd; Cwd::getcwd()};
Condition0%0/3
$ENV{'PWD'} || do { require Cwd; Cwd::getcwd() }
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PWD'} || do { require Cwd; Cwd::getcwd() }
$ENV{'PWD'} do { require Cwd; Cwd::getcwd() }
Truth table0%0/3
A || B
A= $ENV{'PWD'}B= do { require Cwd; Cwd::getcwd() }
ABresult
1X1
011
000
570 0 my $realDir = File::Basename::dirname($realConfigFn);
571 0 ▶ chdir($realDir)
Branch: 1 branch
BranchTrueFalseTotal
chdir $realDir or Carp::croak("Can't chdir to ${realDir}: $!") 0 0 0
572 or Carp::croak "Can't chdir to $realDir: $!";
573  
574 # Read $configFn with scalar context.
575 0 my $opts = $class->cli_read_file($realConfigFn);
576  
577 0 ▶ my $object = (ref $class || $class)->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq 'HASH' ? : 0 0 0
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
578 ref $opts eq 'HASH' ? %$opts : @$opts,
579 @moreOpts
580 );
581 0 ▶ chdir($oldcwd)
Branch: 1 branch
BranchTrueFalseTotal
chdir $oldcwd or Carp::croak("Can't chdir back to ${oldcwd}: $!") 0 0 0
582 or Carp::croak "Can't chdir back to $oldcwd: $!";
583 0 $object;
584 }
585  
586 sub cli_read_file :method {
587 7 ▶ my ($classOrObj, $fileNameSpec, %moreOpts) = @_;
Subroutine cli_read_file: called CC=3 SCAR=11.0
Pod undocumented
588 7 my ($fileName, %opts) = lexpand($fileNameSpec);
589 7 my ($ftype) = $fileName =~ m{\.(\w+)$};
590 7 ▶ $ftype //= "";
Condition100%2/2
$ftype //= ""
l!l
61
MC/DC100%1/1
$ftype //= ""
$ftype
Truth table100%2/2
A
A= $ftype
Aresult
11
00
591  
592 7 ▶ my $sub = $classOrObj->can("cli_read_file__$ftype")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $classOrObj->can("cli_read_file__$ftype") or Carp::croak("Unsupported file type '${ftype}': $fileName") 0 7 7
593 or Carp::croak "Unsupported file type '$ftype': $fileName";
594  
595 7 $sub->($classOrObj, $fileName, %opts, %moreOpts);
596 }
597  
598 # No filename extension => read entire content except last \n.
599 sub cli_read_file__ {
600 3 ▶ my ($classOrObj, $fileName) = @_;
Subroutine cli_read_file__: called CC=2 SCAR=7.0
Pod undocumented
601 3 ▶ open my $fh, '<:utf8', $fileName
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $fileName or Carp::croak("Can't open ${fileName}: $!") 0 3 3
602 or Carp::croak "Can't open $fileName: $!";
603 3 my $all = do {local $/; <$fh>};
604 3 chomp($all);
605 3 $all;
606 }
607  
608 # .txt => array of lines
609 sub cli_read_file__txt {
610 2 ▶ my ($classOrObj, $fileName) = @_;
Subroutine cli_read_file__txt: called CC=2 SCAR=6.9
Pod undocumented
611 2 my $all = $classOrObj->cli_read_file__($fileName);
612 2 my @list = split "\n", $all;
613 2 ▶ wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
614 }
615  
616 # .yml
617 9 *cli_read_file__yaml = *cli_read_file__yml;*cli_read_file__yaml = *cli_read_file__yml;
618 sub cli_read_file__yml {
619 2 ▶ my ($classOrObj, $fileName) = @_;
Subroutine cli_read_file__yml: called CC=1 SCAR=0.0
Pod undocumented
620 2 require YAML::Syck;
621 2 YAML::Syck::LoadFile($fileName);
622 }
623  
624 # .json
625 sub cli_read_file__json {
626 2 ▶ my ($classOrObj, $fileName, %opts) = @_;
Subroutine cli_read_file__json: called CC=7 SCAR=20.7
Pod undocumented
627 2 my $allow_comments = delete $opts{allow_comments};
628 2 ▶ open my $fh, '<', $fileName
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fileName or Carp::croak("Can't open ${fileName}: $!") 0 2 2
629 or Carp::croak "Can't open $fileName: $!";
630 2 my $all = do {local $/; <$fh>};
631 2 ▶ unless (defined $all) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $all) 0 2 2
632 0 Carp::croak "Can't read $fileName: $!";
633 }
634 2 ▶ if ($allow_comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_comments) 2 0 2
635 2 require MOP4Import::Util::CommentedJson;
636 2 local $@;
637 2 eval {
638 2 $all = MOP4Import::Util::CommentedJson->strip_json_comments($all);
639 };
640 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
641 0 Carp::carp "Can't strip comment in $fileName: $@";
642 }
643 }
644 2 local $@;
645 2 my @result;
646 2 eval {
647 2 @result = $classOrObj->cli_json_decoder->incr_parse($all);
648 };
649 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
650 0 Carp::croak "decode_json failed in $fileName: $@";
651 }
652 2 ▶ @result >= 2 ? \@result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
@result >= 2 ? : 0 2 2
653 }
654  
655 9 ▶MY->cli_run(\@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 9 9
656  
657 9 1;
« blib/lib/MOP4Import/Types.pm ↑ Summary Util.pm »