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   1581907 use strict;
  45         117  
  45         2045  
2 45     45   270 use warnings;
  45         107  
  45         4047  
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.632';
8              
9 45     45   1018 use 5.020;
  45         224  
10 45     45   326 use Moo::Role;
  45         148  
  45         442  
11 45     45   26786 use strictures 2;
  45         511  
  45         2822  
12 45     45   25306 use stable 0.031 'postderef';
  45         1286  
  45         611  
13 45     45   11576 use experimental 'signatures';
  45         124  
  45         256  
14 45     45   3437 no autovivification warn => qw(fetch store exists delete);
  45         131  
  45         448  
15 45     45   4436 use if "$]" >= 5.022, experimental => 're_strict';
  45         115  
  45         1327  
16 45     45   5678 no if "$]" >= 5.031009, feature => 'indirect';
  45         138  
  45         3777  
17 45     45   309 no if "$]" >= 5.033001, feature => 'multidimensional';
  45         94  
  45         3481  
18 45     45   297 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  45         90  
  45         3085  
19 45     45   290 no if "$]" >= 5.041009, feature => 'smartmatch';
  45         100  
  45         2498  
20 45     45   326 no feature 'switch';
  45         151  
  45         1754  
21 45     45   261 use Safe::Isa;
  45         103  
  45         8701  
22 45     45   359 use Types::Standard qw(Str Undef InstanceOf);
  45         114  
  45         761  
23 45     45   162183 use Types::Common::Numeric 'PositiveOrZeroInt';
  45         140  
  45         484  
24 45     45   75385 use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type);
  45         209  
  45         5390  
25 45     45   380 use namespace::clean;
  45         88  
  45         246  
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 14027     14027 0 1366741 sub BUILD ($self, $args) {
  14027         28798  
  14027         26476  
  14027         23873  
83 14027 100       216683 $self->{_uri} = $args->{_uri} if exists $args->{_uri};
84             }
85              
86 18596     18596 0 923797 sub TO_JSON ($self) {
  18596         41245  
  18596         31627  
87 18596         78344 my $thing = $self->__thing; # annotation or error
88              
89             return +{
90             # note that locations are JSON pointers, not uri fragments!
91 18596 100       468471 !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__