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   1122014 use strict;
  45         97  
  45         1495  
2 45     45   172 use warnings;
  45         100  
  45         2984  
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.637';
8              
9 45     45   722 use 5.020;
  45         152  
10 45     45   212 use Moo::Role;
  45         76  
  45         306  
11 45     45   17667 use strictures 2;
  45         397  
  45         2231  
12 45     45   17412 use stable 0.031 'postderef';
  45         865  
  45         408  
13 45     45   8079 use experimental 'signatures';
  45         85  
  45         205  
14 45     45   2376 no autovivification warn => qw(fetch store exists delete);
  45         79  
  45         326  
15 45     45   3086 use if "$]" >= 5.022, experimental => 're_strict';
  45         68  
  45         1155  
16 45     45   3293 no if "$]" >= 5.031009, feature => 'indirect';
  45         108  
  45         2763  
17 45     45   210 no if "$]" >= 5.033001, feature => 'multidimensional';
  45         82  
  45         2239  
18 45     45   208 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  45         80  
  45         2239  
19 45     45   207 no if "$]" >= 5.041009, feature => 'smartmatch';
  45         89  
  45         1660  
20 45     45   183 no feature 'switch';
  45         82  
  45         1132  
21 45     45   175 use Safe::Isa;
  45         69  
  45         6728  
22 45     45   249 use Types::Standard qw(Str Undef InstanceOf);
  45         71  
  45         534  
23 45     45   111163 use Types::Common::Numeric 'PositiveOrZeroInt';
  45         85  
  45         293  
24 45     45   52378 use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type);
  45         160  
  45         4140  
25 45     45   309 use namespace::clean;
  45         73  
  45         170  
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 912555 sub BUILD ($self, $args) {
  14045         20018  
  14045         17410  
  14045         16991  
83 14045 100       139813 $self->{_uri} = $args->{_uri} if exists $args->{_uri};
84             }
85              
86 18623     18623 0 628700 sub TO_JSON ($self) {
  18623         27792  
  18623         23714  
87 18623         57313 my $thing = $self->__thing; # annotation or error
88              
89             return +{
90             # note that locations are JSON pointers, not uri fragments!
91 18623 100       285239 !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__