blib/lib/JQ/Lite/Util/Paths.pm

filter: Statementstmt 87.4% 446 / 510 Branchbran 67.2% 277 / 412 Conditioncond 49.4% 41 / 83 MC/DCmcdc 26.4% 14 / 53 Subroutinesub 97.6% 41 / 42 Podpod n/a totaltotal 74.4% 819 / 1100 CC 256 SCAR 27.2
CC
256
Cov
76%
_get_path_value
46.5
_resolve_paths_from_expr
35.9
_set_path_value
26.4
CRAP 15.2
« blib/lib/JQ/Lite/Expression.pm ↑ Summary bin/jq-lite »
1 package JQ::Lite::Util;
2  
3 182 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 182 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 182 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 182 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶sub _apply_assignment {
Subroutine _apply_assignment: called CC=13 SCAR=26.3
10 18 my ($self, $item, $path, $value_spec, $operator) = @_;
11  
12 18 ▶ return $item unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 18 18
13 18 ▶ return $item unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 2 16 18
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
0216
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
14  
15 16 ▶ $operator //= '=';
Condition50%1/2
$operator //= "="
l!l
160
MC/DC0%0/1
$operator //= "="
$operator
Truth table50%1/2
A
A= $operator
Aresult
11
00
16  
17 16 my $value = _resolve_assignment_value($self, $item, $value_spec);
18  
19 16 ▶ if ($operator ne '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator ne "=") 6 10 16
20 6 my $current = _clone_for_assignment(_get_path_value($item, $path));
21 6 my $current_num = _coerce_number($current);
22 6 my $value_num = _coerce_number($value);
23  
24 6 ▶ return $item unless defined $current_num && defined $value_num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_num and defined $value_num 0 6 6
Condition33%1/3
defined $current_num and defined $value_num
!ll&&!rl&&r
006
MC/DC0%0/2
defined $current_num and defined $value_num
defined $current_num defined $value_num
Truth table33%1/3
A and B
A= defined $current_numB= defined $value_num
ABresult
0X0
100
111
25  
26 6 my $result;
27 6 ▶ if ($operator eq '+=') {
Branch: 4 branches
BranchTrueFalseTotal
if ($operator eq '+=') { } 2 4 6
elsif ($operator eq '-=') { } 1 3 4
elsif ($operator eq '*=') { } 1 2 3
elsif ($operator eq '/=') { } 2 0 2
28 2 $result = $current_num + $value_num;
29 }
30 elsif ($operator eq '-=') {
31 1 $result = $current_num - $value_num;
32 }
33 elsif ($operator eq '*=') {
34 1 $result = $current_num * $value_num;
35 }
36 elsif ($operator eq '/=') {
37 2 ▶ return $item if $value_num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value_num == 0 1 1 2
38 1 $result = $current_num / $value_num;
39 }
40 else {
41 0 return $item;
42 }
43  
44 5 $value = $result;
45 }
46  
47 15 _set_path_value($item, $path, $value);
48  
49 15 return $item;
50 }
51  
52 6 ▶sub _get_path_value {
Subroutine _get_path_value: called CC=15 SCAR=38.4
53 6 my ($target, $path) = @_;
54  
55 6 ▶ return undef unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 6 6
56 6 ▶ return undef unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 6 6
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
006
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
57  
58 6 my @segments = _parse_path_segments($path);
59 6 ▶ return undef unless @segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @segments 0 6 6
60  
61 6 my $cursor = $target;
62 6 for my $index (0 .. $#segments) {
63 12 my $segment = $segments[$index];
64 12 my $is_last = ($index == $#segments);
65  
66 12 ▶ if ($segment->{type} eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->{"type"} eq "key") 12 0 12
67 12 ▶ return undef unless ref $cursor eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cursor eq "HASH" 0 12 12
68 12 my $key = $segment->{value};
69  
70 12 ▶ return $cursor->{$key} if $is_last;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last 6 6 12
71  
72 6 ▶ return undef unless exists $cursor->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cursor->{$key} 0 6 6
73 6 $cursor = $cursor->{$key};
74 6 next;
75 }
76  
77 0 ▶ if ($segment->{type} eq 'index') {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->{"type"} eq "index") 0 0 0
78 0 ▶ return undef unless ref $cursor eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cursor eq "ARRAY" 0 0 0
79  
80 0 my $numeric = _normalize_path_array_index($segment->{value}, scalar @$cursor);
81  
82 0 ▶ return undef if $numeric < 0 || $numeric > $#$cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric < 0 or $numeric > $cursor->$#* 0 0 0
Condition0%0/3
$numeric < 0 or $numeric > $cursor->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
$numeric < 0 or $numeric > $cursor->$#*
$numeric < 0 $numeric > $cursor->$#*
Truth table0%0/3
A or B
A= $numeric < 0B= $numeric > $cursor->$#*
ABresult
1X1
011
000
83  
84 0 ▶ return $cursor->[$numeric] if $is_last;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last 0 0 0
85  
86 0 $cursor = $cursor->[$numeric];
87 0 next;
88 }
89 }
90  
91 0 return undef;
92 }
93  
94 12 ▶sub _coerce_number {
Subroutine _coerce_number: called CC=5 SCAR=19.4
95 12 my ($value) = @_;
96  
97 12 ▶ return 0 if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 11 12
98  
99 11 ▶ if (JSON::PP::is_bool($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($value)) 0 11 11
100 0 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
101 }
102  
103 11 ▶ return 0 + $value if looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($value) 11 0 11
104  
105 0 return undef;
106 }
107  
108 16 ▶sub _resolve_assignment_value {
Subroutine _resolve_assignment_value: called CC=13 SCAR=32.0
109 16 my ($self, $item, $value_spec) = @_;
110  
111 16 ▶ return undef unless defined $value_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_spec 0 16 16
112  
113 16 ▶ if ($value_spec->{type} && $value_spec->{type} eq 'path') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_spec->{"type"} and $value_spec->{"type"} eq "path") 1 15 16
Condition66%2/3
$value_spec->{"type"} and $value_spec->{"type"} eq "path"
!ll&&!rl&&r
0151
MC/DC50%1/2
$value_spec->{"type"} and $value_spec->{"type"} eq "path"
$value_spec->{"type"} $value_spec->{"type"} eq "path"
Truth table66%2/3
A and B
A= $value_spec->{"type"}B= $value_spec->{"type"} eq "path"
ABresult
0X0
100
111
114 1 ▶ my $path = $value_spec->{value} // '';
Condition50%1/2
$value_spec->{"value"} // ""
l!l
10
MC/DC0%0/1
$value_spec->{"value"} // ""
$value_spec->{"value"}
Truth table50%1/2
A
A= $value_spec->{"value"}
Aresult
11
00
115 1 $path =~ s/^\.//;
116  
117 1 my @values = _traverse($item, $path);
118 1 return _clone_for_assignment($values[0]);
119 }
120  
121 15 ▶ if ($value_spec->{type} && $value_spec->{type} eq 'expression') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_spec->{"type"} and $value_spec->{"type"} eq "expression") 1 14 15
Condition66%2/3
$value_spec->{"type"} and $value_spec->{"type"} eq "expression"
!ll&&!rl&&r
0141
MC/DC50%1/2
$value_spec->{"type"} and $value_spec->{"type"} eq "expression"
$value_spec->{"type"} $value_spec->{"type"} eq "expression"
Truth table66%2/3
A and B
A= $value_spec->{"type"}B= $value_spec->{"type"} eq "expression"
ABresult
0X0
100
111
122 1 ▶ my $expr = $value_spec->{value} // '';
Condition50%1/2
$value_spec->{"value"} // ""
l!l
10
MC/DC0%0/1
$value_spec->{"value"} // ""
$value_spec->{"value"}
Truth table50%1/2
A
A= $value_spec->{"value"}
Aresult
11
00
123  
124 1 my ($values, $ok) = _evaluate_value_expression($self, $item, $expr);
125 1 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
126 1 ▶ return _clone_for_assignment(@$values ? $values->[0] : undef);
Branch: 1 branch
BranchTrueFalseTotal
@$values ? : 1 0 1
127 }
128  
129 0 ▶ if (defined $self && $self->can('run_query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self and $self->can("run_query")) 0 0 0
Condition0%0/3
defined $self and $self->can("run_query")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and $self->can("run_query")
defined $self $self->can("run_query")
Truth table0%0/3
A and B
A= defined $selfB= $self->can("run_query")
ABresult
0X0
100
111
130 0 my @outputs = $self->run_query(_encode_json($item), $expr);
131 0 ▶ return _clone_for_assignment($outputs[0]) if @outputs;
Branch: 1 branch
BranchTrueFalseTotal
if @outputs 0 0 0
132 }
133  
134 0 return _clone_for_assignment($expr);
135 }
136  
137 14 return _clone_for_assignment($value_spec->{value});
138 }
139  
140 15 ▶sub _set_path_value {
Subroutine _set_path_value: called CC=16 SCAR=32.8
141 15 my ($target, $path, $value) = @_;
142  
143 15 ▶ return unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 15 15
144  
145 15 my @segments = _parse_path_segments($path);
146 15 ▶ return unless @segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @segments 0 15 15
147  
148 15 my $cursor = $target;
149 15 for my $index (0 .. $#segments) {
150 29 my $segment = $segments[$index];
151 29 my $is_last = ($index == $#segments);
152  
153 29 ▶ if ($segment->{type} eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->{"type"} eq "key") 27 2 29
154 27 ▶ return unless ref $cursor eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cursor eq "HASH" 0 27 27
155 27 my $key = $segment->{value};
156  
157 27 ▶ if ($is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_last) 15 12 27
158 15 $cursor->{$key} = $value;
159 15 last;
160 }
161  
162 12 ▶ if (!exists $cursor->{$key} || !defined $cursor->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cursor->{$key} and defined $cursor->{$key}) 0 12 12
Condition33%1/3
exists $cursor->{$key} and defined $cursor->{$key}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $cursor->{$key} and defined $cursor->{$key}
exists $cursor->{$key} defined $cursor->{$key}
Truth table33%1/3
A and B
A= exists $cursor->{$key}B= defined $cursor->{$key}
ABresult
0X0
100
111
163 0 my $next = $segments[$index + 1];
164 0 ▶ $cursor->{$key} = ($next->{type} eq 'index') ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$next->{'type'} eq 'index' ? : 0 0 0
165 }
166  
167 12 $cursor = $cursor->{$key};
168 12 next;
169 }
170  
171 2 ▶ if ($segment->{type} eq 'index') {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->{"type"} eq "index") 2 0 2
172 2 ▶ return unless ref $cursor eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cursor eq "ARRAY" 0 2 2
173  
174 2 my $numeric = _normalize_path_array_index($segment->{value}, scalar @$cursor);
175  
176 2 ▶ return if $numeric < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric < 0 0 2 2
177  
178 2 ▶ if ($is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_last) 0 2 2
179 0 $cursor->[$numeric] = $value;
180 0 last;
181 }
182  
183 2 ▶ if (!defined $cursor->[$numeric]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cursor->[$numeric]) 0 2 2
184 0 my $next = $segments[$index + 1];
185 0 ▶ $cursor->[$numeric] = ($next->{type} eq 'index') ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$next->{'type'} eq 'index' ? : 0 0 0
186 }
187  
188 2 $cursor = $cursor->[$numeric];
189 2 next;
190 }
191 }
192  
193 15 return;
194 }
195  
196 2 ▶sub _normalize_path_array_index {
Subroutine _normalize_path_array_index: called CC=3 SCAR=11.3
197 2 my ($idx, $array_size) = @_;
198  
199 2 my $numeric = int($idx);
200 2 ▶ if ($idx =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx =~ /^-?\d+$/) 2 0 2
201 2 ▶ $numeric += $array_size if $numeric < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric < 0 0 2 2
202 }
203  
204 2 return $numeric;
205 }
206  
207 21 ▶sub _parse_path_segments {
Subroutine _parse_path_segments: called CC=7 SCAR=19.7
208 21 my ($path) = @_;
209  
210 21 ▶ $path //= '';
Condition50%1/2
$path //= ""
l!l
210
MC/DC0%0/1
$path //= ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
211 21 $path =~ s/^\s+|\s+$//g;
212  
213 21 my @segments;
214 21 for my $chunk (split /\./, $path) {
215 40 ▶ next if $chunk eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $chunk eq "" 0 40 40
216  
217 40 ▶ while (length $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chunk) 41 40 81
218 41 ▶ if ($chunk =~ s/^\[(\-?\d+)\]//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ s/^\[(\-?\d+)\]//) 2 39 41
219 2 push @segments, { type => 'index', value => $1 };
220 2 next;
221 }
222  
223 39 ▶ if ($chunk =~ s/^([^\[]+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ s/^([^\[]+)//) 39 0 39
224 39 push @segments, { type => 'key', value => $1 };
225 39 next;
226 }
227  
228 0 last;
229 }
230 }
231  
232 21 return @segments;
233 }
234  
235 22 ▶sub _clone_for_assignment {
Subroutine _clone_for_assignment: called CC=3 SCAR=12.0
236 22 my ($value) = @_;
237  
238 22 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 20 22
239 20 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 20 0 20
240  
241 0 my $json = _encode_json($value);
242 0 return _decode_json($json);
243 }
244  
245 0 ▶sub _map {
Subroutine _map: not called CC=3 SCAR=24.8
246 0 my ($self, $data, $filter) = @_;
247  
248 0 ▶ if (ref $data ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "ARRAY") 0 0 0
249 0 warn "_map expects array reference";
250 0 return ();
251 }
252  
253 0 my @mapped;
254 0 for my $item (@$data) {
255 0 push @mapped, $self->run_query(_encode_json($item), $filter);
256 }
257  
258 0 return @mapped;
259 }
260  
261 6 ▶sub _apply_all {
Subroutine _apply_all: called CC=12 SCAR=27.6
262 6 my ($self, $value, $expr) = @_;
263  
264 6 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 5 1 6
265 5 ▶ return JSON::PP::true unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 1 4 5
266  
267 4 for my $item (@$value) {
268 9 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) { } 4 5 9
269 4 my @evaluated = $self->run_query(_encode_json($item), $expr);
270 4 ▶ return JSON::PP::false unless @evaluated;
Branch: 1 branch
BranchTrueFalseTotal
unless @evaluated 0 4 4
271 4 ▶ return JSON::PP::false if grep { !_is_truthy($_) } @evaluated;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not _is_truthy($_);} @evaluated 1 3 4
272 }
273 else {
274 5 ▶ return JSON::PP::false unless _is_truthy($item);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_truthy($item) 1 4 5
275 }
276 }
277  
278 2 return JSON::PP::true;
279 }
280  
281 1 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) 0 1 1
282 0 my @evaluated = $self->run_query(_encode_json($value), $expr);
283 0 ▶ return JSON::PP::false unless @evaluated;
Branch: 1 branch
BranchTrueFalseTotal
unless @evaluated 0 0 0
284 0 ▶ return grep { !_is_truthy($_) } @evaluated ? JSON::PP::false : JSON::PP::true;
Branch: 1 branch
BranchTrueFalseTotal
@evaluated ? : 0 0 0
285 }
286  
287 1 ▶ return _is_truthy($value) ? JSON::PP::true : JSON::PP::false;
Branch: 1 branch
BranchTrueFalseTotal
_is_truthy($value) ? : 0 1 1
288 }
289  
290 6 ▶sub _apply_any {
Subroutine _apply_any: called CC=10 SCAR=24.1
291 6 my ($self, $value, $expr) = @_;
292  
293 6 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 4 2 6
294 4 ▶ return JSON::PP::false unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 0 4 4
295  
296 4 for my $item (@$value) {
297 9 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) { } 4 5 9
298 4 my @evaluated = $self->run_query(_encode_json($item), $expr);
299 4 ▶ return JSON::PP::true if grep { _is_truthy($_) } @evaluated;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_is_truthy($_);} @evaluated 1 3 4
300 }
301 else {
302 5 ▶ return JSON::PP::true if _is_truthy($item);
Branch: 1 branch
BranchTrueFalseTotal
if _is_truthy($item) 1 4 5
303 }
304 }
305  
306 2 return JSON::PP::false;
307 }
308  
309 2 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) 0 2 2
310 0 my @evaluated = $self->run_query(_encode_json($value), $expr);
311 0 ▶ return grep { _is_truthy($_) } @evaluated ? JSON::PP::true : JSON::PP::false;
Branch: 1 branch
BranchTrueFalseTotal
@evaluated ? : 0 0 0
312 }
313  
314 2 ▶ return _is_truthy($value) ? JSON::PP::true : JSON::PP::false;
Branch: 1 branch
BranchTrueFalseTotal
_is_truthy($value) ? : 1 1 2
315 }
316  
317 46 ▶sub _is_truthy {
Subroutine _is_truthy: called CC=12 SCAR=25.7
318 46 my ($value) = @_;
319  
320 46 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 42 46
321  
322 42 ▶ if (JSON::PP::is_bool($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($value)) 34 8 42
323 34 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 17 17 34
324 }
325  
326 8 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 2 6 8
327 2 ▶ return @$value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$value ? : 1 1 2
328 }
329  
330 6 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 0 6 6
331 0 ▶ return scalar(keys %$value) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$value ? : 0 0 0
332 }
333  
334 6 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 6 0 6
335 6 ▶ return 0 if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 6 6
336 6 ▶ if (looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($value)) 4 2 6
337 4 ▶ return $value != 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value != 0 ? : 2 2 4
338 }
339 2 return 1;
340 }
341  
342 0 return 1;
343 }
344  
345 37 ▶sub _apply_case_transform {
Subroutine _apply_case_transform: called CC=6 SCAR=18.0
346 37 my ($value, $mode) = @_;
347  
348 37 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 37 37
349 0 return undef;
350 }
351  
352 37 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 6 31 37
353 6 return [ map { _apply_case_transform($_, $mode) } @$value ];
354 }
355  
356 31 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 29 2 31
357 29 ▶ return uc $value if $mode eq 'upper';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "upper" 12 17 29
358 17 ▶ return lc $value if $mode eq 'lower';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "lower" 11 6 17
359 6 return _to_titlecase($value);
360 }
361  
362 2 return $value;
363 }
364  
365 12 ▶sub _apply_ascii_case_transform {
Subroutine _apply_ascii_case_transform: called CC=6 SCAR=18.5
366 12 my ($value, $mode) = @_;
367  
368 12 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 12 12
369 0 return undef;
370 }
371  
372 12 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 2 10 12
373 2 return [ map { _apply_ascii_case_transform($_, $mode) } @$value ];
374 }
375  
376 10 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 10 0 10
377 10 my $copy = $value;
378 10 ▶ if ($mode eq 'upper') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'upper') { } 5 5 10
elsif ($mode eq 'lower') { } 5 0 5
379 5 $copy =~ tr/a-z/A-Z/;
380 }
381 elsif ($mode eq 'lower') {
382 5 $copy =~ tr/A-Z/a-z/;
383 }
384 10 return $copy;
385 }
386  
387 0 return $value;
388 }
389  
390 6 ▶sub _to_titlecase {
Subroutine _to_titlecase: called CC=1 SCAR=0.0
391 6 my ($value) = @_;
392  
393 6 my $result = lc $value;
394 6 $result =~ s/(^|[^\p{L}\p{N}])(\p{L})/$1 . uc($2)/ge;
395 6 return $result;
396 }
397  
398 19 ▶sub _apply_trim {
Subroutine _apply_trim: called CC=4 SCAR=13.9
399 19 my ($value) = @_;
400  
401 19 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 18 19
402 1 return undef;
403 }
404  
405 18 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 13 5 18
406 13 my $copy = $value;
407 13 $copy =~ s/^\s+//;
408 13 $copy =~ s/\s+$//;
409 13 return $copy;
410 }
411  
412 5 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 4 1 5
413 4 return [ map { _apply_trim($_) } @$value ];
414 }
415  
416 1 return $value;
417 }
418  
419 34 ▶sub _apply_trimstr {
Subroutine _apply_trimstr: called CC=12 SCAR=24.9
420 34 my ($value, $needle, $mode) = @_;
421  
422 34 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 32 34
423 2 return undef;
424 }
425  
426 32 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 4 28 32
427 4 return [ map { _apply_trimstr($_, $needle, $mode) } @$value ];
428 }
429  
430 28 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 5 23 28
431 5 return $value;
432 }
433  
434 23 ▶ return $value if !_is_string_scalar($value);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_string_scalar($value) 4 19 23
435  
436 19 ▶ $needle = '' unless defined $needle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $needle 0 19 19
437 19 my $target = "$value";
438 19 my $pattern = "$needle";
439 19 my $len = length $pattern;
440  
441 19 ▶ return $target if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 2 17 19
442  
443 17 ▶ if ($mode eq 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "left") 9 8 17
444 9 ▶ return $target if index($target, $pattern) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($target, $pattern) != 0 4 5 9
445 5 return substr($target, $len);
446 }
447  
448 8 ▶ if ($mode eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "right") 8 0 8
449 8 ▶ return $target if $len > length($target);
Branch: 1 branch
BranchTrueFalseTotal
if $len > length $target 2 6 8
450 6 ▶ return $target unless substr($target, -$len) eq $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($target, -$len) eq $pattern 2 4 6
451 4 return substr($target, 0, length($target) - $len);
452 }
453  
454 0 return $target;
455 }
456  
457 5 ▶sub _apply_paths {
Subroutine _apply_paths: called CC=3 SCAR=11.0
458 5 my ($value) = @_;
459  
460 5 ▶ if (!ref $value || JSON::PP::is_bool($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value or JSON::PP::is_bool($value)) 2 3 5
Condition66%2/3
not ref $value or JSON::PP::is_bool($value)
l!l&&r!l&&!r
203
MC/DC50%1/2
not ref $value or JSON::PP::is_bool($value)
not ref $value JSON::PP::is_bool($value)
Truth table66%2/3
not(A) or B
A= not ref $valueB= JSON::PP::is_bool($value)
ABresult
1X1
011
000
461 2 return [];
462 }
463  
464 3 my @paths;
465 3 _collect_paths($value, [], \@paths);
466 3 return \@paths;
467 }
468  
469 3 ▶sub _apply_scalar_paths {
Subroutine _apply_scalar_paths: called CC=2 SCAR=6.9
470 3 my ($value) = @_;
471  
472 3 ▶ return [] if _is_scalar_value($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_scalar_value($value) 2 1 3
473  
474 1 my @paths;
475 1 _collect_scalar_paths($value, [], \@paths);
476 1 return \@paths;
477 }
478  
479 6 ▶sub _apply_leaf_paths {
Subroutine _apply_leaf_paths: called CC=2 SCAR=6.9
480 6 my ($value) = @_;
481  
482 6 ▶ if (_is_leaf_value($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_leaf_value($value)) 2 4 6
483 2 return [ [] ];
484 }
485  
486 4 my @paths;
487 4 _collect_leaf_paths($value, [], \@paths);
488 4 return \@paths;
489 }
490  
491 43 ▶sub _validate_path_array {
Subroutine _validate_path_array: called CC=8 SCAR=20.9
492 43 my ($path, $caller) = @_;
493  
494 43 ▶ $caller //= 'getpath';
Condition50%1/2
$caller //= "getpath"
l!l
430
MC/DC0%0/1
$caller //= "getpath"
$caller
Truth table50%1/2
A
A= $caller
Aresult
11
00
495  
496 43 ▶ die "$caller(): path must be an array" if ref($path) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $path ne "ARRAY" 0 43 43
497  
498 43 for my $segment (@$path) {
499 64 ▶ my $is_boolean = ref($segment) && JSON::PP::is_bool($segment);
Condition100%3/3
ref $segment && JSON::PP::is_bool($segment)
!ll&&!rl&&r
5428
MC/DC100%2/2
ref $segment && JSON::PP::is_bool($segment)
ref $segment JSON::PP::is_bool($segment)
Truth table100%3/3
A && B
A= ref $segmentB= JSON::PP::is_bool($segment)
ABresult
0X0
100
111
500  
501 64 ▶ die "$caller(): path elements must be defined" if !defined $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $segment 2 62 64
502 62 ▶ die "$caller(): path elements must be scalars" if ref($segment) && !$is_boolean;
Branch: 1 branch
BranchTrueFalseTotal
if ref $segment and not $is_boolean 2 60 62
Condition100%3/3
ref $segment and not $is_boolean
!ll&&!rl&&r
5282
MC/DC100%2/2
ref $segment and not $is_boolean
ref $segment not $is_boolean
Truth table100%3/3
A and not(B)
A= ref $segmentB= not $is_boolean
ABresult
0X0
100
111
503 }
504  
505 39 return [ @$path ];
506 }
507  
508 14 ▶sub _apply_getpath {
Subroutine _apply_getpath: called CC=19 SCAR=31.1
509 14 my ($self, $value, $expr) = @_;
510  
511 14 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 14 14
512  
513 14 ▶ $expr //= '';
Condition50%1/2
$expr //= ""
l!l
140
MC/DC0%0/1
$expr //= ""
$expr
Truth table50%1/2
A
A= $expr
Aresult
11
00
514 14 $expr =~ s/^\s+|\s+$//g;
515 14 ▶ return undef if $expr eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "" 0 14 14
516  
517 14 my @paths;
518  
519 14 my $decoded = eval { _decode_json($expr) };
520 14 ▶ if (!$@ && defined $decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and defined $decoded) 13 1 14
Condition66%2/3
not $@ and defined $decoded
!ll&&!rl&&r
1013
MC/DC50%1/2
not $@ and defined $decoded
not $@ defined $decoded
Truth table66%2/3
not(A) and B
A= not $@B= defined $decoded
ABresult
0X0
100
111
521 13 ▶ if (ref $decoded eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq 'ARRAY') { } 12 1 13
522 12 ▶ if (@$decoded && ref $decoded->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$decoded and ref $decoded->[0] eq 'ARRAY') { } 1 11 12
Condition100%3/3
@$decoded and ref $decoded->[0] eq "ARRAY"
!ll&&!rl&&r
1101
MC/DC100%2/2
@$decoded and ref $decoded->[0] eq "ARRAY"
@$decoded ref $decoded->[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @$decodedB= ref $decoded->[0] eq "ARRAY"
ABresult
0X0
100
111
523 1 for my $path (@$decoded) {
524 2 push @paths, _validate_path_array($path, 'getpath');
525 }
526 }
527 else {
528 11 push @paths, _validate_path_array($decoded, 'getpath');
529 }
530 }
531 else {
532 1 die 'getpath(): path must be an array';
533 }
534 }
535  
536 13 ▶ if (!@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 1 12 13
537 1 my @outputs = $self->run_query(_encode_json($value), $expr);
538 1 for my $output (@outputs) {
539 7 ▶ next unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 7 7
540  
541 7 ▶ if (ref $output eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq 'ARRAY') { } 7 0 7
542 7 ▶ if (@$output && ref $output->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$output and ref $output->[0] eq 'ARRAY') { } 0 7 7
Condition33%1/3
@$output and ref $output->[0] eq "ARRAY"
!ll&&!rl&&r
070
MC/DC0%0/2
@$output and ref $output->[0] eq "ARRAY"
@$output ref $output->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @$outputB= ref $output->[0] eq "ARRAY"
ABresult
0X0
100
111
543 0 for my $path (@$output) {
544 0 push @paths, _validate_path_array($path, 'getpath');
545 }
546 }
547 else {
548 7 push @paths, _validate_path_array($output, 'getpath');
549 }
550 }
551 else {
552 0 die 'getpath(): path must be an array';
553 }
554 }
555 }
556  
557 13 ▶ return undef unless @paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @paths 0 13 13
558  
559 13 my @values = map { _traverse_path_array($value, $_) } @paths;
560 13 ▶ return @values == 1 ? $values[0] : \@values;
Branch: 1 branch
BranchTrueFalseTotal
@values == 1 ? : 11 2 13
561 }
562  
563 10 ▶sub _apply_setpath {
Subroutine _apply_setpath: called CC=6 SCAR=18.4
564 10 my ($self, $value, $paths_expr, $value_expr) = @_;
565  
566 10 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
567  
568 10 ▶ $paths_expr //= '';
Condition50%1/2
$paths_expr //= ""
l!l
100
MC/DC0%0/1
$paths_expr //= ""
$paths_expr
Truth table50%1/2
A
A= $paths_expr
Aresult
11
00
569 10 $paths_expr =~ s/^\s+|\s+$//g;
570 10 ▶ return $value if $paths_expr eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $paths_expr eq "" 0 10 10
571  
572 10 my @paths = _resolve_paths_from_expr($self, $value, $paths_expr);
573 7 ▶ return $value unless @paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @paths 0 7 7
574  
575 7 my $replacement = _evaluate_setpath_value($self, $value, $value_expr);
576 7 my $result = $value;
577  
578 7 for my $path (@paths) {
579 11 $result = _set_value_at_path($result, [@$path], $replacement);
580 }
581  
582 7 return $result;
583 }
584  
585 10 ▶sub _resolve_paths_from_expr {
Subroutine _resolve_paths_from_expr: called CC=18 SCAR=35.8
586 10 my ($self, $value, $expr) = @_;
587  
588 10 ▶ return () unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 10 10
589  
590 10 my $clean = $expr;
591 10 $clean =~ s/^\s+|\s+$//g;
592 10 ▶ return () if $clean eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $clean eq "" 0 10 10
593  
594 10 my @paths;
595  
596 10 my $decoded = eval { _decode_json($clean) };
597 10 ▶ if (!$@ && defined $decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and defined $decoded) 9 1 10
Condition66%2/3
not $@ and defined $decoded
!ll&&!rl&&r
109
MC/DC50%1/2
not $@ and defined $decoded
not $@ defined $decoded
Truth table66%2/3
not(A) and B
A= not $@B= defined $decoded
ABresult
0X0
100
111
598 9 ▶ if (ref $decoded eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq 'ARRAY') { } 8 1 9
599 8 ▶ if (@$decoded && ref $decoded->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$decoded and ref $decoded->[0] eq 'ARRAY') { } 0 8 8
Condition33%1/3
@$decoded and ref $decoded->[0] eq "ARRAY"
!ll&&!rl&&r
080
MC/DC0%0/2
@$decoded and ref $decoded->[0] eq "ARRAY"
@$decoded ref $decoded->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @$decodedB= ref $decoded->[0] eq "ARRAY"
ABresult
0X0
100
111
600 0 push @paths, map { _validate_path_array($_, 'setpath') } @$decoded;
601 }
602 else {
603 8 push @paths, _validate_path_array($decoded, 'setpath');
604 }
605 }
606 else {
607 1 die 'setpath(): path must be an array';
608 }
609 }
610  
611 7 ▶ if (!@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 1 6 7
612 1 my @outputs = $self->run_query(_encode_json($value), $clean);
613 1 for my $output (@outputs) {
614 5 ▶ next unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 5 5
615  
616 5 ▶ if (ref $output eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $output eq 'ARRAY') { } 5 0 5
elsif (not ref $output or JSON::PP::is_bool($output)) { } 0 0 0
617 5 ▶ if (@$output && ref $output->[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (@$output and ref $output->[0] eq 'ARRAY') { } 0 5 5
elsif (not @$output && ref $output->[0]) { } 5 0 5
Condition33%1/3
@$output and ref $output->[0] eq "ARRAY"
!ll&&!rl&&r
050
MC/DC0%0/2
@$output and ref $output->[0] eq "ARRAY"
@$output ref $output->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @$outputB= ref $output->[0] eq "ARRAY"
ABresult
0X0
100
111
618 0 push @paths, map { _validate_path_array($_, 'setpath') } @$output;
619 }
620 ▶ elsif (!@$output || !ref $output->[0]) {
Condition33%1/3
@$output && ref $output->[0]
!ll&&!rl&&r
050
MC/DC0%0/2
@$output && ref $output->[0]
@$output ref $output->[0]
Truth table33%1/3
A && B
A= @$outputB= ref $output->[0]
ABresult
0X0
100
111
621 5 push @paths, _validate_path_array($output, 'setpath');
622 }
623 }
624 ▶ elsif (!ref $output || JSON::PP::is_bool($output)) {
Condition0%0/3
not ref $output or JSON::PP::is_bool($output)
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $output or JSON::PP::is_bool($output)
not ref $output JSON::PP::is_bool($output)
Truth table0%0/3
not(A) or B
A= not ref $outputB= JSON::PP::is_bool($output)
ABresult
1X1
011
000
625 0 die 'setpath(): path must be an array';
626 }
627 }
628 }
629  
630 7 return @paths;
631 }
632  
633 7 ▶sub _evaluate_setpath_value {
Subroutine _evaluate_setpath_value: called CC=8 SCAR=24.0
634 7 my ($self, $context, $expr) = @_;
635  
636 7 ▶ return undef unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 7 7
637  
638 7 my $clean = $expr;
639 7 $clean =~ s/^\s+|\s+$//g;
640 7 ▶ return undef if $clean eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $clean eq "" 0 7 7
641  
642 7 my $decoded = eval { _decode_json($clean) };
643 7 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 6 1 7
644 6 return $decoded;
645 }
646  
647 1 ▶ if ($clean =~ /^'(.*)'$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean =~ /^'(.*)'$/) 0 1 1
648 0 my $text = $1;
649 0 $text =~ s/\\'/'/g;
650 0 return $text;
651 }
652  
653 1 ▶ if ($clean =~ /^\.(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean =~ /^\.(.+)$/) 1 0 1
654 1 my $path = $1;
655 1 my @values = _traverse($context, $path);
656 1 ▶ return @values ? $values[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@values ? : 1 0 1
657 }
658  
659 0 my @outputs = $self->run_query(_encode_json($context), $clean);
660 0 ▶ return @outputs ? $outputs[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@outputs ? : 0 0 0
661 }
662  
663 26 ▶sub _set_value_at_path {
Subroutine _set_value_at_path: called CC=10 SCAR=23.5
664 26 my ($current, $path, $replacement) = @_;
665  
666 26 ▶ return _deep_clone($replacement) unless @$path;
Branch: 1 branch
BranchTrueFalseTotal
unless @$path 0 26 26
667  
668 26 my ($segment, @rest) = @$path;
669  
670 26 ▶ if (ref $current eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current eq "HASH") 23 3 26
671 23 my $key = _coerce_hash_key($segment);
672 23 ▶ return $current unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 23 23
673  
674 23 my %copy = %$current;
675 23 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) { } 12 11 23
676 12 ▶ my $next_value = exists $copy{$key} ? $copy{$key} : _guess_container_for_segment($rest[0]);
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{$key} ? : 12 0 12
677 12 $copy{$key} = _set_value_at_path($next_value, \@rest, $replacement);
678 }
679 else {
680 11 $copy{$key} = _deep_clone($replacement);
681 }
682  
683 23 return \%copy;
684 }
685  
686 3 ▶ if (ref $current eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current eq "ARRAY") 1 2 3
687 1 my $index = _normalize_array_index_for_set($segment, scalar @$current);
688 1 ▶ return $current unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 1 1
689  
690 1 my @copy = @$current;
691 1 _ensure_array_length(\@copy, $index);
692  
693 1 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) { } 1 0 1
694 1 ▶ my $next_value = defined $copy[$index] ? $copy[$index] : _guess_container_for_segment($rest[0]);
Branch: 1 branch
BranchTrueFalseTotal
defined $copy[$index] ? : 0 1 1
695 1 $copy[$index] = _set_value_at_path($next_value, \@rest, $replacement);
696 }
697 else {
698 0 $copy[$index] = _deep_clone($replacement);
699 }
700  
701 1 return \@copy;
702 }
703  
704 2 my $container = _guess_container_for_segment($segment);
705 2 return _set_value_at_path($container, $path, $replacement);
706 }
707  
708 45 ▶sub _coerce_hash_key {
Subroutine _coerce_hash_key: called CC=5 SCAR=16.2
709 45 my ($segment) = @_;
710  
711 45 ▶ return undef if !defined $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $segment 0 45 45
712  
713 45 ▶ if (JSON::PP::is_bool($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($segment)) 4 41 45
714 4 ▶ return $segment ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$segment ? : 2 2 4
715 }
716  
717 41 ▶ return undef if ref $segment;
Branch: 1 branch
BranchTrueFalseTotal
if ref $segment 0 41 41
718  
719 41 return "$segment";
720 }
721  
722 3 ▶sub _guess_container_for_segment {
Subroutine _guess_container_for_segment: called CC=2 SCAR=7.1
723 3 my ($segment) = @_;
724  
725 3 ▶ return [] if _is_numeric_segment($segment);
Branch: 1 branch
BranchTrueFalseTotal
if _is_numeric_segment($segment) 0 3 3
726 3 return {};
727 }
728  
729 7 ▶sub _is_numeric_segment {
Subroutine _is_numeric_segment: called CC=5 SCAR=16.6
730 7 my ($segment) = @_;
731  
732 7 ▶ return 0 if !defined $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $segment 0 7 7
733  
734 7 ▶ if (JSON::PP::is_bool($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($segment)) 4 3 7
735 4 return 1;
736 }
737  
738 3 ▶ return 0 if ref $segment;
Branch: 1 branch
BranchTrueFalseTotal
if ref $segment 0 3 3
739  
740 3 ▶ return ($segment =~ /^-?\d+$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$segment =~ /^-?\d+$/ ? : 0 3 3
741 }
742  
743 1 ▶sub _normalize_array_index_for_set {
Subroutine _normalize_array_index_for_set: called CC=8 SCAR=24.3
744 1 my ($segment, $length) = @_;
745  
746 1 ▶ return undef if !defined $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $segment 0 1 1
747  
748 1 ▶ if (JSON::PP::is_bool($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($segment)) 0 1 1
749 0 ▶ $segment = $segment ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$segment ? : 0 0 0
750 }
751  
752 1 ▶ return undef if ref $segment;
Branch: 1 branch
BranchTrueFalseTotal
if ref $segment 0 1 1
753 1 ▶ return undef if $segment !~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment =~ /^-?\d+$/ 0 1 1
754  
755 1 my $index = int($segment);
756 1 ▶ $index += $length if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 1 1
757  
758 1 ▶ return undef if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 1 1
759  
760 1 return $index;
761 }
762  
763 6 ▶sub _normalize_array_index_for_get {
Subroutine _normalize_array_index_for_get: called CC=8 SCAR=21.2
764 6 my ($segment, $length) = @_;
765  
766 6 ▶ return undef if !defined $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $segment 0 6 6
767  
768 6 ▶ if (JSON::PP::is_bool($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::PP::is_bool($segment)) 2 4 6
769 2 ▶ $segment = $segment ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$segment ? : 1 1 2
770 }
771  
772 6 ▶ return undef if ref $segment;
Branch: 1 branch
BranchTrueFalseTotal
if ref $segment 0 6 6
773 6 ▶ return undef if $segment !~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment =~ /^-?\d+$/ 0 6 6
774  
775 6 my $index = int($segment);
776 6 ▶ $index += $length if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 1 5 6
777  
778 6 ▶ return undef if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 6 6
779  
780 6 return $index;
781 }
782  
783 1 ▶sub _ensure_array_length {
Subroutine _ensure_array_length: called CC=3 SCAR=11.0
784 1 my ($array_ref, $index) = @_;
785  
786 1 ▶ return unless ref $array_ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array_ref eq "ARRAY" 0 1 1
787  
788 1 ▶ while (@$array_ref <= $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$array_ref <= $index) 1 1 2
789 1 push @$array_ref, undef;
790 }
791 }
792  
793 8 ▶sub _collect_paths {
Subroutine _collect_paths: called CC=9 SCAR=22.5
794 8 my ($value, $current_path, $paths) = @_;
795  
796 8 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 6 2 8
797 6 for my $key (sort keys %$value) {
798 11 my $child = $value->{$key};
799 11 my @next = (@$current_path, $key);
800 11 push @$paths, [@next];
801  
802 11 ▶ if (ref $child eq 'HASH' || ref $child eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child eq "HASH" or ref $child eq "ARRAY") 5 6 11
Condition100%3/3
ref $child eq "HASH" or ref $child eq "ARRAY"
l!l&&r!l&&!r
326
MC/DC100%2/2
ref $child eq "HASH" or ref $child eq "ARRAY"
ref $child eq "HASH" ref $child eq "ARRAY"
Truth table100%3/3
A or B
A= ref $child eq "HASH"B= ref $child eq "ARRAY"
ABresult
1X1
011
000
803 5 _collect_paths($child, \@next, $paths);
804 }
805 }
806 6 return;
807 }
808  
809 2 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 2 0 2
810 2 for my $index (0 .. $#$value) {
811 4 my $child = $value->[$index];
812 4 my @next = (@$current_path, $index);
813 4 push @$paths, [@next];
814  
815 4 ▶ if (ref $child eq 'HASH' || ref $child eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child eq "HASH" or ref $child eq "ARRAY") 0 4 4
Condition33%1/3
ref $child eq "HASH" or ref $child eq "ARRAY"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $child eq "HASH" or ref $child eq "ARRAY"
ref $child eq "HASH" ref $child eq "ARRAY"
Truth table33%1/3
A or B
A= ref $child eq "HASH"B= ref $child eq "ARRAY"
ABresult
1X1
011
000
816 0 _collect_paths($child, \@next, $paths);
817 }
818 }
819 2 return;
820 }
821  
822 0 push @$paths, [@$current_path];
823 }
824  
825 3 ▶sub _collect_scalar_paths {
Subroutine _collect_scalar_paths: called CC=11 SCAR=24.8
826 3 my ($value, $current_path, $paths) = @_;
827  
828 3 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 2 1 3
829 2 for my $key (sort keys %$value) {
830 3 my $child = $value->{$key};
831 3 my @next = (@$current_path, $key);
832  
833 3 ▶ if (_is_scalar_value($child)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_scalar_value($child)) { } 2 1 3
elsif (ref $child eq 'HASH' or ref $child eq 'ARRAY') { } 1 0 1
834 2 push @$paths, [@next];
835 }
836 ▶ elsif (ref $child eq 'HASH' || ref $child eq 'ARRAY') {
Condition33%1/3
ref $child eq "HASH" or ref $child eq "ARRAY"
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $child eq "HASH" or ref $child eq "ARRAY"
ref $child eq "HASH" ref $child eq "ARRAY"
Truth table33%1/3
A or B
A= ref $child eq "HASH"B= ref $child eq "ARRAY"
ABresult
1X1
011
000
837 1 _collect_scalar_paths($child, \@next, $paths);
838 }
839 }
840 2 return;
841 }
842  
843 1 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 0 1
844 1 for my $index (0 .. $#$value) {
845 2 my $child = $value->[$index];
846 2 my @next = (@$current_path, $index);
847  
848 2 ▶ if (_is_scalar_value($child)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_scalar_value($child)) { } 1 1 2
elsif (ref $child eq 'HASH' or ref $child eq 'ARRAY') { } 1 0 1
849 1 push @$paths, [@next];
850 }
851 ▶ elsif (ref $child eq 'HASH' || ref $child eq 'ARRAY') {
Condition33%1/3
ref $child eq "HASH" or ref $child eq "ARRAY"
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $child eq "HASH" or ref $child eq "ARRAY"
ref $child eq "HASH" ref $child eq "ARRAY"
Truth table33%1/3
A or B
A= ref $child eq "HASH"B= ref $child eq "ARRAY"
ABresult
1X1
011
000
852 1 _collect_scalar_paths($child, \@next, $paths);
853 }
854 }
855 1 return;
856 }
857 }
858  
859 20 ▶sub _traverse_path_array {
Subroutine _traverse_path_array: called CC=12 SCAR=25.4
860 20 my ($value, $path) = @_;
861  
862 20 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 20 20
863 20 ▶ return $value unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 20 20
864 20 ▶ return $value if ref($path) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $path ne "ARRAY" 0 20 20
865  
866 20 my $cursor = $value;
867 20 for my $segment (@$path) {
868 25 ▶ return undef unless defined $cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cursor 0 25 25
869  
870 25 ▶ if (ref $cursor eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cursor eq "HASH") 18 7 25
871 18 my $key = _coerce_hash_key($segment);
872 18 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 18 18
873 18 ▶ return undef unless exists $cursor->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cursor->{$key} 1 17 18
874 17 $cursor = $cursor->{$key};
875 17 next;
876 }
877  
878 7 ▶ if (ref $cursor eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cursor eq "ARRAY") 6 1 7
879 6 my $index = _normalize_array_index_for_get($segment, scalar @$cursor);
880 6 ▶ return undef unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 6 6
881  
882 6 ▶ return undef if $index > $#$cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $cursor->$#* 0 6 6
883  
884 6 $cursor = $cursor->[$index];
885 6 next;
886 }
887  
888 1 return undef;
889 }
890  
891 18 return $cursor;
892 }
893  
894 9 ▶sub _collect_leaf_paths {
Subroutine _collect_leaf_paths: called CC=7 SCAR=19.5
895 9 my ($value, $current_path, $paths) = @_;
896  
897 9 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 5 4 9
898 5 for my $key (sort keys %$value) {
899 7 my $child = $value->{$key};
900 7 my @next = (@$current_path, $key);
901  
902 7 ▶ if (_is_leaf_value($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_leaf_value($child)) { } 3 4 7
903 3 push @$paths, [@next];
904 }
905 else {
906 4 _collect_leaf_paths($child, \@next, $paths);
907 }
908 }
909 5 return;
910 }
911  
912 4 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 4 0 4
913 4 for my $index (0 .. $#$value) {
914 6 my $child = $value->[$index];
915 6 my @next = (@$current_path, $index);
916  
917 6 ▶ if (_is_leaf_value($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_leaf_value($child)) { } 5 1 6
918 5 push @$paths, [@next];
919 }
920 else {
921 1 _collect_leaf_paths($child, \@next, $paths);
922 }
923 }
924 4 return;
925 }
926  
927 0 push @$paths, [@$current_path];
928 }
929  
930 27 ▶sub _is_leaf_value {
Subroutine _is_leaf_value: called CC=5 SCAR=16.2
931 27 my ($value) = @_;
932  
933 27 ▶ return 1 unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 10 17 27
934 17 ▶ return 1 if JSON::PP::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($value) 5 12 17
935 12 ▶ return 0 if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 5 7 12
936 7 ▶ return 0 if ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 7 0 7
937 0 return 1;
938 }
939  
940 8 ▶sub _is_scalar_value {
Subroutine _is_scalar_value: called CC=1 SCAR=0.0
941 8 return _is_leaf_value(@_);
942 }
943  
944 182 1;
« blib/lib/JQ/Lite/Expression.pm ↑ Summary bin/jq-lite »