blib/lib/JSV/Reference.pm

filter: Statementstmt 82.7% 67 / 81 Branchbran 46.6% 14 / 30 Conditioncond 26.6% 8 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 6 totaltotal 58.1% 104 / 179 CC 26 SCAR 26.9
CC
26
Cov
63%
__ANON__
37.1
resolve
17.9
get_schema
16.0
CRAP 14.7
« blib/lib/JSV/Context.pm ↑ Summary blib/lib/JSV/Keyword/Draft4/Properties.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 sub new {
17 62 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
18 62 ▶ my $args = ref $_[0] ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 62 62
19  
20 62 %$args = (
21 registered_schema_map => {},
22 max_recursion => 10,
23 %$args,
24 );
25  
26 62 bless $args => $class;
27 }
28  
29 sub resolve {
30 6 ▶ my ($self, $ref, $opts) = @_;
Subroutine resolve: called CC=10 SCAR=28.8
Pod undocumented
31 6 ▶ die 'ref value should be hash' unless ref $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "HASH" 0 6 6
32 6 ▶ die '$ref not found' unless exists $ref->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ref->{"\$ref"} 0 6 6
33 6 my $ref_uri = URI->new($ref->{'$ref'});
34  
35 6 ▶ if ( ! $ref_uri->scheme && $opts->{base_uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ref_uri->scheme and $opts->{"base_uri"}) 0 6 6
Condition33%1/3
not $ref_uri->scheme and $opts->{"base_uri"}
!ll&&!rl&&r
060
MC/DC0%0/2
not $ref_uri->scheme and $opts->{"base_uri"}
not $ref_uri->scheme $opts->{"base_uri"}
Truth table33%1/3
not(A) and B
A= not $ref_uri->schemeB= $opts->{"base_uri"}
ABresult
0X0
100
111
36 0 $ref_uri = $ref_uri->abs($opts->{base_uri});
37 }
38
39 6 ▶ 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 6 6
Condition33%2/6
$ref_uri->scheme or $ref_uri->fragment or $ref_uri->as_string eq "#"
l!l&&r!l&&!r
600
$ref_uri->scheme or $ref_uri->fragment
l!l&&r!l&&!r
060
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 table25%1/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 6 my $ref_obj = $self->get_schema($ref_uri, $opts);
42  
43 6 ▶ 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"}) 0 6 6
Condition33%1/3
ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}
!ll&&!rl&&r
060
MC/DC0%0/2
ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}
ref $ref_obj eq "HASH" exists $ref_obj->{"\$ref"}
Truth table33%1/3
A and B
A= ref $ref_obj eq "HASH"B= exists $ref_obj->{"\$ref"}
ABresult
0X0
100
111
44 0 $self->resolve($ref_obj, $opts);
45 }
46  
47 6 %$ref = %$ref_obj;
48  
49 ### TODO: Does this weaken have means?
50 6 weaken($ref_obj);
51  
52 6 $ref->{id} = $ref_uri->as_string;
53 }
54  
55 sub get_schema {
56 6 ▶ my ($self, $uri, $opts) = @_;
Subroutine get_schema: called CC=9 SCAR=27.7
Pod undocumented
57  
58 6 my ($normalized_uri, $fragment) = $self->normalize_uri($uri);
59 6 ▶ my $schema = $self->{registered_schema_map}{$normalized_uri} || $opts->{root};
Condition33%1/3
$self->{'registered_schema_map'}{$normalized_uri} || $opts->{'root'}
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{'registered_schema_map'}{$normalized_uri} || $opts->{'root'}
$self->{'registered_schema_map'}{$normalized_uri} $opts->{'root'}
Truth table33%1/3
A || B
A= $self->{'registered_schema_map'}{$normalized_uri}B= $opts->{'root'}
ABresult
1X1
011
000
60 6 ▶ unless (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 0 6 6
61 0 die sprintf("cannot resolve reference: uri = %s", $uri);
62 }
63  
64 6 ▶ if (exists $schema->{'$ref'} && $schema->{'$ref'} eq $normalized_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri) 0 6 6
Condition33%1/3
exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri
!ll&&!rl&&r
600
MC/DC0%0/2
exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri
exists $schema->{"\$ref"} $schema->{"\$ref"} eq $normalized_uri
Truth table33%1/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 6 ▶ if ( $fragment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment) 6 0 6
69 6 eval {
70 6 $schema = JSON::Pointer->get($schema, $fragment, 1);
71 };
72 6 ▶ if (my $e = $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = $@) { } 0 6 6
elsif (not $schema) { } 0 6 6
73 0 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 6 ▶ unless (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 0 6 6
81 0 die sprintf("cannot resolve reference: uri = %s", $uri);
82 }
83  
84 6 return $schema;
85 }
86  
87 sub register_schema {
88 6 ▶ my ($self, $uri, $schema) = @_;
Subroutine register_schema: called CC=1 SCAR=3.2
Pod undocumented
89 6 my $normalized_uri = $self->normalize_uri($uri);
90 6 my $cloned_schema = clone($schema);
91  
92 ### recursive reference resolution
93 walkdepth(+{
94 wanted => sub {
95 6 ▶ if (
Subroutine __ANON__: called CC=7 SCAR=36.1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Data::Walk::type and $Data::Walk::type eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1) 0 0 0
Condition16%2/12
defined $Data::Walk::type and $Data::Walk::type eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1
!ll&&!rl&&r
000
defined $Data::Walk::type and $Data::Walk::type eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"}
!ll&&!rl&&r
000
defined $Data::Walk::type and $Data::Walk::type eq "HASH" and exists $_->{"\$ref"}
!ll&&!rl&&r
060
defined $Data::Walk::type and $Data::Walk::type eq "HASH"
!ll&&!rl&&r
006
MC/DC0%0/5
defined $Data::Walk::type and $Data::Walk::type eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1
defined $Data::Walk::type $Data::Walk::type eq "HASH" exists $_->{"\$ref"} not ref $_->{"\$ref"} keys %$_ == 1
Truth table0%0/6
A and B and C and not(D) and E
A= defined $Data::Walk::typeB= $Data::Walk::type eq "HASH"C= exists $_->{"\$ref"}D= not ref $_->{"\$ref"}E= keys %$_ == 1
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
96 defined $Data::Walk::type &&
97 $Data::Walk::type eq "HASH" &&
98 exists $_->{'$ref'} &&
99 !ref $_->{'$ref'} &&
100 keys %$_ == 1
101 ) {
102 0 my $ref_uri = URI->new($_->{'$ref'});
103 0 ▶ return if $ref_uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_uri->scheme 0 0 0
104 0 $_->{'$ref'} = $ref_uri->abs($normalized_uri)->as_string;
105 }
106 },
107 6 }, $cloned_schema);
108  
109 0 $self->{registered_schema_map}{$normalized_uri} = $cloned_schema;
110 }
111  
112 sub unregister_schema {
113 0 ▶ my ($self, $uri) = @_;
Subroutine unregister_schema: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $normalized_uri = $self->normalize_uri($uri);
115 0 delete $self->{registered_schema_map}{$normalized_uri};
116 }
117  
118 sub normalize_uri {
119 12 ▶ my ($self, $uri) = @_;
Subroutine normalize_uri: called CC=2 SCAR=6.9
Pod undocumented
120 12 my %parts;
121  
122 12 @parts{qw/scheme authority path query fragment/} = uri_split($uri);
123 12 my $fragment = $parts{fragment};
124 12 $parts{fragment} = undef;
125  
126 12 my $normalized_uri = uri_join(@parts{qw/scheme authority path query fragment/});
127  
128 12 ▶ return wantarray ? ($normalized_uri, $fragment) : $normalized_uri;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 6 12
129 }
130  
131 48 1;
132  
133 __END__
« blib/lib/JSV/Context.pm ↑ Summary blib/lib/JSV/Keyword/Draft4/Properties.pm »