Validator/JSONPointer.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 72.7% 32 / 44 Conditioncond 82.7% 24 / 29 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 12 totaltotal 81.7% 175 / 214 CC 34 SCAR 25.5
CC
34
Cov
87%
xget
25.5
keys
6.2
get
4.1
CRAP 12.9
↑ Summary Validator/Constraints/Draft6.pm »
1 package JSONSchema::Validator::JSONPointer;
2  
3 # ABSTRACT: JSON Pointer with URI resolving
4  
5 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use overload
13 559 ▶ 'bool' => sub { $_[0]->value },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
14 17 fallback => 1;
15  
16 17 our @ISA = 'Exporter';
17 17 our @EXPORT_OK = qw(json_pointer);
18  
19 5904 ▶sub json_pointer {
Subroutine json_pointer: called CC=1 SCAR=0.0
Pod undocumented
20 5904 return __PACKAGE__;
21 }
22  
23 5762 ▶sub append {
Subroutine append: called CC=2 SCAR=6.9
Pod undocumented
24 5762 my ($class, $path, @values) = @_;
25 5762 my $suffix = join('/', map { $class->escape($_) } @values);
26 5762 ▶ return $path =~ m!/$!
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[/$] ? : 1042 4720 5762
27 ? $path . $suffix
28 : $path . '/' . $suffix;
29 }
30  
31 67 ▶sub join {
Subroutine join: called CC=1 SCAR=0.0
Pod undocumented
32 67 my ($class, @parts) = @_;
33 67 return '/' . join('/', map { $class->escape($_) } @parts);
34 }
35  
36 5829 ▶sub escape {
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
37 5829 my ($class, $value) = @_;
38 5829 $value =~ s!~!~0!g;
39 5829 $value =~ s!/!~1!g;
40 5829 return $value;
41 }
42  
43 379 ▶sub unescape {
Subroutine unescape: called CC=1 SCAR=0.0
Pod undocumented
44 379 my ($class, $value) = @_;
45 379 $value =~ s!~1!/!g;
46 379 $value =~ s!~0!~!g;
47 379 return $value;
48 }
49  
50 1114 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
51 1114 my ($class, %params) = @_;
52  
53 1114 my ($scope, $value, $validator) = @params{qw/scope value validator/};
54  
55 1114 ▶ croak 'JSONPointer: scope is required' unless defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope 0 1114 1114
56 1114 ▶ croak 'JSONPointer: validator is required' unless $validator;
Branch: 1 branch
BranchTrueFalseTotal
unless $validator 0 1114 1114
57  
58 1114 weaken($validator);
59  
60 1114 my $self = {
61 scope => $scope,
62 value => $value,
63 validator => $validator
64 };
65  
66 1114 bless $self, $class;
67  
68 1114 return $self;
69 }
70  
71 3107 ▶sub validator { shift->{validator} }
Subroutine validator: called CC=1 SCAR=0.0
Pod undocumented
72 1156 ▶sub scope { shift->{scope} }
Subroutine scope: called CC=1 SCAR=0.0
Pod undocumented
73 2076 ▶sub value { shift->{value} }
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
74  
75 928 ▶sub xget {
Subroutine xget: called CC=23 SCAR=32.4
Pod undocumented
76 928 my ($self, @parts) = @_;
77  
78 928 my $current_scope = $self->scope;
79 928 my $current_value = $self->value;
80  
81 928 ▶ while (ref $current_value eq 'HASH' && $current_value->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $current_value->{"\$ref"}) 0 928 928
Condition66%2/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
!ll&&!rl&&r
678610
MC/DC0%0/2
ref $current_value eq "HASH" and $current_value->{"\$ref"}
ref $current_value eq "HASH" $current_value->{"\$ref"}
Truth table66%2/3
A and B
A= ref $current_value eq "HASH"B= $current_value->{"\$ref"}
ABresult
0X0
100
111
82 0 my $ref = URI->new($current_value->{'$ref'});
83 0 ▶ $ref = $ref->abs($current_scope) if $current_scope;
Branch: 1 branch
BranchTrueFalseTotal
if $current_scope 0 0 0
84 0 ($current_scope, $current_value) = $self->validator->resolver->resolve($ref);
85 }
86  
87 928 ▶ if (ref $current_value eq 'HASH' && $self->validator->using_id_with_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $self->validator->using_id_with_ref) 114 814 928
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
!ll&&!rl&&r
67747114
MC/DC100%2/2
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
ref $current_value eq "HASH" $self->validator->using_id_with_ref
Truth table100%3/3
A and B
A= ref $current_value eq "HASH"B= $self->validator->using_id_with_ref
ABresult
0X0
100
111
88 114 my $id = $current_value->{$self->validator->ID_FIELD};
89 114 ▶ if ($id && !ref $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and not ref $id) 89 25 114
Condition66%2/3
$id and not ref $id
!ll&&!rl&&r
25089
MC/DC50%1/2
$id and not ref $id
$id not ref $id
Truth table66%2/3
A and not(B)
A= $idB= not ref $id
ABresult
0X0
100
111
90 89 ▶ $current_scope = $current_scope
Branch: 1 branch
BranchTrueFalseTotal
$current_scope ? : 89 0 89
91 ? URI->new($id)->abs($current_scope)->as_string
92 : $id;
93 }
94 }
95  
96 928 for my $part (@parts) {
97 1184 ▶ if (ref $current_value eq 'HASH' && exists $current_value->{$part}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $current_value eq 'HASH' and exists $current_value->{$part}) { } 914 270 1184
elsif (ref $current_value eq 'ARRAY' and $part =~ /^\d+$/ and scalar @$current_value > $part) { } 67 203 270
Condition100%3/3
ref $current_value eq "HASH" and exists $current_value->{$part}
!ll&&!rl&&r
67203914
MC/DC100%2/2
ref $current_value eq "HASH" and exists $current_value->{$part}
ref $current_value eq "HASH" exists $current_value->{$part}
Truth table100%3/3
A and B
A= ref $current_value eq "HASH"B= exists $current_value->{$part}
ABresult
0X0
100
111
98 914 $current_value = $current_value->{$part};
99 ▶ } elsif (ref $current_value eq 'ARRAY' && $part =~ m/^\d+$/ && scalar(@$current_value) > $part) {
Condition66%4/6
ref $current_value eq "ARRAY" and $part =~ /^\d+$/ and scalar @$current_value > $part
!ll&&!rl&&r
203067
ref $current_value eq "ARRAY" and $part =~ /^\d+$/
!ll&&!rl&&r
203067
MC/DC33%1/3
ref $current_value eq "ARRAY" and $part =~ /^\d+$/ and scalar @$current_value > $part
ref $current_value eq "ARRAY" $part =~ /^\d+$/ scalar @$current_value > $part
Truth table50%2/4
A and B and C
A= ref $current_value eq "ARRAY"B= $part =~ /^\d+$/C= scalar @$current_value > $part
ABCresult
0XX0
10X0
1100
1111
100 67 $current_value = $current_value->[$part];
101 } else {
102 203 $current_value = undef;
103 203 last;
104 }
105  
106 981 ▶ while (ref $current_value eq 'HASH' && $current_value->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $current_value->{"\$ref"}) 38 981 1019
Condition100%3/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
!ll&&!rl&&r
4593638
MC/DC100%2/2
ref $current_value eq "HASH" and $current_value->{"\$ref"}
ref $current_value eq "HASH" $current_value->{"\$ref"}
Truth table100%3/3
A and B
A= ref $current_value eq "HASH"B= $current_value->{"\$ref"}
ABresult
0X0
100
111
107 38 my $ref = URI->new($current_value->{'$ref'});
108 38 ▶ $ref = $ref->abs($current_scope) if $current_scope;
Branch: 1 branch
BranchTrueFalseTotal
if $current_scope 2 36 38
109 38 ($current_scope, $current_value) = $self->validator->resolver->resolve($ref);
110 }
111  
112 981 ▶ if (ref $current_value eq 'HASH' && $self->validator->using_id_with_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $self->validator->using_id_with_ref) 230 751 981
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
!ll&&!rl&&r
45706230
MC/DC100%2/2
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
ref $current_value eq "HASH" $self->validator->using_id_with_ref
Truth table100%3/3
A and B
A= ref $current_value eq "HASH"B= $self->validator->using_id_with_ref
ABresult
0X0
100
111
113 230 my $id = $current_value->{$self->validator->ID_FIELD};
114 230 ▶ if ($id && !ref $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and not ref $id) 2 228 230
Condition66%2/3
$id and not ref $id
!ll&&!rl&&r
22802
MC/DC50%1/2
$id and not ref $id
$id not ref $id
Truth table66%2/3
A and not(B)
A= $idB= not ref $id
ABresult
0X0
100
111
115 2 ▶ $current_scope = $current_scope
Branch: 1 branch
BranchTrueFalseTotal
$current_scope ? : 2 0 2
116 ? URI->new($id)->abs($current_scope)->as_string
117 : $id;
118 }
119 }
120 }
121  
122 928 return __PACKAGE__->new(
123 value => $current_value,
124 scope => $current_scope,
125 validator => $self->validator
126 )
127 }
128  
129 209 ▶sub get {
Subroutine get: called CC=4 SCAR=14.0
Pod undocumented
130 # pointer is string which is already urldecoded and utf8-decoded
131 209 my ($self, $pointer) = @_;
132 209 ▶ return $self unless $pointer;
Branch: 1 branch
BranchTrueFalseTotal
unless $pointer 0 209 209
133  
134 209 ▶ croak "Invalid JSON Pointer $pointer" unless $pointer =~ s!^/!!;
Branch: 1 branch
BranchTrueFalseTotal
unless $pointer =~ s[^/][] 0 209 209
135  
136 my @parts = length $pointer
137 209 ▶ ? map { $self->unescape($_) } split(/\//, $pointer, -1)
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 207 2 209
138 : ('');
139  
140 209 return $self->xget(@parts);
141 }
142  
143 61 ▶sub keys {
Subroutine keys: called CC=6 SCAR=18.3
Pod undocumented
144 61 my ($self, %params) = @_;
145 61 ▶ my $raw = $params{raw} // 0;
Condition100%2/2
$params{"raw"} // 0
l!l
3427
MC/DC100%1/1
$params{"raw"} // 0
$params{"raw"}
Truth table100%2/2
A
A= $params{"raw"}
Aresult
11
00
146  
147 61 ▶ if (ref $self->value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->value eq "HASH") 34 27 61
148 34 ▶ return map { $raw ? $_ : $self->join($_) } keys %{$self->value};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 41 0 41
149 }
150  
151 27 ▶ if (ref $self->value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->value eq "ARRAY") 27 0 27
152 27 ▶ return map { $raw ? $_ : $self->join($_) } 0 .. $#{$self->value};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 0 67 67
153 }
154  
155 0 return;
156 }
157  
158 17 1;
159  
160 __END__
↑ Summary Validator/Constraints/Draft6.pm »