| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 56 | 1114 | ▶ | croak 'JSONPointer: validator is required' unless $validator; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
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"}
| |||||||||||||||||||||||||||||||||||
| 82 | 0 | my $ref = URI->new($current_value->{'$ref'}); | |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $ref = $ref->abs($current_scope) if $current_scope; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
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
| |||||||||||||||||||||||||||||||||||
| 88 | 114 | my $id = $current_value->{$self->validator->ID_FIELD}; | |||||||||||||||||||||||||||||||||
| 89 | 114 | ▶ | if ($id && !ref $id) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$id and not ref $id
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
| |||||||||||||||||||||||||||||||||||
| 90 | 89 | ▶ | $current_scope = $current_scope | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $current_value eq "HASH" and exists $current_value->{$part}
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}
| |||||||||||||||||||||||||||||||||||
| 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
ref $current_value eq "ARRAY" and $part =~ /^\d+$/
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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $current_value eq "HASH" and $current_value->{"\$ref"}
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"}
| |||||||||||||||||||||||||||||||||||
| 107 | 38 | my $ref = URI->new($current_value->{'$ref'}); | |||||||||||||||||||||||||||||||||
| 108 | 38 | ▶ | $ref = $ref->abs($current_scope) if $current_scope; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $current_value eq "HASH" and $self->validator->using_id_with_ref
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
| |||||||||||||||||||||||||||||||||||
| 113 | 230 | my $id = $current_value->{$self->validator->ID_FIELD}; | |||||||||||||||||||||||||||||||||
| 114 | 230 | ▶ | if ($id && !ref $id) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$id and not ref $id
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
| |||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | $current_scope = $current_scope | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | 209 | ▶ | croak "Invalid JSON Pointer $pointer" unless $pointer =~ s!^/!!; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | my @parts = length $pointer | ||||||||||||||||||||||||||||||||||
| 137 | 209 | ▶ | ? map { $self->unescape($_) } split(/\//, $pointer, -1) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$params{"raw"} // 0
$params{"raw"}
Truth table100%2/2
A
A= $params{"raw"}
| |||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | 61 | ▶ | if (ref $self->value eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 148 | 34 | ▶ | return map { $raw ? $_ : $self->join($_) } keys %{$self->value}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 27 | ▶ | if (ref $self->value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | 27 | ▶ | return map { $raw ? $_ : $self->join($_) } 0 .. $#{$self->value}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 0 | return; | |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 17 | 1; | |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||||||||