blib/lib/Struct/Path/JsonPointer.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 40 / 40 Conditioncond 95.2% 20 / 21 MC/DCmcdc 92.8% 13 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 98.8% 168 / 170 CC 30 SCAR 22.5
CC
30
Cov
99%
__ANON__
13.0
path2str
12.0
str2path
7.0
CRAP 9.5
↑ Summary
1 package Struct::Path::JsonPointer;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our @EXPORT_OK = qw(
12 path2str
13 str2path
14 );
15  
16 =head1 NAME
17  
18 Struct::Path::JsonPointer - JsonPointer (L<rfc6901|https://tools.ietf.org/html/rfc6901>)
19 syntax frontend for L<Struct::Path|Struct::Path>
20  
21 =begin html
22  
23 <a href="https://travis-ci.org/mr-mixas/Struct-Path-JsonPointer.pm"><img src="https://travis-ci.org/mr-mixas/Struct-Path-JsonPointer.pm.svg?branch=master" alt="Travis CI"></a>
24 <a href='https://coveralls.io/github/mr-mixas/Struct-Path-JsonPointer.pm?branch=master'><img src='https://coveralls.io/repos/github/mr-mixas/Struct-Path-JsonPointer.pm/badge.svg?branch=master' alt='Coverage Status'/></a>
25 <a href="https://badge.fury.io/pl/Struct-Path-JsonPointer"><img src="https://badge.fury.io/pl/Struct-Path-JsonPointer.svg" alt="CPAN version"></a>
26  
27 =end html
28  
29 =head1 VERSION
30  
31 Version 0.04
32  
33 =cut
34  
35 4 our $VERSION = '0.04';
36  
37 =head1 SYNOPSIS
38  
39 use Struct::Path qw(path);
40 use Struct::Path::JsonPointer qw(str2path);
41  
42 my $data = {
43 "foo" => ["bar", "baz"],
44 "" => 0,
45 "a/b" => 1,
46 "c%d" => 2,
47 "e^f" => 3,
48 "g|h" => 4,
49 "i\\j" => 5,
50 "k\"l" => 6,
51 " " => 7,
52 "m~n" => 8
53 };
54  
55 my ($found) = path($data, str2path('/foo/0'), deref => 1);
56 print $found; # 'bar'
57  
58 =head1 EXPORT
59  
60 Nothing is exported by default.
61  
62 =head1 SUBROUTINES
63  
64 =head2 path2str
65  
66 Convert L<Struct::Path|Struct::Path> path to JsonPointer.
67  
68 $pointer = path2str($path);
69  
70 =cut
71  
72 12 ▶sub path2str {
Subroutine path2str: called CC=12 SCAR=24.8
Pod documented
73 12 ▶ croak "Arrayref expected for path" unless (ref $_[0] eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 11 12
74  
75 11 my $str = '';
76 11 my $i = 0;
77  
78 11 for my $step (@{$_[0]}) {
79 15 ▶ if (ref $step eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $step eq 'ARRAY') { } 4 11 15
elsif (ref $step eq 'HASH') { } 10 1 11
80 croak "Only one array index allowed, step #$i"
81 4 ▶ if (@{$step} != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @{$step;} != 1 1 3 4
82  
83 3 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($step->[0]) and int $step->[0] == $step->[0]) 2 1 3
Condition100%3/3
&looks_like_number($step->[0]) and int $step->[0] == $step->[0]
!ll&&!rl&&r
111
MC/DC100%2/2
&looks_like_number($step->[0]) and int $step->[0] == $step->[0]
&looks_like_number($step->[0]) int $step->[0] == $step->[0]
Truth table100%3/3
A and B
A= &looks_like_number($step->[0])B= int $step->[0] == $step->[0]
ABresult
0X0
100
111
84 looks_like_number($step->[0])
85 and int($step->[0]) == $step->[0]
86 ) {
87 2 croak "Incorrect array index, step #$i";
88 }
89  
90 1 $str .= "/$step->[0]";
91 } elsif (ref $step eq 'HASH') {
92 croak "Only keys allowed for hashes, step #$i"
93 10 ▶ unless (keys %{$step} == 1 and exists $step->{K});
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$step;} == 1 and exists $step->{"K"} 2 8 10
Condition100%3/3
keys %{$step;} == 1 and exists $step->{"K"}
!ll&&!rl&&r
118
MC/DC100%2/2
keys %{$step;} == 1 and exists $step->{"K"}
keys %{$step;} == 1 exists $step->{"K"}
Truth table100%3/3
A and B
A= keys %{$step;} == 1B= exists $step->{"K"}
ABresult
0X0
100
111
94  
95 croak "Incorrect hash keys format, step #$i"
96 8 ▶ unless (ref $step->{K} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $step->{"K"} eq "ARRAY" 1 7 8
97  
98 croak "Only one hash key allowed, step #$i"
99 7 ▶ unless (@{$step->{K}} == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @{$step->{"K"};} == 1 1 6 7
100  
101 6 my $key = $step->{K}->[0];
102 6 $key =~ s|~|~0|g;
103 6 $key =~ s|/|~1|g;
104  
105 6 $str .= "/$key";
106 } else {
107 1 croak "Unsupported thing in the path, step #$i";
108 }
109  
110 7 $i++;
111 }
112  
113 3 return $str;
114 }
115  
116 =head2 str2path
117  
118 Convert JsonPointer to L<Struct::Path|Struct::Path> path.
119  
120 $path = str2path($pointer);
121  
122 =cut
123  
124 # some steps (numbers, dash) should be evaluated using structure
125 22 ▶sub _hook {
Subroutine _hook: called CC=1 SCAR=0.0
126 22 my ($step, $last) = @_;
127  
128 22 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
129 22 ▶ if (ref $_ eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 11 11 22
elsif (ref $_ eq 'HASH') { } 10 1 11
130 11 ▶ if ($step eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($step eq '-') { } 1 10 11
131 1 $step = @{$_}; # Hyphen as array index should append new item
132 } else {
133 10 ▶ croak "Incorrect array index, step #" . @{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
unless $step eq abs int $step 1 9 10
134 unless ($step eq abs(int($step)));
135  
136 2 croak "Index is out of range, step #" . @{$_[0]}
137 9 ▶ if ($step > ($_{opts}->{expand} ? @{$_} : $#{$_}));
Branch: 2 branches
BranchTrueFalseTotal
if $step > ($_{"opts"}{"expand"} ? @{$_;} : $#{$_;}) 2 7 9
$_{'opts'}{'expand'} ? : 2 7 9
138 }
139  
140 8 ▶ splice @{$_}, $step, 0, undef if ($last and $_{opts}->{insert});
Branch: 1 branch
BranchTrueFalseTotal
if $last and $_{"opts"}{"insert"} 1 7 8
Condition100%3/3
$last and $_{"opts"}{"insert"}
!ll&&!rl&&r
161
MC/DC100%2/2
$last and $_{"opts"}{"insert"}
$last $_{"opts"}{"insert"}
Truth table100%3/3
A and B
A= $lastB= $_{"opts"}{"insert"}
ABresult
0X0
100
111
141  
142 8 push @{$_[0]}, [$step]; # update path
143 8 push @{$_[1]}, \$_->[$step]; # update refs stack
144  
145 8 ▶ splice @{$_}, $step, 1 if ($last and $_{opts}->{delete});
Branch: 1 branch
BranchTrueFalseTotal
if $last and $_{"opts"}{"delete"} 1 7 8
Condition100%3/3
$last and $_{"opts"}{"delete"}
!ll&&!rl&&r
161
MC/DC100%2/2
$last and $_{"opts"}{"delete"}
$last $_{"opts"}{"delete"}
Truth table100%3/3
A and B
A= $lastB= $_{"opts"}{"delete"}
ABresult
0X0
100
111
146  
147 } elsif (ref $_ eq 'HASH') {
148 2 croak "'$step' key doesn't exist, step #" . @{$_[0]}
149 10 ▶ unless (exists $_->{$step} or $_{opts}->{expand});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_->{$step} or $_{"opts"}{"expand"} 2 8 10
Condition100%3/3
exists $_->{$step} or $_{"opts"}{"expand"}
l!l&&r!l&&!r
712
MC/DC100%2/2
exists $_->{$step} or $_{"opts"}{"expand"}
exists $_->{$step} $_{"opts"}{"expand"}
Truth table100%3/3
A or B
A= exists $_->{$step}B= $_{"opts"}{"expand"}
ABresult
1X1
011
000
150  
151 8 push @{$_[0]}, {K => [$step]}; # update path
152 8 push @{$_[1]}, \$_->{$step}; # update refs stack
153 } else {
154 1 croak "Structure doesn't match, step #" . @{$_[0]};
155 }
156  
157 16 return 1;
158 }
159 22 };
160  
161 31 ▶sub str2path {
Subroutine str2path: called CC=7 SCAR=19.5
Pod documented
162 31 ▶ croak "Undefined JSON Pointer passed" unless (defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 30 31
163  
164 30 my @steps = split('/', $_[0], -1);
165  
166 30 ▶ croak "JSON Pointer should start with a slash or be empty"
Branch: 1 branch
BranchTrueFalseTotal
if shift @steps and substr($_[0], 0, 1) ne "/" 1 29 30
Condition66%2/3
shift @steps and substr($_[0], 0, 1) ne "/"
!ll&&!rl&&r
2901
MC/DC50%1/2
shift @steps and substr($_[0], 0, 1) ne "/"
shift @steps substr($_[0], 0, 1) ne "/"
Truth table66%2/3
A and B
A= shift @stepsB= substr($_[0], 0, 1) ne "/"
ABresult
0X0
100
111
167 if (shift @steps and substr($_[0], 0, 1) ne '/');
168  
169 29 my @path;
170  
171 29 for (@steps) {
172 39 ▶ if (looks_like_number($_) or $_ eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($_) or $_ eq '-') { } 22 17 39
Condition100%3/3
&looks_like_number($_) or $_ eq "-"
l!l&&r!l&&!r
17517
MC/DC100%2/2
&looks_like_number($_) or $_ eq "-"
&looks_like_number($_) $_ eq "-"
Truth table100%3/3
A or B
A= &looks_like_number($_)B= $_ eq "-"
ABresult
1X1
011
000
173 22 push @path, _hook($_, @path == $#steps);
174 } else { # hash
175 17 s|~1|/|g;
176 17 s|~0|~|g;
177  
178 17 push @path, {K => [$_]};
179 }
180 }
181  
182 29 return \@path;
183 }
184  
185 =head1 AUTHOR
186  
187 Michael Samoglyadov, C<< <mixas at cpan.org> >>
188  
189 =head1 BUGS
190  
191 Please report any bugs or feature requests to
192 C<bug-struct-path-jsonpointer at rt.cpan.org>, or through the web interface at
193 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Path-JsonPointer>. I
194 will be notified, and then you'll automatically be notified of progress on your
195 bug as I make changes.
196  
197 =head1 SUPPORT
198  
199 You can find documentation for this module with the perldoc command.
200  
201 perldoc Struct::Path::JsonPointer
202  
203 You can also look for information at:
204  
205 =over 4
206  
207 =item * RT: CPAN's request tracker (report bugs here)
208  
209 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Struct-Path-JsonPointer>
210  
211 =item * AnnoCPAN: Annotated CPAN documentation
212  
213 L<http://annocpan.org/dist/Struct-Path-JsonPointer>
214  
215 =item * CPAN Ratings
216  
217 L<http://cpanratings.perl.org/d/Struct-Path-JsonPointer>
218  
219 =item * Search CPAN
220  
221 L<http://search.cpan.org/dist/Struct-Path-JsonPointer/>
222  
223 =back
224  
225 =head1 SEE ALSO
226  
227 L<JSON::Pointer>, L<rfc6901|https://tools.ietf.org/html/rfc6901>
228  
229 L<Struct::Path>, L<Struct::Path::PerlStyle>, L<Struct::Diff>
230  
231 =head1 LICENSE AND COPYRIGHT
232  
233 Copyright 2018 Michael Samoglyadov.
234  
235 This program is free software; you can redistribute it and/or modify it under
236 the terms of either: the GNU General Public License as published by the Free
237 Software Foundation; or the Artistic License.
238  
239 See L<http://dev.perl.org/licenses/> for more information.
240  
241 =cut
242  
243 4 1; # End of Struct::Path::JsonPointer
↑ Summary