JSV/Reference.pm

filter: Statementstmt 92.5% 75 / 81 Branchbran 76.6% 23 / 30 Conditioncond 81.4% 22 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 6 totaltotal 81.1% 142 / 175 CC 25 SCAR 18.2
CC
25
Cov
87%
resolve
10.3
get_schema
9.7
__ANON__
6.1
CRAP 6.2
« JSV/Keyword/Draft4/Properties.pm ↑ Summary JSV/Keyword/Draft4/Type.pm »
1 package JSV::Reference;
2  
3 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 48 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use Data::Walk;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 48 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use URI::Split qw(uri_split uri_join);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 48 my %memo;
15  
16 63 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
17 63 my $class = shift;
18 63 ▶ my $args = ref $_[0] ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 63 63
19  
20 63 %$args = (
21 registered_schema_map => {},
22 max_recursion => 10,
23 %$args,
24 );
25  
26 63 bless $args => $class;
27 }
28  
29 79 ▶sub resolve {
Subroutine resolve: called CC=10 SCAR=23.3
Pod undocumented
30 79 my ($self, $ref, $opts) = @_;
31 79 ▶ die 'ref value should be hash' unless ref $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "HASH" 0 79 79
32 79 ▶ die '$ref not found' unless exists $ref->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ref->{"\$ref"} 0 79 79
33 79 my $ref_uri = URI->new($ref->{'$ref'});
34  
35 79 ▶ if ( ! $ref_uri->scheme && $opts->{base_uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ref_uri->scheme and $opts->{"base_uri"}) 4 75 79
Condition100%3/3
not $ref_uri->scheme and $opts->{"base_uri"}
!ll&&!rl&&r
49264
MC/DC100%2/2
not $ref_uri->scheme and $opts->{"base_uri"}
not $ref_uri->scheme $opts->{"base_uri"}
Truth table100%3/3
not(A) and B
A= not $ref_uri->schemeB= $opts->{"base_uri"}
ABresult
0X0
100
111
36 4 $ref_uri = $ref_uri->abs($opts->{base_uri});
37 }
38
39 79 ▶ die '$ref format invalid' unless $ref_uri->scheme || $ref_uri->fragment || $ref_uri->as_string eq "#";
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_uri->scheme or $ref_uri->fragment or $ref_uri->as_string eq "#" 0 79 79
Condition83%5/6
$ref_uri->scheme or $ref_uri->fragment or $ref_uri->as_string eq "#"
l!l&&r!l&&!r
7720
$ref_uri->scheme or $ref_uri->fragment
l!l&&r!l&&!r
53242
MC/DC0%0/3
$ref_uri->scheme or $ref_uri->fragment or $ref_uri->as_string eq "#"
$ref_uri->scheme $ref_uri->fragment $ref_uri->as_string eq "#"
Truth table75%3/4
A or B or C
A= $ref_uri->schemeB= $ref_uri->fragmentC= $ref_uri->as_string eq "#"
ABCresult
1XX1
01X1
0011
0000
40  
41 79 my $ref_obj = $self->get_schema($ref_uri, $opts);
42  
43 77 ▶ if ( ref $ref_obj eq 'HASH' && exists $ref_obj->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}) 5 72 77
Condition66%2/3
ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}
!ll&&!rl&&r
0725
MC/DC50%1/2
ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}
ref $ref_obj eq "HASH" exists $ref_obj->{"\$ref"}
Truth table66%2/3
A and B
A= ref $ref_obj eq "HASH"B= exists $ref_obj->{"\$ref"}
ABresult
0X0
100
111
44 5 $self->resolve($ref_obj, $opts);
45 }
46  
47 77 %$ref = %$ref_obj;
48  
49 ### TODO: Does this weaken have means?
50 77 weaken($ref_obj);
51  
52 77 $ref->{id} = $ref_uri->as_string;
53 }
54  
55 79 ▶sub get_schema {
Subroutine get_schema: called CC=9 SCAR=22.7
Pod undocumented
56 79 my ($self, $uri, $opts) = @_;
57  
58 79 my ($normalized_uri, $fragment) = $self->normalize_uri($uri);
59 79 ▶ my $schema = $self->{registered_schema_map}{$normalized_uri} || $opts->{root};
Condition100%3/3
$self->{'registered_schema_map'}{$normalized_uri} || $opts->{'root'}
l!l&&r!l&&!r
50281
MC/DC100%2/2
$self->{'registered_schema_map'}{$normalized_uri} || $opts->{'root'}
$self->{'registered_schema_map'}{$normalized_uri} $opts->{'root'}
Truth table100%3/3
A || B
A= $self->{'registered_schema_map'}{$normalized_uri}B= $opts->{'root'}
ABresult
1X1
011
000
60 79 ▶ unless (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 1 78 79
61 1 die sprintf("cannot resolve reference: uri = %s", $uri);
62 }
63  
64 78 ▶ if (exists $schema->{'$ref'} && $schema->{'$ref'} eq $normalized_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri) 0 78 78
Condition66%2/3
exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri
!ll&&!rl&&r
7260
MC/DC0%0/2
exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri
exists $schema->{"\$ref"} $schema->{"\$ref"} eq $normalized_uri
Truth table66%2/3
A and B
A= exists $schema->{"\$ref"}B= $schema->{"\$ref"} eq $normalized_uri
ABresult
0X0
100
111
65 0 die sprintf("cannot resolve reference: uri = %s", $uri);
66 }
67  
68 78 ▶ if ( $fragment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment) 69 9 78
69 69 eval {
70 69 $schema = JSON::Pointer->get($schema, $fragment, 1);
71 };
72 69 ▶ if (my $e = $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = $@) { } 1 68 69
elsif (not $schema) { } 0 68 68
73 1 die sprintf("cannot resolve reference fragment: uri = %s, msg = %s", $uri, $e);
74 }
75 elsif (!$schema) {
76 0 die sprintf("cannot resolve reference fragment: uri = %s, msg = %s", $uri);
77 }
78 }
79  
80 77 ▶ unless (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 0 77 77
81 0 die sprintf("cannot resolve reference: uri = %s", $uri);
82 }
83  
84 77 return $schema;
85 }
86  
87 9 ▶sub register_schema {
Subroutine register_schema: called CC=1 SCAR=0.0
Pod undocumented
88 9 my ($self, $uri, $schema) = @_;
89 9 my $normalized_uri = $self->normalize_uri($uri);
90 9 my $cloned_schema = clone($schema);
91  
92 ### recursive reference resolution
93 walkdepth(+{
94 410 ▶ wanted => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
95 410 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1) 28 382 410
Condition77%7/9
ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1
!ll&&!rl&&r
382028
ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"}
!ll&&!rl&&r
382028
ref $_ eq "HASH" and exists $_->{"\$ref"}
!ll&&!rl&&r
3127028
MC/DC50%2/4
ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1
ref $_ eq "HASH" exists $_->{"\$ref"} not ref $_->{"\$ref"} keys %$_ == 1
Truth table60%3/5
A and B and not(C) and D
A= ref $_ eq "HASH"B= exists $_->{"\$ref"}C= not ref $_->{"\$ref"}D= keys %$_ == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
96 ref $_ eq "HASH" &&
97 exists $_->{'$ref'} &&
98 !ref $_->{'$ref'} &&
99 keys %$_ == 1
100 ) {
101 28 my $ref_uri = URI->new($_->{'$ref'});
102 28 ▶ return if $ref_uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_uri->scheme 1 27 28
103 27 $_->{'$ref'} = $ref_uri->abs($normalized_uri)->as_string;
104 }
105 },
106 9 }, $cloned_schema);
107  
108 9 $self->{registered_schema_map}{$normalized_uri} = $cloned_schema;
109 }
110  
111 0 ▶sub unregister_schema {
Subroutine unregister_schema: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ($self, $uri) = @_;
113 0 my $normalized_uri = $self->normalize_uri($uri);
114 0 delete $self->{registered_schema_map}{$normalized_uri};
115 }
116  
117 88 ▶sub normalize_uri {
Subroutine normalize_uri: called CC=2 SCAR=6.9
Pod undocumented
118 88 my ($self, $uri) = @_;
119 88 my %parts;
120  
121 88 @parts{qw/scheme authority path query fragment/} = uri_split($uri);
122 88 my $fragment = $parts{fragment};
123 88 $parts{fragment} = undef;
124  
125 88 my $normalized_uri = uri_join(@parts{qw/scheme authority path query fragment/});
126  
127 88 ▶ return wantarray ? ($normalized_uri, $fragment) : $normalized_uri;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 79 9 88
128 }
129  
130 48 1;
131  
132 __END__
« JSV/Keyword/Draft4/Properties.pm ↑ Summary JSV/Keyword/Draft4/Type.pm »