| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 79 | ▶ | die '$ref not found' unless exists $ref->{'$ref'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 79 | my $ref_uri = URI->new($ref->{'$ref'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 79 | ▶ | if ( ! $ref_uri->scheme && $opts->{base_uri} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $ref_uri->scheme and $opts->{"base_uri"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$ref_uri->scheme or $ref_uri->fragment or $ref_uri->as_string eq "#"
$ref_uri->scheme or $ref_uri->fragment
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 "#"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $ref_obj eq "HASH" and exists $ref_obj->{"\$ref"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 79 | ▶ | unless (ref $schema eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $schema->{"\$ref"} and $schema->{"\$ref"} eq $normalized_uri
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | die sprintf("cannot resolve reference: uri = %s", $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 78 | ▶ | if ( $fragment ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 69 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 69 | $schema = JSON::Pointer->get($schema, $fragment, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 69 | ▶ | if (my $e = $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition77%7/9
ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"} and keys %$_ == 1
ref $_ eq "HASH" and exists $_->{"\$ref"} and not ref $_->{"\$ref"}
ref $_ eq "HASH" and exists $_->{"\$ref"}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 48 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||