| 1 | package JSON::Pointer; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use B; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use JSON::Pointer::Context; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use JSON::Pointer::Exception qw(:all); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use JSON::Pointer::Syntax qw(is_array_numeric_index); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 12 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 147 | ▶ | sub traverse { | ||||||||||||||||||||||||||||||
|
Subroutine
traverse: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 19 | 147 | my ($class, $document, $pointer, $opts) = @_; | |||||||||||||||||||||||||||||||
| 20 | $opts = +{ | ||||||||||||||||||||||||||||||||
| 21 | strict => 1, | ||||||||||||||||||||||||||||||||
| 22 | inclusive => 0, | ||||||||||||||||||||||||||||||||
| 23 | 147 | ▶ | %{ $opts || +{} } | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts || {}
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||
| 25 | 147 | $pointer = uri_unescape($pointer); | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 147 | my @tokens = JSON::Pointer::Syntax->tokenize($pointer); | |||||||||||||||||||||||||||||||
| 28 | 147 | my $context = JSON::Pointer::Context->new(+{ | |||||||||||||||||||||||||||||||
| 29 | pointer => $pointer, | ||||||||||||||||||||||||||||||||
| 30 | tokens => \@tokens, | ||||||||||||||||||||||||||||||||
| 31 | target => $document, | ||||||||||||||||||||||||||||||||
| 32 | parent => $document, | ||||||||||||||||||||||||||||||||
| 33 | }); | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 147 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||||||
| 36 | 197 | $context->begin($token); | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 197 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 39 | 197 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 197 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 42 | 141 | ▶ | unless (exists $parent->{$token}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 24 | return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict}); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 117 | $context->next($parent->{$token}); | |||||||||||||||||||||||||||||||
| 47 | 117 | next; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 50 | 56 | ▶ | if ($token eq '-') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 1 | $token = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 56 | my $max_index = $#{$parent}; | |||||||||||||||||||||||||||||||
| 55 | 56 | ▶ | $max_index++ if $opts->{inclusive}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 56 | ▶ | if (is_array_numeric_index($token) && $token <= $max_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_array_numeric_index($token) and $token <= $max_index
MC/DC100%2/2
is_array_numeric_index($token) and $token <= $max_index
is_array_numeric_index($token)
$token <= $max_index
Truth table100%3/3
A and B
A= is_array_numeric_index($token)B= $token <= $max_index
| |||||||||||||||||||||||||||||||||
| 58 | 44 | $context->next($parent->[$token]); | |||||||||||||||||||||||||||||||
| 59 | 44 | next; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||||
| 62 | 12 | return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict}); | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||
| 66 | 0 | return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict}); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 111 | $context->result(1); | |||||||||||||||||||||||||||||||
| 71 | 111 | return $context; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 33 | ▶ | sub get { | ||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 33 | my ($class, $document, $pointer, $strict) = @_; | |||||||||||||||||||||||||||||||
| 76 | 33 | ▶ | $strict = 0 unless defined $strict; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 33 | my $context; | |||||||||||||||||||||||||||||||
| 79 | 33 | eval { | |||||||||||||||||||||||||||||||
| 80 | 33 | $context = $class->traverse($document, $pointer, +{ strict => $strict }); | |||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||
| 82 | 33 | ▶ | if (my $e = $@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 3 | croak $e; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 30 | ▶ | return $context->result ? $context->target : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 12 | ▶ | sub get_relative { | ||||||||||||||||||||||||||||||
|
Subroutine
get_relative: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 90 | 12 | my ($class, $document, $current_pointer, $relative_pointer, $strict) = @_; | |||||||||||||||||||||||||||||||
| 91 | 12 | ▶ | $strict = 0 unless defined $strict; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 12 | my @current_tokens = JSON::Pointer::Syntax->tokenize($current_pointer); | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 12 | my $context = JSON::Pointer::Context->new(+{ | |||||||||||||||||||||||||||||||
| 96 | pointer => $current_pointer, | ||||||||||||||||||||||||||||||||
| 97 | tokens => \@current_tokens, | ||||||||||||||||||||||||||||||||
| 98 | target => $document, | ||||||||||||||||||||||||||||||||
| 99 | parent => $document, | ||||||||||||||||||||||||||||||||
| 100 | }); | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 12 | my ($steps, $relative_pointer_suffix, $use_index) = | |||||||||||||||||||||||||||||||
| 103 | ($relative_pointer =~ m{^(0|[1-9]?[0-9]+)([^#]*)(#?)$}); | ||||||||||||||||||||||||||||||||
| 104 | 12 | ▶ | $relative_pointer_suffix ||= ""; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$relative_pointer_suffix ||= ""
MC/DC100%1/1
$relative_pointer_suffix ||= ""
$relative_pointer_suffix
Truth table100%2/2
A
A= $relative_pointer_suffix
| |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 12 | ▶ | unless (defined $steps) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 1 | return _throw_or_return(ERROR_INVALID_POINTER_SYNTAX, $context, +{ strict => $strict }); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 11 | ▶ | for (my $i = 0; $i < $steps; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 11 | ▶ | if (@current_tokens == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 1 | return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, +{ strict => $strict }); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | 10 | pop(@current_tokens); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | if ($use_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 4 | my @relative_tokens = JSON::Pointer::Syntax->tokenize($relative_pointer_suffix); | |||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | return (@relative_tokens > 0) ? $relative_tokens[-1] : $current_tokens[-1]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 6 | my $absolute_pointer = JSON::Pointer::Syntax->as_pointer(@current_tokens) . $relative_pointer_suffix; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 6 | eval { | |||||||||||||||||||||||||||||||
| 125 | 6 | $context = $class->traverse($document, $absolute_pointer, +{ strict => $strict }); | |||||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||||||
| 127 | 6 | ▶ | if (my $e = $@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 0 | croak $e; | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | return $context->result ? $context->target : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | sub contains { | ||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 135 | 9 | my ($class, $document, $pointer) = @_; | |||||||||||||||||||||||||||||||
| 136 | 9 | my $context = $class->traverse($document, $pointer, +{ strict => 0 }); | |||||||||||||||||||||||||||||||
| 137 | 9 | return $context->result; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 34 | ▶ | sub add { | ||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=10 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 141 | 34 | my ($class, $document, $pointer, $value) = @_; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 34 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 34 | my $context = $class->traverse($patched_document, $pointer, +{ strict => 0, inclusive => 1 }); | |||||||||||||||||||||||||||||||
| 146 | 34 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 147 | 34 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 34 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 150 | 21 | ▶ | if (!$context->result && @{$context->processed_tokens} < @{$context->tokens} - 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $context->result and @{$context->processed_tokens;} < @{$context->tokens;} - 1
MC/DC100%2/2
not $context->result and @{$context->processed_tokens;} < @{$context->tokens;} - 1
not $context->result
@{$context->processed_tokens;} < @{$context->tokens;} - 1
Truth table100%3/3
not(A) and B
A= not $context->resultB= @{$context->processed_tokens;} < @{$context->tokens;} - 1
| |||||||||||||||||||||||||||||||||
| 151 | ### Parent isn't object | ||||||||||||||||||||||||||||||||
| 152 | 2 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 153 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 154 | context => $context, | ||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 19 | ▶ | if (defined $context->last_token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | 18 | $parent->{$context->last_token} = $value; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||||||
| 162 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 163 | 1 | $patched_document = $value; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 19 | return $patched_document; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 169 | 13 | ▶ | unless ($context->result) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 3 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 171 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 172 | context => $context, | ||||||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 10 | ▶ | if (defined $context->last_token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 10 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 178 | 10 | ▶ | my $target_index = ($context->last_token eq "-") ? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | $parent_array_length : $context->last_token; | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 10 | splice(@$parent, $target_index, 0, $value); | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||||||
| 184 | 0 | $patched_document = $value; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 10 | return $patched_document; | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | unless ($context->result) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | 0 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 192 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 193 | context => $context, | ||||||||||||||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 0 | return $value; | |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 14 | ▶ | sub remove { | ||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=12 SCAR=25.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 202 | 14 | my ($class, $document, $pointer) = @_; | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 14 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 14 | my $context = $class->traverse($patched_document, $pointer); | |||||||||||||||||||||||||||||||
| 207 | 13 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 208 | 13 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 13 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 211 | 8 | my $target_member = $context->last_token; | |||||||||||||||||||||||||||||||
| 212 | 8 | ▶ | if (defined $target_member) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | 7 | my $removed = delete $parent->{$target_member}; | |||||||||||||||||||||||||||||||
| 214 | 7 | ▶ | return wantarray ? ($patched_document, $removed) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||||||
| 217 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 218 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 222 | 4 | my $target_index = $context->last_token; | |||||||||||||||||||||||||||||||
| 223 | 4 | ▶ | if (defined $target_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | 3 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 225 | 3 | ▶ | $target_index = $parent_array_length if ($target_index eq "-"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 3 | my $removed = splice(@$parent, $target_index, 1); | |||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | return wantarray ? ($patched_document, $removed) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | else { | ||||||||||||||||||||||||||||||||
| 230 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||||||
| 235 | 1 | ▶ | unless ($context->result) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 0 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 237 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 238 | context => $context, | ||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 12 | ▶ | sub replace { | ||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 247 | 12 | my ($class, $document, $pointer, $value) = @_; | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 12 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 250 | 12 | my $context = $class->traverse($patched_document, $pointer); | |||||||||||||||||||||||||||||||
| 251 | 12 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 252 | 12 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 12 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 255 | 4 | my $target_member = $context->last_token; | |||||||||||||||||||||||||||||||
| 256 | 4 | ▶ | if (defined $target_member) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | 3 | my $replaced = $parent->{$context->last_token}; | |||||||||||||||||||||||||||||||
| 258 | 3 | $parent->{$context->last_token} = $value; | |||||||||||||||||||||||||||||||
| 259 | 3 | ▶ | return wantarray ? ($patched_document, $replaced) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | else { | ||||||||||||||||||||||||||||||||
| 262 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 263 | 1 | ▶ | return wantarray ? ($value, $patched_document) : $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | else { | ||||||||||||||||||||||||||||||||
| 267 | 8 | my $target_index = $context->last_token; | |||||||||||||||||||||||||||||||
| 268 | 8 | ▶ | if (defined $target_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 269 | 6 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 270 | 6 | ▶ | $target_index = $parent_array_length if ($target_index eq "-"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 6 | my $replaced = $parent->[$target_index]; | |||||||||||||||||||||||||||||||
| 272 | 6 | $parent->[$target_index] = $value; | |||||||||||||||||||||||||||||||
| 273 | 6 | ▶ | return wantarray ? ($patched_document, $replaced) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||
| 275 | else { | ||||||||||||||||||||||||||||||||
| 276 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 277 | 2 | ▶ | return wantarray ? ($value, $patched_document) : $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 283 | 0 | shift->replace(@_); | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | sub copy { | ||||||||||||||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 287 | 1 | my ($class, $document, $from_pointer, $to_pointer) = @_; | |||||||||||||||||||||||||||||||
| 288 | 1 | my $context = $class->traverse($document, $from_pointer); | |||||||||||||||||||||||||||||||
| 289 | 1 | return $class->add($document, $to_pointer, $context->target); | |||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 5 | ▶ | sub move { | ||||||||||||||||||||||||||||||
|
Subroutine
move: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 293 | 5 | my ($class, $document, $from_pointer, $to_pointer) = @_; | |||||||||||||||||||||||||||||||
| 294 | 5 | my ($patched_document, $removed) = $class->remove($document, $from_pointer); | |||||||||||||||||||||||||||||||
| 295 | 5 | $class->add($patched_document, $to_pointer, $removed); | |||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | 23 | ▶ | sub test { | ||||||||||||||||||||||||||||||
|
Subroutine
test: called CC=16 SCAR=29.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 299 | 23 | my ($class, $document, $pointer, $value) = @_; | |||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | 23 | my $context = $class->traverse($document, $pointer, +{ strict => 0 }); | |||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | 23 | ▶ | return 0 unless $context->result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | 22 | my $target = $context->target; | |||||||||||||||||||||||||||||||
| 306 | 22 | my $target_type = ref $target; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 22 | ▶ | if ($target_type eq "HASH" || $target_type eq "ARRAY") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$target_type eq "HASH" or $target_type eq "ARRAY"
MC/DC100%2/2
$target_type eq "HASH" or $target_type eq "ARRAY"
$target_type eq "HASH"
$target_type eq "ARRAY"
Truth table100%3/3
A or B
A= $target_type eq "HASH"B= $target_type eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 309 | 4 | ▶ | return encode_json($target) eq encode_json($value) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||
| 311 | elsif (defined $target) { | ||||||||||||||||||||||||||||||||
| 312 | 17 | ▶ | if (JSON::is_bool($target)) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | return JSON::is_bool($value) && $target == $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&JSON::is_bool($value) and $target == $value
MC/DC0%0/2
&JSON::is_bool($value) and $target == $value
&JSON::is_bool($value)
$target == $value
Truth table0%0/3
A and B
A= &JSON::is_bool($value)B= $target == $value
| |||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||
| 315 | ▶ | elsif (_is_iv_or_nv($target) && _is_iv_or_nv($value)) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
_is_iv_or_nv($target) and _is_iv_or_nv($value)
MC/DC100%2/2
_is_iv_or_nv($target) and _is_iv_or_nv($value)
_is_iv_or_nv($target)
_is_iv_or_nv($value)
Truth table100%3/3
A and B
A= _is_iv_or_nv($target)B= _is_iv_or_nv($value)
| |||||||||||||||||||||||||||||||||
| 316 | 12 | ▶ | return $target == $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | ▶ | elsif (_is_pv($target) && _is_pv($value)) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
_is_pv($target) and _is_pv($value)
MC/DC50%1/2
_is_pv($target) and _is_pv($value)
_is_pv($target)
_is_pv($value)
Truth table66%2/3
A and B
A= _is_pv($target)B= _is_pv($value)
| |||||||||||||||||||||||||||||||||
| 319 | 4 | ▶ | return $target eq $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||||||
| 322 | 1 | return 0; | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | else { | ||||||||||||||||||||||||||||||||
| 326 | ### null | ||||||||||||||||||||||||||||||||
| 327 | 1 | ▶ | return !defined $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 38 | ▶ | sub _throw_or_return { | ||||||||||||||||||||||||||||||
|
Subroutine
_throw_or_return: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 332 | 38 | my ($code, $context, $strict) = @_; | |||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 38 | ▶ | if ($strict) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 335 | 6 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 336 | code => $code, | ||||||||||||||||||||||||||||||||
| 337 | context => $context, | ||||||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | else { | ||||||||||||||||||||||||||||||||
| 341 | 32 | $context->last_error($code); | |||||||||||||||||||||||||||||||
| 342 | 32 | return $context; | |||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 30 | ▶ | sub _is_iv_or_nv { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_iv_or_nv: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 347 | 30 | my $value = shift; | |||||||||||||||||||||||||||||||
| 348 | 30 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||||||||||||||||||||||||
| 349 | 30 | ▶ | return ( ($flags & ( B::SVp_IOK | B::SVp_NOK )) && !($flags & B::SVp_POK) ); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$flags & 12288 && !($flags & 16384)
MC/DC50%1/2
$flags & 12288 && !($flags & 16384)
$flags & 12288
!($flags & 16384)
Truth table66%2/3
A && not(B)
A= $flags & 12288B= !($flags & 16384)
| |||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 9 | ▶ | sub _is_pv { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_pv: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 353 | 9 | my $value = shift; | |||||||||||||||||||||||||||||||
| 354 | 9 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||||||||||||||||||||||||
| 355 | 9 | ▶ | return ( !($flags & ( B::SVp_IOK | B::SVp_NOK )) && ($flags & B::SVp_POK) ); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
!($flags & 12288) && $flags & 16384
MC/DC50%1/2
!($flags & 12288) && $flags & 16384
!($flags & 12288)
$flags & 16384
Truth table66%2/3
not(A) && B
A= !($flags & 12288)B= $flags & 16384
| |||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 12 | 1; | |||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | __END__ | ||||||||||||||||||||||||||||||||