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 SCAR 35.9
CC
34
Cov
87%
xget
25.5
keys
6.2
get
4.1
CRAP 36.4
« Validator/Constraints/Draft6.pm ↑ Summary Validator/URIResolver.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 983 ▶ 'bool' => sub { $_[0]->value },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 17 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 17 our @ISA = 'Exporter';
17 17 our @EXPORT_OK = qw(json_pointer);
18  
19 sub json_pointer {
20 5849 ▶ return __PACKAGE__;
Subroutine json_pointer: called CC=1 SCAR=0.0
Pod undocumented
21 }
22  
23 sub append {
24 5707 ▶ my ($class, $path, @values) = @_;
Subroutine append: called CC=2 SCAR=6.9
Pod undocumented
25 5707 my $suffix = join('/', map { $class->escape($_) } @values);
26 5707 ▶ return $path =~ m!/$!
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[/$] ? : 1005 4702 5707
27 ? $path . $suffix
28 : $path . '/' . $suffix;
29 }
30  
31 sub join {
32 61 ▶ my ($class, @parts) = @_;
Subroutine join: called CC=1 SCAR=0.0
Pod undocumented
33 61 return '/' . join('/', map { $class->escape($_) } @parts);
34 }
35  
36 sub escape {
37 5768 ▶ my ($class, $value) = @_;
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
38 5768 $value =~ s!~!~0!g;
39 5768 $value =~ s!/!~1!g;
40 5768 return $value;
41 }
42  
43 sub unescape {
44 373 ▶ my ($class, $value) = @_;
Subroutine unescape: called CC=1 SCAR=0.0
Pod undocumented
45 373 $value =~ s!~1!/!g;
46 373 $value =~ s!~0!~!g;
47 373 return $value;
48 }
49  
50 sub new {
51 1013 ▶ my ($class, %params) = @_;
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
52  
53 1013 my ($scope, $value, $validator) = @params{qw/scope value validator/};
54  
55 1013 ▶ croak 'JSONPointer: scope is required' unless defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope 0 1013 1013
56 1013 ▶ croak 'JSONPointer: validator is required' unless $validator;
Branch: 1 branch
BranchTrueFalseTotal
unless $validator 0 1013 1013
57  
58 1013 weaken($validator);
59  
60 1013 my $self = {
61 scope => $scope,
62 value => $value,
63 validator => $validator
64 };
65  
66 1013 bless $self, $class;
67  
68 1013 return $self;
69 }
70  
71 2845 ▶sub validator { shift->{validator} }
Subroutine validator: called CC=1 SCAR=0.0
Pod undocumented
72 1046 ▶sub scope { shift->{scope} }
Subroutine scope: called CC=1 SCAR=0.0
Pod undocumented
73 2348 ▶sub value { shift->{value} }
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
74  
75 sub xget {
76 829 ▶ my ($self, @parts) = @_;
Subroutine xget: called CC=23 SCAR=32.4
Pod undocumented
77  
78 829 my $current_scope = $self->scope;
79 829 my $current_value = $self->value;
80  
81 829 ▶ while (ref $current_value eq 'HASH' && $current_value->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $current_value->{"\$ref"}) 0 829 829
Condition66%2/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
!ll&&!rl&&r
617680
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 829 ▶ 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 715 829
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
!ll&&!rl&&r
61654114
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 829 for my $part (@parts) {
97 1082 ▶ 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}) { } 852 230 1082
elsif (ref $current_value eq 'ARRAY' and $part =~ /^\d+$/ and scalar @$current_value > $part) { } 61 169 230
Condition100%3/3
ref $current_value eq "HASH" and exists $current_value->{$part}
!ll&&!rl&&r
61169852
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 852 $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
169061
ref $current_value eq "ARRAY" and $part =~ /^\d+$/
!ll&&!rl&&r
169061
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 61 $current_value = $current_value->[$part];
101 } else {
102 169 $current_value = undef;
103 169 last;
104 }
105  
106 913 ▶ while (ref $current_value eq 'HASH' && $current_value->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_value eq "HASH" and $current_value->{"\$ref"}) 34 913 947
Condition100%3/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
!ll&&!rl&&r
4387034
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 34 my $ref = URI->new($current_value->{'$ref'});
108 34 ▶ $ref = $ref->abs($current_scope) if $current_scope;
Branch: 1 branch
BranchTrueFalseTotal
if $current_scope 2 32 34
109 34 ($current_scope, $current_value) = $self->validator->resolver->resolve($ref);
110 }
111  
112 913 ▶ 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 683 913
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
!ll&&!rl&&r
43640230
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 829 return __PACKAGE__->new(
123 value => $current_value,
124 scope => $current_scope,
125 validator => $self->validator
126 )
127 }
128  
129 sub get {
130 # pointer is string which is already urldecoded and utf8-decoded
131 203 ▶ my ($self, $pointer) = @_;
Subroutine get: called CC=4 SCAR=14.0
Pod undocumented
132 203 ▶ return $self unless $pointer;
Branch: 1 branch
BranchTrueFalseTotal
unless $pointer 0 203 203
133  
134 203 ▶ croak "Invalid JSON Pointer $pointer" unless $pointer =~ s!^/!!;
Branch: 1 branch
BranchTrueFalseTotal
unless $pointer =~ s[^/][] 0 203 203
135  
136 my @parts = length $pointer
137 203 ▶ ? map { $self->unescape($_) } split(/\//, $pointer, -1)
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 201 2 203
138 : ('');
139  
140 203 return $self->xget(@parts);
141 }
142  
143 sub keys {
144 51 ▶ my ($self, %params) = @_;
Subroutine keys: called CC=6 SCAR=18.3
Pod undocumented
145 51 ▶ my $raw = $params{raw} // 0;
Condition100%2/2
$params{"raw"} // 0
l!l
2625
MC/DC100%1/1
$params{"raw"} // 0
$params{"raw"}
Truth table100%2/2
A
A= $params{"raw"}
Aresult
11
00
146  
147 51 ▶ if (ref $self->value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->value eq "HASH") 26 25 51
148 26 ▶ return map { $raw ? $_ : $self->join($_) } keys %{$self->value};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 33 0 33
149 }
150  
151 25 ▶ if (ref $self->value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->value eq "ARRAY") 25 0 25
152 25 ▶ return map { $raw ? $_ : $self->join($_) } 0 .. $#{$self->value};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 0 61 61
153 }
154  
155 0 return;
156 }
157  
158 17 1;
159  
160 __END__
« Validator/Constraints/Draft6.pm ↑ Summary Validator/URIResolver.pm »