script/jsonfold.pl

filter: Statementstmt 71.1% 69 / 97 Branchbran 32.1% 9 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 15 / 21 Podpod n/a totaltotal 56.7% 93 / 164 CC 20 SCAR 18.6
CC
20
Cov
57%
main
14.0
stdout_width
12.0
get_config
7.1
CRAP 6.4
« blib/lib/JSON/JSONFold.pm ↑ Summary blib/lib/JSON/JSONFold/Config.pm »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.014 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use lib "$FindBin::RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::PP ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::JSONFold qw(jsonfold_config write_json) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=3.9
16 0 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
17 0 ▶ return if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
18 0 local $SIG{__DIE__};
19 0 Carp::confess(@_);
20 1 };
21  
22  
23 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 local $SIG{__WARN__};
25 0 Carp::cluck(@_);
26 1 };
27 }
28  
29 0 ▶sub demo_data {
Subroutine demo_data: not called CC=1 SCAR=6.9
30 0 return JSON::JSONFold::CLI::demo_data() ;
31 }
32  
33 1 ▶sub parse_options {
Subroutine parse_options: called CC=2 SCAR=7.0
34 1 my %cfg ;
35  
36 1 my %opt = (
37 compact => 'default',
38 indent => 2,
39 demo => 0,
40 verbose => 0,
41 sort_keys => 1,
42 help => 0,
43 cfg => \%cfg,
44 );
45  
46 GetOptions(
47 'demo' => \$opt{demo},
48 'verbose|v' => \$opt{verbose},
49 'help|h' => \$opt{help},
50 'input|i=s' => \$opt{input},
51 'compact=s' => \$opt{compact},
52 'indent=i' => \$opt{indent},
53 'native' => \$opt{native},
54 'sort-keys!' => \$cfg{sort_keys},
55  
56 'width=i' => \$cfg{width},
57 'pack-items=i' => \$cfg{pack_items},
58 'pack-array-items=i' => \$cfg{pack_array_items},
59 'pack-obj-items=i' => \$cfg{pack_obj_items},
60 'pack-nesting=i' => \$cfg{pack_nesting},
61 'fold-items=i' => \$cfg{fold_items},
62 'fold-array-items=i' => \$cfg{fold_array_items},
63 'fold-obj-items=i' => \$cfg{fold_obj_items},
64 'fold-nesting=i' => \$cfg{fold_nesting},
65 'join-items=i' => \$cfg{join_items},
66 'join-array-items=i' => \$cfg{join_array_items},
67 'join-obj-items=i' => \$cfg{join_obj_items},
68 'join-nesting=i' => \$cfg{join_nesting},
69 1 ▶ ) or die "Try --help\n";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("demo", \$opt{"demo"}, "verbose|v", \$opt{"verbose"}, "help|h", \$opt{"help"}, "input|i=s", \$opt{"input"}, "compact=s", \$opt{"compact"}, "indent=i", \$opt{"indent"}, "native", \$opt{"native"}, "sort-keys!", \$cfg{"sort_keys"}, "width=i", \$cfg{"width"}, "pack-items=i", \$cfg{"pack_items"}, "pack-array-items=i", \$cfg{"pack_array_items"}, "pack-obj-items=i", \$cfg{"pack_obj_items"}, "pack-nesting=i", \$cfg{"pack_nesting"}, "fold-items=i", \$cfg{"fold_items"}, "fold-array-items=i", \$cfg{"fold_array_items"}, "fold-obj-items=i", \$cfg{"fold_obj_items"}, "fold-nesting=i", \$cfg{"fold_nesting"}, "join-items=i", \$cfg{"join_items"}, "join-array-items=i", \$cfg{"join_array_items"}, "join-obj-items=i", \$cfg{"join_obj_items"}, "join-nesting=i", \$cfg{"join_nesting"}) or die "Try --help\n" 1 0 1
70  
71 1 return \%opt;
72 }
73  
74 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
75 0 my $out = shift ;
76 0 $out->print(<<___
77 Usage: json-jsonfold [options] < input.json
78  
79 --demo
80 --compact=default|none|low|med|high|max|pack|fold|join|off
81 --width=N
82 --indent=N
83 --sort-keys
84 --input=FILE
85 --pack-items=N / --pack-array-items=N / --pack-obj-items=N / --pack-nesting=N
86 --fold-items=N / --fold-array-items=N / --fold-obj-items=N / --fold-nesting=N
87 --join-items=N / --join-array-items=N / --join-obj-items=N / --join-nesting=N
88 ___
89 ) ;
90 }
91  
92 1 ▶sub read_input {
Subroutine read_input: called CC=4 SCAR=18.5
93 1 my ($input) = @_ ;
94  
95 1 my $json_text;
96 1 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 0 1 1
97 0 ▶ open my $fh, '<:encoding(UTF-8)', $input or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $input or die "${input}: $!\n" 0 0 0
98 0 local $/;
99 0 $json_text = <$fh>;
100 0 ▶ close $fh or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${input}: $!\n" 0 0 0
101 } else {
102 1 binmode STDIN, ":encoding(UTF-8)" ;
103 1 local $/;
104 1 $json_text = <STDIN>;
105 }
106  
107 1 return JSON::PP->new->allow_nonref->decode($json_text);
108 }
109  
110 1 ▶sub get_config {
Subroutine get_config: called CC=5 SCAR=19.5
111 1 my ($opt) = @_;
112  
113 1 my %cfg = %{$opt->{cfg}} ;
114  
115 1 for my $phase (qw(pack fold join)) {
116 3 my $k = "${phase}_items";
117 3 my $v = delete($cfg{$k}) ;
118 3 ▶ next unless defined($v) ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 3 0 3
119  
120 0 ▶ $cfg{"${phase}_array_items"} //= $v ;
Condition0%0/3
$cfg{"${phase}_array_items"} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$cfg{"${phase}_array_items"} //= $v
$cfg{"${phase}_array_items"} $v
Truth table0%0/3
A //= B
A= $cfg{"${phase}_array_items"}B= $v
ABresult
1X1
011
000
121 0 ▶ $cfg{"${phase}_obj_items"} //= $v ;
Condition0%0/3
$cfg{"${phase}_obj_items"} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$cfg{"${phase}_obj_items"} //= $v
$cfg{"${phase}_obj_items"} $v
Truth table0%0/3
A //= B
A= $cfg{"${phase}_obj_items"}B= $v
ABresult
1X1
011
000
122 }
123 # Get only set options
124 0 %cfg = map { ($_ => $cfg{$_}) } grep { defined $cfg{$_} } keys(%cfg) ;
125 # Temporary hack until we figure sort order.
126  
127 1 $cfg{sort_keys} = 1 ;
128  
129 1 my $config = jsonfold_config($opt->{compact}, $opt->{width}, %cfg);
130 1 return $config ;
131 }
132  
133 0 ▶sub show_verbose {
Subroutine show_verbose: not called CC=1 SCAR=6.9
134 0 my ($label) = shift ;
135 0 my $dumper = new Data::Dumper([])->Terse(1)->Indent(1)->Sortkeys(1)->Pair('=')->Quotekeys(0) ;
136  
137 0 my $s = $dumper->Values( \@_)->Dump ;
138 0 $s =~ s/\s+/ /gsm ;
139  
140 0 print STDERR "$label: $s\n" ;
141  
142 }
143  
144 0 ▶sub stdout_width {
Subroutine stdout_width: not called CC=3 SCAR=24.8
145 0 ▶ return unless -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDOUT 0 0 0
146  
147 0 eval {
148 0 require Term::ReadKey;
149 0 my ($cols) = Term::ReadKey::GetTerminalSize(*STDOUT);
150 0 ▶ return $cols if $cols;
Branch: 1 branch
BranchTrueFalseTotal
if $cols 0 0 0
151 };
152  
153 0 return $ENV{COLUMNS} ;
154 }
155  
156  
157 1 ▶sub main {
Subroutine main: called CC=9 SCAR=26.4
158 1 my $opt = parse_options();
159  
160 1 ▶ if ($opt->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 0 1 1
161 0 usage();
162 0 return 0;
163 }
164  
165 1 binmode STDOUT, ':encoding(UTF-8)' ;
166 1 my $verose = $opt->{verbose} ;
167  
168 1 ▶ my $data = $opt->{demo} ? demo_data() : read_input($opt->{input});
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'demo'} ? : 0 1 1
169 1 ▶ $opt->{cfg}{width} //= stdout_width() if -t STDOUT ;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDOUT 0 1 1
Condition0%0/3
$opt->{"cfg"}{"width"} //= stdout_width()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"cfg"}{"width"} //= stdout_width()
$opt->{"cfg"}{"width"} stdout_width()
Truth table0%0/3
A //= B
A= $opt->{"cfg"}{"width"}B= stdout_width()
ABresult
1X1
011
000
170  
171 1 my $cfg = get_config($opt);
172 1 my $verbose = $opt->{verbose} ;
173 1 my $native = $opt->{native} ;
174  
175 1 ▶ print STDERR "Backend: ", $JSON::JSONFold::BACKEND || "-", "\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
Condition0%0/2
$JSON::JSONFold::BACKEND || '-'
l!l
00
MC/DC0%0/1
$JSON::JSONFold::BACKEND || '-'
$JSON::JSONFold::BACKEND
Truth table0%0/2
A
A= $JSON::JSONFold::BACKEND
Aresult
11
00
176 1 ▶ show_verbose("config", { $cfg->as_hash }) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
177
178 1 my $info = write_json($data, \*STDOUT, $opt->{width}, $cfg, sort_keys => $opt->{sort_keys}, gold => !$native);
179  
180 1 ▶ show_verbose("stats", { % $info }) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
181 1 return 0;
182 }
183  
184 1 main() ;
« blib/lib/JSON/JSONFold.pm ↑ Summary blib/lib/JSON/JSONFold/Config.pm »