blib/lib/JSON/JQ.pm

filter: Statementstmt 93.2% 69 / 74 Branchbran 69.4% 25 / 36 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 83.5% 112 / 134 CC 21 SCAR 22.0
CC
21
Cov
84%
new
13.3
process
12.0
BEGIN
1.0
CRAP 9.0
↑ Summary Build »
1 package JSON::JQ;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 3 our $VERSION = '0.05';
7 # internal flags
8 3 our $DEBUG = 0;
9 3 our $DUMP_DISASM = 0;
10
11 3 ▶use FindBin ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 FindBin::again();
13 3 ▶use POSIX qw/isatty/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON qw/from_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 # jv_print_flags in jv.h
18 3 ▶use enum qw/BITMASK:JV_PRINT_ PRETTY ASCII COLOR SORTED INVALID REFCOUNT TAB ISATTY SPACE0 SPACE1 SPACE2/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # jq.h
20 3 ▶use enum qw/:JQ_DEBUG_=1 TRACE TRACE_DETAIL TRACE_ALL/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 3 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 XSLoader::load('JSON::JQ', $VERSION);
24
25 sub new {
26 35 ▶ my ( $pkg, $param ) = @_;
Subroutine new: called CC=12 SCAR=25.9
Pod documented
27
28 35 ▶ croak "script or script_file parameter required" unless exists $param->{script} or exists $param->{script_file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"script"} or exists $param->{"script_file"} 0 35 35
Condition33%1/3
exists $param->{"script"} or exists $param->{"script_file"}
l!l&&r!l&&!r
3500
MC/DC0%0/2
exists $param->{"script"} or exists $param->{"script_file"}
exists $param->{"script"} exists $param->{"script_file"}
Truth table33%1/3
A or B
A= exists $param->{"script"}B= exists $param->{"script_file"}
ABresult
1X1
011
000
29 35 my $self = {};
30 # script string or script file
31 35 ▶ $self->{script} = $param->{script} if exists $param->{script};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"script"} 35 0 35
32 35 ▶ $self->{script_file} = $param->{script_file} if exists $param->{script_file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"script_file"} 0 35 35
33 # script initial arguments
34 35 ▶ $self->{variable} = exists $param->{variable} ? $param->{variable} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'variable'} ? : 1 34 35
35 # internal attributes
36 35 ▶ $self->{_attribute}->{JQ_ORIGIN} = path($FindBin::Bin)->realpath->stringify if $FindBin::Bin;
Branch: 1 branch
BranchTrueFalseTotal
if $FindBin::Bin 35 0 35
37 $self->{_attribute}->{JQ_LIBRARY_PATH} = exists $param->{library_paths} ? $param->{library_paths} :
38 35 ▶ [ '~/.jq', '$ORIGIN/../lib/jq', '$ORIGIN/lib' ];
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'library_paths'} ? : 1 34 35
39 35 ▶ $self->{_attribute}->{PROGRAM_ORIGIN} = exists $param->{script_file} ? path($param->{script_file})->parent->stringify : '.';
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'script_file'} ? : 0 35 35
40 # error callback will push error messages into this array
41 35 $self->{_errors} = [];
42 # debug callback print flags
43 35 my $dump_opts = JV_PRINT_INDENT_FLAGS(2);
44 35 $dump_opts |= JV_PRINT_SORTED;
45 35 ▶ $dump_opts |= JV_PRINT_COLOR | JV_PRINT_ISATTY if isatty(*STDERR);
Branch: 1 branch
BranchTrueFalseTotal
if isatty(*STDERR) 0 35 35
46 35 $self->{_dumpopts} = $dump_opts;
47 # jq debug flags
48 35 ▶ $self->{jq_flags} = exists $param->{debug_flag} ? $param->{debug_flag} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'debug_flag'} ? : 0 35 35
49 35 bless $self, $pkg;
50 35 ▶ unless ($self->_init()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_init) 1 34 35
51 1 croak "jq_compile_args() failed with errors:\n ". join("\n ", @{ $self->{_errors} });
52 }
53 34 return $self;
54 }
55
56 sub process {
57 41 ▶ my ( $self, $param ) = @_;
Subroutine process: called CC=10 SCAR=24.8
Pod documented
58
59 41 my $input;
60 41 ▶ if (exists $param->{data}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $param->{'data'}) { } 40 1 41
elsif (exists $param->{'json'}) { } 1 0 1
elsif (exists $param->{'json_file'}) { } 0 0 0
61 40 $input = $param->{data};
62 }
63 elsif (exists $param->{json}) {
64 1 $input = from_json($param->{json}, { utf8 => 1 });
65 }
66 elsif (exists $param->{json_file}) {
67 0 my $file = path($param->{json_file});
68 0 $input = from_json($file->slurp, { utf8 => 1 });
69 }
70 else {
71 0 croak "JSON::JQ::process(): required parameter not found, check method documentation";
72 }
73 41 my $output = [];
74 41 my $rc = $self->_process($input, $output);
75 # treat it as option EXIT_STATUS is on
76 41 ▶ $rc -= 10 if $rc >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if $rc >= 10 6 35 41
77 41 ▶ if ($rc == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rc == 1) { } 4 37 41
elsif ($rc == 4 and @{$self->{'_errors'};} == 0) { } 2 35 37
elsif ($rc != 0) { } 0 35 35
78 # NOTE: treat this case as successful run
79 4 warn "JSON::JQ::process(): returned null/false (undef output), perhaps the input is undef.\n";
80 }
81 2 ▶ elsif ($rc == 4 and @{ $self->{_errors} } == 0) {
Condition66%2/3
$rc == 4 and @{$self->{"_errors"};} == 0
!ll&&!rl&&r
3502
MC/DC50%1/2
$rc == 4 and @{$self->{"_errors"};} == 0
$rc == 4 @{$self->{"_errors"};} == 0
Truth table66%2/3
A and B
A= $rc == 4B= @{$self->{"_errors"};} == 0
ABresult
0X0
100
111
82 # treat it as succeeded
83 2 push @$output, undef;
84 }
85 elsif ($rc != 0) {
86 0 croak "JSON::JQ::process(): failed with return code = $rc and errors:\n ". join("\n ", @{ $self->{_errors} });
87 }
88 41 ▶ return wantarray ? @$output : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 36 5 41
89 }
90
91 =head1 NAME
92
93 JSON::JQ - jq (https://stedolan.github.io/jq/) library binding
94
95 =head1 SYNOPSIS
96
97 use JSON::JQ;
98 my $jq = JSON::JQ->new({ script => '.' });
99 # 1. process perl data
100 my $results = $jq->process({ data => { foo => 'bar' }});
101 # 2. process json string
102 my $results = $jq->process({ json => '{ "foo": "bar" }'});
103 # 3. process json file
104 my $results = $jq->process({ json_file => 'foo.json' });
105 # check items in @$results
106
107
108 =head1 DESCRIPTION
109
110 This is a L<jq|https://stedolan.github.io/jq/> library binding, making it possible to process
111 data using jq script/filter/module. Check the jq homepage for a detailed explanation and documentation.
112
113
114 =head1 METHODS
115
116 =head2 new({ parameter => value, ... })
117
118 Construct a jq engine instance and return it, jq script must be provided by either I<script> or
119 I<script_file> parameter.
120
121 =over 4
122
123 =item * script
124
125 A string of jq script. The simplest one is '.', which does data 'echo'.
126
127 =item * script_file
128
129 A path to the file which contains the jq script. Shell-bang on first line will be ignored safely.
130
131 =item * variable
132
133 A hash reference with pre-defined variables and their values, they can be used by the jq script
134 later. Complex data structures like nested arrays and/or hashes are acceptable.
135
136 Check the jq official documentation on how to reference variables inside script.
137
138 =item * library_paths
139
140 An array reference with one or more directory paths inside. They will be used to search jq
141 library/module when needed.
142
143 The default search paths are I<'~/.jq'> I<'$ORIGIN/../lib/jq'> I<'$ORIGIN/lib'>, which
144 confirm with jq executable.
145
146 Check the jq official documentation on how to use this functionality in script.
147
148 =back
149
150 =head2 process({ parameter => value, ... })
151
152 Process given input and return results as array reference. The input data must be provided
153 via one of the parameters below.
154
155 =over 4
156
157 =item * data
158
159 A Perl variable representing JSON formed data. The straight way to understand its equivalent
160 is the return of C<JSON::from_json> call.
161
162 Any other type of data which the jq engine will accept can do. Such as I<undef>, which says Null
163 input. It is useful when the output data is created solely by a script itself.
164
165 Bear in mind that the jq engine cannot understand (blessed) perl objects, with one exception - objects
166 returned via C<JSON::true()> or C<JSON::false()>. They will be handled by underlying XS code
167 properly before passing them to the jq engine.
168
169 Check I<SPECIAL DATA MAPPING> section below.
170
171 =item * json
172
173 A json encoded string. It will be decoded using C<JSON::from_json> before handling to jq engine.
174 Which also means, it must fully conform with the JSON specification.
175
176 =item * json_file
177
178 Similar to I<json> parameter above, instead read the JSON string from given file.
179
180 =back
181
182 =head1 SPECIAL DATA MAPPING
183
184 The following JSON values are mapped to corresponding Perl values:
185
186 =over 4
187
188 =item * true: C<JSON::true>
189
190 =item * false: C<JSON::false>
191
192 =item * null: C<undef>
193
194 =back
195
196 =head1 CALLBACKS
197
198 The following jq engine callbacks are implemented:
199
200 =over 4
201
202 =item error callback
203
204 Any error message raised by jq engine during its initialization and execution will be
205 pushed into perl instance's private I<_error> attribute. It is transparent to user,
206 each method will croak on critical errors and show them.
207
208 =item debug callback
209
210 This is a builtin debug feature of the engine. It prints out debug messages when triggered.
211 Check the jq official documentation for more details.
212
213 =back
214
215 =head1 DEBUG
216
217 Limited debug functionality is implemented via the following module variables:
218
219 =over 4
220
221 =item C<$JSON::JQ::DUMP_DISASM> [default: off]
222
223 When on, print out the jq script disassembly code using C<jq_dump_disassembly>.
224
225 =item C<$JSON::JQ::DEBUG> [default: off]
226
227 B<Internal use only>. When on, print out debug messages from XS code.
228
229 =back
230
231 =head1 BUGS
232
233 Please report bugs to L<https://github.com/dxma/perl5-json-jq/issues>.
234
235 =head1 AUTHOR
236
237 Dongxu Ma
238 CPAN ID: DONGXU
239 dongxu _dot_ ma _at_ gmail.com
240 https://github.com/dxma
241
242 =head1 COPYRIGHT
243
244 This program is free software licensed under the...
245
246 The MIT License
247
248 The full text of the license can be found in the
249 LICENSE file included with this module.
250
251
252 =head1 SEE ALSO
253
254 =over
255
256 =item L<jq official wiki|https://github.com/stedolan/jq/wiki>
257
258 =back
259
260 =cut
261
262 3 1;
↑ Summary Build »