AsType/Registry.pm

filter: Statementstmt 86.8% 119 / 137 Branchbran 77.0% 37 / 48 Conditioncond 79.1% 19 / 24 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 7 totaltotal 80.3% 205 / 255 CC 37 SCAR 20.6
CC
37
Cov
84%
fetch
16.9
_resolve_uri
12.0
resolve_json_pointer
7.0
CRAP 7.9
« AsType/Visit.pm ↑ Summary AsType/Draft2019_09/Vocabulary/Validation.pm »
1 package JSON::Schema::AsType::Registry;
2 45 our $AUTHORITY = 'cpan:YANICK';
3 45 $JSON::Schema::AsType::Registry::VERSION = '1.0.0';
4 # ABSTRACT: Schema registry for JSON::Schema::AsType
5  
6  
7 45 ▶use 5.42.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 45 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 45 ▶use Test::Deep::NoTest qw( eq_deeply );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 ▶use JSON::Pointer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 45 ▶use JSON qw( from_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 45 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 ▶use Ref::Util qw( is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 45 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has registry => (
22 is => 'ro',
23 lazy => 1,
24 137 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 45 traits => ['Hash'],
26 handles => {},
27 );
28  
29 45 has fetch_remote => (
30 is => 'ro',
31 default => 1,
32 );
33  
34 5 ▶sub all_schema_uris($self) {
Subroutine all_schema_uris: called CC=1 SCAR=0.0
Pod undocumented
35 5 return sort keys $self->registry->%*;
36 }
37  
38 6455 ▶sub register_schema {
Subroutine register_schema: called CC=5 SCAR=16.2
Pod undocumented
39  
40 # TODO Use a type instead to coerce into canonical
41 6455 my ( $self, $uri, $schema ) = @_;
42  
43 6455 $uri = URI->new($uri)->canonical;
44  
45 6455 ▶ if ( my $already = $self->registered_schema($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $already = $self->registered_schema($uri)) 893 5562 6455
46 893 my $s = $schema;
47 893 ▶ $s = $s->schema if $s isa JSON::Schema::AsType;
Branch: 1 branch
BranchTrueFalseTotal
if $s isa "JSON::Schema::AsType" 867 26 893
48 893 ▶ return $already if eq_deeply( $s, $already->schema );
Branch: 1 branch
BranchTrueFalseTotal
if eq_deeply($s, $already->schema) 893 0 893
49 45 ▶ use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 0 p [ $s, $already->schema ]->@*;
51 0 die "schema $uri already registered for a different schema\n";
52 }
53  
54 5562 ▶ unless ( $schema isa JSON::Schema::AsType ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema isa "JSON::Schema::AsType") 453 5109 5562
55  
56 # TODO for Draft4
57 453 $schema = $self->sub_schema( $schema, $uri );
58 }
59  
60 5562 $self->registry->{$uri} = $schema;
61 }
62  
63 15 ▶sub merge_register( $self, $other ) {
Subroutine merge_register: called CC=2 SCAR=6.9
Pod undocumented
64 15 for my ( $url, $schema ) ( $other->registry->%* ) {
65 83 $self->register_schema( $url, $schema );
66 }
67 }
68  
69 8566 ▶sub registered_schema( $self, $uri ) {
Subroutine registered_schema: called CC=1 SCAR=0.0
Pod undocumented
70 8566 $uri = URI->new($uri)->canonical;
71 8566 return $self->registry->{$uri};
72 }
73  
74 1337 ▶sub fetch {
Subroutine fetch: called CC=13 SCAR=28.3
Pod undocumented
75 1337 my ( $self, $url ) = @_;
76  
77 1337 $DB::single = 1;
78  
79 1337 $url = $self->resolve_uri( $url, $self->uri );
80  
81 # # is it one of the spec schemas?
82 # if ( $url =~ qr[^https?://json-schema.org/draft-0?(\d+)/schema] ) {
83  
84 # # TODO get the metaschema
85 # my $module = 'JSON::Schema::AsType::Draft' . $1
86 # use_module($module)->metaschema;
87 # }
88  
89 # urgh...
90 1337 ▶ $url->scheme("https")
Branch: 1 branch
BranchTrueFalseTotal
if $url->can("host") and $url->host eq "json-schema.org" 927 410 1337
Condition100%3/3
$url->can("host") and $url->host eq "json-schema.org"
!ll&&!rl&&r
23387927
MC/DC100%2/2
$url->can("host") and $url->host eq "json-schema.org"
$url->can("host") $url->host eq "json-schema.org"
Truth table100%3/3
A and B
A= $url->can("host")B= $url->host eq "json-schema.org"
ABresult
0X0
100
111
91 if $url->can('host')
92 and $url->host eq 'json-schema.org';
93  
94 # $url->fragment( $fragment =~ s[/+$][]r ) if $fragment;
95  
96 1337 ▶ if ( my $schema = $self->registered_schema($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $self->registered_schema($url)) 1163 174 1337
97 1163 return $schema;
98 }
99  
100 174 my $root_uri = $url->clone;
101 174 $root_uri->fragment(undef);
102  
103 174 my $schema = $self->registered_schema($root_uri);
104  
105 174 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 141 33 174
106 141 my $fragment = $url->fragment;
107  
108 # $fragment =~ s#/+$##;
109 141 $url->fragment(undef);
110 141 my ( $s, $jp_url ) =
111 $self->resolve_json_pointer( $schema->schema, $fragment, $url );
112 137 ▶ unless ( $s or ref $s eq 'JSON::PP::Boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s or ref $s eq "JSON::PP::Boolean") 0 137 137
Condition66%2/3
$s or ref $s eq "JSON::PP::Boolean"
l!l&&r!l&&!r
13610
MC/DC0%0/2
$s or ref $s eq "JSON::PP::Boolean"
$s ref $s eq "JSON::PP::Boolean"
Truth table66%2/3
A or B
A= $sB= ref $s eq "JSON::PP::Boolean"
ABresult
1X1
011
000
113 0 die "reference #" . $fragment . ' not found';
114 }
115  
116 137 return $self->register_schema( $jp_url => $s );
117 }
118  
119 33 ▶ if ( $root_uri->host eq 'json-schema.org'
Branch: 1 branch
BranchTrueFalseTotal
if ($root_uri->host eq "json-schema.org" and $root_uri->path eq "/v1") 18 15 33
Condition66%2/3
$root_uri->host eq "json-schema.org" and $root_uri->path eq "/v1"
!ll&&!rl&&r
01518
MC/DC50%1/2
$root_uri->host eq "json-schema.org" and $root_uri->path eq "/v1"
$root_uri->host eq "json-schema.org" $root_uri->path eq "/v1"
Truth table66%2/3
A and B
A= $root_uri->host eq "json-schema.org"B= $root_uri->path eq "/v1"
ABresult
0X0
100
111
120 and $root_uri->path eq '/v1' ) {
121  
122 # TODO
123 18 return $self->new(
124 schema => {},
125 uri => 'https://json-schema.org/v1'
126 );
127 }
128  
129 15 ▶ if ( $root_uri->host eq 'json-schema.org'
Branch: 1 branch
BranchTrueFalseTotal
if ($root_uri->host eq "json-schema.org" and $root_uri->path =~ m[/draft(?:-0?|/)([\d-]+)/schema$]u) 15 0 15
Condition33%1/3
$root_uri->host eq "json-schema.org" and $root_uri->path =~ m[/draft(?:-0?|/)([\d-]+)/schema$]u
!ll&&!rl&&r
0015
MC/DC0%0/2
$root_uri->host eq "json-schema.org" and $root_uri->path =~ m[/draft(?:-0?|/)([\d-]+)/schema$]u
$root_uri->host eq "json-schema.org" $root_uri->path =~ m[/draft(?:-0?|/)([\d-]+)/schema$]u
Truth table33%1/3
A and B
A= $root_uri->host eq "json-schema.org"B= $root_uri->path =~ m[/draft(?:-0?|/)([\d-]+)/schema$]u
ABresult
0X0
100
111
130 and $root_uri->path =~ m#/draft(?:-0?|/)([\d-]+)/schema$# ) {
131 15 my $module = 'JSON::Schema::AsType::Draft' . $1 =~ s/-/_/r;
132 15 my $ms = use_module($module)->_metaschema;
133 15 $self->merge_register($ms);
134 15 goto __SUB__;
135 }
136  
137 0 ▶ die "fetching remote schemas disabled, can't retrieve $url\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fetch_remote 0 0 0
138 unless $self->fetch_remote;
139  
140 0 $schema = eval { from_json LWP::Simple::get($url) };
141  
142 0 ▶ die "couldn't get schema from '$url'\n" unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 0 0
143  
144 0 return $self->register_schema(
145 $url => $self->new( uri => $url, schema => $schema ) );
146 }
147  
148 141 ▶sub resolve_json_pointer( $self, $schema, $pointer, $url ) {
Subroutine resolve_json_pointer: called CC=7 SCAR=19.5
Pod undocumented
149  
150 141 ▶ $url = $self->resolve_uri( $self->_has_id($schema), $url )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_id($schema) 63 78 141
151 if $self->_has_id($schema);
152  
153 141 $pointer =~ s[^/][];
154  
155 141 my @path_entries = split '/', $pointer;
156 141 ▶ push @path_entries, '' if $pointer =~ m#/.*/$#;
Branch: 1 branch
BranchTrueFalseTotal
if $pointer =~ m[/.*/$]u 4 137 141
157 141 for my $path (@path_entries) {
158 289 $path = $self->_unescape_ref($path);
159  
160 289 ▶ $schema = is_hashref($schema) ? $schema->{$path} : $schema->[$path];
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($schema) ? : 289 0 289
161  
162 289 ▶ die "reference " . $url . "#$pointer not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 4 285 289
163 unless defined $schema;
164  
165 285 $path = $self->_escape_ref($path);
166  
167 285 ▶ $url =
Condition100%2/2
$self->_has_id($schema) // "#./$path"
l!l
17268
MC/DC100%1/1
$self->_has_id($schema) // "#./$path"
$self->_has_id($schema)
Truth table100%2/2
A
A= $self->_has_id($schema)
Aresult
11
00
168 $self->resolve_uri( $self->_has_id($schema) // "#./$path", $url );
169 }
170  
171 137 return ( $schema, $url );
172  
173 }
174  
175 6954 ▶sub resolve_uri( $self, $uri, $base = undef ) {
Subroutine resolve_uri: called CC=3 SCAR=11.2
Pod undocumented
176 6954 ▶ return _resolve_uri( $uri, $base // $self->uri );
Condition66%2/3
$base // $self->uri
l!l&&r!l&&!r
168552690
MC/DC0%0/2
$base // $self->uri
$base $self->uri
Truth table66%2/3
A // B
A= $baseB= $self->uri
ABresult
1X1
011
000
177 }
178  
179 6962 ▶sub _resolve_uri {
Subroutine _resolve_uri: called CC=12 SCAR=24.9
180 6962 my ( $uri, $base ) = @_;
181 6962 $uri = URI->new($uri);
182 6962 $base = URI->new($base);
183  
184 6962 ▶ return $uri unless $base;
Branch: 1 branch
BranchTrueFalseTotal
unless $base 1 6961 6962
185  
186 6961 my $result;
187 6961 ▶ if ( $base isa 'URI::urn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base isa "URI::urn") 186 6775 6961
188 186 ▶ return URI->new($uri)->canonical if $uri !~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ /^#/u 101 85 186
189 85 $result = $base->clone;
190 85 $result->fragment( $uri =~ s/^#//r );
191 85 return $result;
192 }
193  
194 6775 $result = URI->new($uri)->abs($base)->canonical;
195  
196 # let's look at those fragments
197 6775 my $uri_doc = $uri->clone;
198 6775 $uri_doc->fragment(undef);
199 6775 my $base_doc = $base->clone;
200 6775 $base_doc->fragment(undef);
201  
202 6775 ▶ if ( !"$uri_doc" or $uri_doc->eq($base_doc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not "$uri_doc" or $uri_doc->eq($base_doc)) { } 4455 2320 6775
Condition100%3/3
not "$uri_doc" or $uri_doc->eq($base_doc)
l!l&&r!l&&!r
37576982320
MC/DC100%2/2
not "$uri_doc" or $uri_doc->eq($base_doc)
not "$uri_doc" $uri_doc->eq($base_doc)
Truth table100%3/3
not(A) or B
A= not "$uri_doc"B= $uri_doc->eq($base_doc)
ABresult
1X1
011
000
203 45 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 4455 my $fragment = $uri->fragment;
205  
206 4455 ▶ if ( $fragment =~ m[^\.] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment =~ /^\./u) { } 3453 1002 4455
207 3453 my $base_fragment = $base->fragment;
208 3453 ▶ $base_fragment .= '/' unless m[/$];
Branch: 1 branch
BranchTrueFalseTotal
unless m[/$]u 3453 0 3453
209  
210 3453 my $path = URI->new($fragment);
211 3453 ▶ $path = $path->abs($base_fragment) if $base_fragment;
Branch: 1 branch
BranchTrueFalseTotal
if $base_fragment 3453 0 3453
212 3453 $path = $path->canonical;
213  
214 3453 ▶ $result->fragment($path) unless $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "/" 3453 0 3453
215 }
216 else {
217 1002 ▶ $result->fragment( $fragment || undef );
Condition100%2/2
$fragment || undef
l!l
796206
MC/DC100%1/1
$fragment || undef
$fragment
Truth table100%2/2
A
A= $fragment
Aresult
11
00
218 }
219  
220 }
221 else {
222 # not the same documents? fragment stays the same
223 45 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
224 2320 ▶ $result->fragment( $uri->fragment || undef );
Condition100%2/2
$uri->fragment || undef
l!l
1572163
MC/DC100%1/1
$uri->fragment || undef
$uri->fragment
Truth table100%2/2
A
A= $uri->fragment
Aresult
11
00
225 }
226  
227 6775 return $result;
228  
229 }
230  
231 0 ▶sub _unescape_ref {
Subroutine _unescape_ref: not called CC=1 SCAR=6.9
232 0 my ( $self, $ref ) = @_;
233  
234 0 $ref =~ s/~0/~/g;
235 0 $ref =~ s!~1!/!g;
236 0 $ref =~ s!%25!%!g;
237  
238 0 $ref;
239 }
240  
241 0 ▶sub _escape_ref {
Subroutine _escape_ref: not called CC=1 SCAR=6.9
242 0 my ( $self, $ref ) = @_;
243  
244 0 $ref =~ s/~/~0/g;
245 0 $ref =~ s!/!~1!g;
246 0 $ref =~ s!%!%25!g;
247  
248 0 $ref;
249 }
250  
251 __END__
« AsType/Visit.pm ↑ Summary AsType/Draft2019_09/Vocabulary/Validation.pm »