File Coverage

blib/lib/JSON/Schema/Modern/ResultNode.pm
Criterion Covered Total %
statement 66 72 91.6
branch 6 8 75.0
condition n/a
subroutine 21 22 95.4
pod 0 3 0.0
total 93 105 88.5


line stmt bran cond sub pod time code
1 45     45   1599066 use strict;
  45         154  
  45         2026  
2 45     45   311 use warnings;
  45         103  
  45         4305  
3             package JSON::Schema::Modern::ResultNode;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Common code for nodes of a JSON::Schema::Modern::Result
6              
7             our $VERSION = '0.634';
8              
9 45     45   1056 use 5.020;
  45         190  
10 45     45   285 use Moo::Role;
  45         142  
  45         396  
11 45     45   26959 use strictures 2;
  45         562  
  45         3355  
12 45     45   25698 use stable 0.031 'postderef';
  45         1109  
  45         483  
13 45     45   11621 use experimental 'signatures';
  45         128  
  45         358  
14 45     45   3654 no autovivification warn => qw(fetch store exists delete);
  45         103  
  45         442  
15 45     45   4271 use if "$]" >= 5.022, experimental => 're_strict';
  45         149  
  45         1418  
16 45     45   4704 no if "$]" >= 5.031009, feature => 'indirect';
  45         128  
  45         4353  
17 45     45   333 no if "$]" >= 5.033001, feature => 'multidimensional';
  45         89  
  45         3315  
18 45     45   298 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  45         113  
  45         3267  
19 45     45   305 no if "$]" >= 5.041009, feature => 'smartmatch';
  45         114  
  45         2278  
20 45     45   327 no feature 'switch';
  45         124  
  45         1750  
21 45     45   262 use Safe::Isa;
  45         102  
  45         9185  
22 45     45   355 use Types::Standard qw(Str Undef InstanceOf);
  45         97  
  45         693  
23 45     45   169868 use Types::Common::Numeric 'PositiveOrZeroInt';
  45         110  
  45         421  
24 45     45   75021 use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type);
  45         220  
  45         5879  
25 45     45   414 use namespace::clean;
  45         102  
  45         264  
26              
27             # not provided when Error and mode = traverse
28             has instance_location => (
29             is => 'ro',
30             isa => json_pointer_type,
31             );
32              
33             has keyword_location => (
34             is => 'ro',
35             isa => json_pointer_type,
36             required => 1,
37             );
38              
39             has absolute_keyword_location => (
40             is => 'ro',
41             isa => InstanceOf['Mojo::URL']|Undef,
42             lazy => 1,
43             default => sub ($self) {
44             # _uri contains data as populated from A() and E():
45             # [ $state->{initial_schema_uri}, $state->{keyword_path}, @extra_path ]
46             # we do the equivalent of:
47             # canonical_uri($state, @extra_path);
48             if (my $uri_bits = delete $self->{_uri}) {
49             my ($initial_schema_uri, $keyword_path, @extra_path) = @$uri_bits;
50              
51             return($initial_schema_uri eq '' && $self->{keyword_location} eq '' ? undef : $initial_schema_uri)
52             if not @extra_path and not length($keyword_path);
53              
54             my $uri = $initial_schema_uri->clone;
55             my $fragment = ($uri->fragment//'').(@extra_path ? jsonp($keyword_path, @extra_path) : $keyword_path);
56             undef $fragment if not length($fragment);
57             $uri->fragment($fragment);
58              
59             undef $uri if $uri eq '' and $self->{keyword_location} eq ''
60             or ($uri->fragment // '') eq $self->{keyword_location} and $uri->clone->fragment(undef) eq '';
61             return $uri;
62             }
63              
64             return;
65             },
66             );
67              
68             has keyword => (
69             is => 'ro',
70             isa => Str|Undef,
71             required => 1,
72             );
73              
74             has depth => (
75             is => 'ro',
76             isa => PositiveOrZeroInt,
77             required => 1,
78             );
79              
80             # TODO: maybe need to support being passed an already-blessed object
81              
82 14045     14045 0 1360653 sub BUILD ($self, $args) {
  14045         27802  
  14045         25871  
  14045         22726  
83 14045 100       210435 $self->{_uri} = $args->{_uri} if exists $args->{_uri};
84             }
85              
86 18623     18623 0 942748 sub TO_JSON ($self) {
  18623         39441  
  18623         32396  
87 18623         78198 my $thing = $self->__thing; # annotation or error
88              
89             return +{
90             # note that locations are JSON pointers, not uri fragments!
91 18623 100       462109 !defined($self->instance_location) ? () : (instanceLocation => $self->instance_location),
    100          
92             keywordLocation => $self->keyword_location,
93             !defined($self->absolute_keyword_location) ? ()
94             : (absoluteKeywordLocation => $self->absolute_keyword_location->to_string),
95             $thing => $self->$thing, # TODO: allow localization in error message
96             };
97             }
98              
99 0     0 0   sub dump ($self) {
  0            
  0            
100 0           my $encoder = JSON::Schema::Modern::_JSON_BACKEND()->new
101             ->utf8(0)
102             ->convert_blessed(1)
103             ->canonical(1)
104             ->indent(1)
105             ->space_after(1);
106 0 0         $encoder->indent_length(2) if $encoder->can('indent_length');
107 0           $encoder->encode($self);
108             }
109              
110             1;
111              
112             __END__