| 1 | package JSON::Pointer; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use B; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use JSON::Pointer::Context; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use JSON::Pointer::Exception qw(:all); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use JSON::Pointer::Syntax qw(is_array_numeric_index); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 11 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | sub traverse { | ||||||||||||||||||||||||||||||||
| 19 | 141 | ▶ | my ($class, $document, $pointer, $opts) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
traverse: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 20 | $opts = +{ | ||||||||||||||||||||||||||||||||
| 21 | strict => 1, | ||||||||||||||||||||||||||||||||
| 22 | inclusive => 0, | ||||||||||||||||||||||||||||||||
| 23 | 141 | ▶ | %{ $opts || +{} } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||
| 25 | 141 | $pointer = uri_unescape($pointer); | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 141 | my @tokens = JSON::Pointer::Syntax->tokenize($pointer); | |||||||||||||||||||||||||||||||
| 28 | 141 | my $context = JSON::Pointer::Context->new(+{ | |||||||||||||||||||||||||||||||
| 29 | pointer => $pointer, | ||||||||||||||||||||||||||||||||
| 30 | tokens => \@tokens, | ||||||||||||||||||||||||||||||||
| 31 | target => $document, | ||||||||||||||||||||||||||||||||
| 32 | parent => $document, | ||||||||||||||||||||||||||||||||
| 33 | }); | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 141 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||||||
| 36 | 182 | $context->begin($token); | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 182 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 39 | 182 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 182 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 42 | 129 | ▶ | 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 | 105 | $context->next($parent->{$token}); | |||||||||||||||||||||||||||||||
| 47 | 105 | next; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 50 | 53 | ▶ | if ($token eq '-') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 1 | $token = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 53 | my $max_index = $#{$parent}; | |||||||||||||||||||||||||||||||
| 55 | 53 | ▶ | $max_index++ if $opts->{inclusive}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 53 | ▶ | 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 | 41 | $context->next($parent->[$token]); | |||||||||||||||||||||||||||||||
| 59 | 41 | 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 | 105 | $context->result(1); | |||||||||||||||||||||||||||||||
| 71 | 105 | return $context; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | sub get { | ||||||||||||||||||||||||||||||||
| 75 | 33 | ▶ | my ($class, $document, $pointer, $strict) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 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 | sub contains { | ||||||||||||||||||||||||||||||||
| 90 | 9 | ▶ | my ($class, $document, $pointer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 91 | 9 | my $context = $class->traverse($document, $pointer, +{ strict => 0 }); | |||||||||||||||||||||||||||||||
| 92 | 9 | return $context->result; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | sub add { | ||||||||||||||||||||||||||||||||
| 96 | 34 | ▶ | my ($class, $document, $pointer, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=10 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 34 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 34 | my $context = $class->traverse($patched_document, $pointer, +{ strict => 0, inclusive => 1 }); | |||||||||||||||||||||||||||||||
| 101 | 34 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 102 | 34 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 34 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 105 | 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
| |||||||||||||||||||||||||||||||||
| 106 | ### Parent isn't object | ||||||||||||||||||||||||||||||||
| 107 | 2 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 108 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 109 | context => $context, | ||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 19 | ▶ | if (defined $context->last_token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 18 | $parent->{$context->last_token} = $value; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||||||
| 117 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 118 | 1 | $patched_document = $value; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 19 | return $patched_document; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 124 | 13 | ▶ | unless ($context->result) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 3 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 126 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 127 | context => $context, | ||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 10 | ▶ | if (defined $context->last_token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 10 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 133 | 10 | ▶ | my $target_index = ($context->last_token eq "-") ? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | $parent_array_length : $context->last_token; | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 10 | splice(@$parent, $target_index, 0, $value); | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||||||
| 139 | 0 | $patched_document = $value; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 10 | return $patched_document; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | unless ($context->result) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 0 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 147 | code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, | ||||||||||||||||||||||||||||||||
| 148 | context => $context, | ||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 0 | return $value; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | sub remove { | ||||||||||||||||||||||||||||||||
| 157 | 14 | ▶ | my ($class, $document, $pointer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=12 SCAR=25.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 14 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 14 | my $context = $class->traverse($patched_document, $pointer); | |||||||||||||||||||||||||||||||
| 162 | 13 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 163 | 13 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 13 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 166 | 8 | my $target_member = $context->last_token; | |||||||||||||||||||||||||||||||
| 167 | 8 | ▶ | if (defined $target_member) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | 7 | my $removed = delete $parent->{$target_member}; | |||||||||||||||||||||||||||||||
| 169 | 7 | ▶ | return wantarray ? ($patched_document, $removed) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||||||
| 172 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 173 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | elsif ($type eq "ARRAY") { | ||||||||||||||||||||||||||||||||
| 177 | 4 | my $target_index = $context->last_token; | |||||||||||||||||||||||||||||||
| 178 | 4 | ▶ | if (defined $target_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 3 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 180 | 3 | ▶ | $target_index = $parent_array_length if ($target_index eq "-"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 3 | my $removed = splice(@$parent, $target_index, 1); | |||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | return wantarray ? ($patched_document, $removed) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | else { | ||||||||||||||||||||||||||||||||
| 185 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||
| 190 | 1 | ▶ | 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 | 1 | ▶ | return wantarray ? (undef, $patched_document) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | sub replace { | ||||||||||||||||||||||||||||||||
| 202 | 12 | ▶ | my ($class, $document, $pointer, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 12 | my $patched_document = clone($document); | |||||||||||||||||||||||||||||||
| 205 | 12 | my $context = $class->traverse($patched_document, $pointer); | |||||||||||||||||||||||||||||||
| 206 | 12 | my $parent = $context->parent; | |||||||||||||||||||||||||||||||
| 207 | 12 | my $type = ref $parent; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 12 | ▶ | if ($type eq "HASH") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 4 | my $target_member = $context->last_token; | |||||||||||||||||||||||||||||||
| 211 | 4 | ▶ | if (defined $target_member) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | 3 | my $replaced = $parent->{$context->last_token}; | |||||||||||||||||||||||||||||||
| 213 | 3 | $parent->{$context->last_token} = $value; | |||||||||||||||||||||||||||||||
| 214 | 3 | ▶ | return wantarray ? ($patched_document, $replaced) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||||||
| 217 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 218 | 1 | ▶ | return wantarray ? ($value, $patched_document) : $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||
| 222 | 8 | my $target_index = $context->last_token; | |||||||||||||||||||||||||||||||
| 223 | 8 | ▶ | if (defined $target_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | 6 | my $parent_array_length = $#{$parent} + 1; | |||||||||||||||||||||||||||||||
| 225 | 6 | ▶ | $target_index = $parent_array_length if ($target_index eq "-"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 6 | my $replaced = $parent->[$target_index]; | |||||||||||||||||||||||||||||||
| 227 | 6 | $parent->[$target_index] = $value; | |||||||||||||||||||||||||||||||
| 228 | 6 | ▶ | return wantarray ? ($patched_document, $replaced) : $patched_document; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||
| 231 | ### pointer is empty string (whole document) | ||||||||||||||||||||||||||||||||
| 232 | 2 | ▶ | return wantarray ? ($value, $patched_document) : $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | sub set { | ||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | shift->replace(@_); | ||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | sub copy { | ||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | my ($class, $document, $from_pointer, $to_pointer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 243 | 1 | my $context = $class->traverse($document, $from_pointer); | |||||||||||||||||||||||||||||||
| 244 | 1 | return $class->add($document, $to_pointer, $context->target); | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | sub move { | ||||||||||||||||||||||||||||||||
| 248 | 5 | ▶ | my ($class, $document, $from_pointer, $to_pointer) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
move: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 249 | 5 | my ($patched_document, $removed) = $class->remove($document, $from_pointer); | |||||||||||||||||||||||||||||||
| 250 | 5 | $class->add($patched_document, $to_pointer, $removed); | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | sub test { | ||||||||||||||||||||||||||||||||
| 254 | 23 | ▶ | my ($class, $document, $pointer, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
test: called CC=16 SCAR=29.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 23 | my $context = $class->traverse($document, $pointer, +{ strict => 0 }); | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 23 | ▶ | return 0 unless $context->result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 22 | my $target = $context->target; | |||||||||||||||||||||||||||||||
| 261 | 22 | my $target_type = ref $target; | |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 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"
| |||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | return encode_json($target) eq encode_json($value) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | elsif (defined $target) { | ||||||||||||||||||||||||||||||||
| 267 | 17 | ▶ | if (JSON::is_bool($target)) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 268 | 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
| |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||
| 270 | ▶ | 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)
| |||||||||||||||||||||||||||||||||
| 271 | 12 | ▶ | return $target == $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | ▶ | 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)
| |||||||||||||||||||||||||||||||||
| 274 | 4 | ▶ | return $target eq $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||||||
| 277 | 1 | return 0; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | else { | ||||||||||||||||||||||||||||||||
| 281 | ### null | ||||||||||||||||||||||||||||||||
| 282 | 1 | ▶ | return !defined $value ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | sub _throw_or_return { | ||||||||||||||||||||||||||||||||
| 287 | 36 | ▶ | my ($code, $context, $strict) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_throw_or_return: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 36 | ▶ | if ($strict) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | 4 | JSON::Pointer::Exception->throw( | |||||||||||||||||||||||||||||||
| 291 | code => $code, | ||||||||||||||||||||||||||||||||
| 292 | context => $context, | ||||||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | else { | ||||||||||||||||||||||||||||||||
| 296 | 32 | $context->last_error($code); | |||||||||||||||||||||||||||||||
| 297 | 32 | return $context; | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | sub _is_iv_or_nv { | ||||||||||||||||||||||||||||||||
| 302 | 30 | ▶ | my $value = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_is_iv_or_nv: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 303 | 30 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||||||||||||||||||||||||
| 304 | 30 | ▶ | return ( ($flags & ( B::SVp_IOK | B::SVp_NOK )) and !($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)
| |||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | sub _is_pv { | ||||||||||||||||||||||||||||||||
| 308 | 9 | ▶ | my $value = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_is_pv: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 309 | 9 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||||||||||||||||||||||||
| 310 | 9 | ▶ | return ( !($flags & ( B::SVp_IOK | B::SVp_NOK )) and ($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
| |||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 11 | 1; | |||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | __END__ | ||||||||||||||||||||||||||||||||