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   1086756 use strict;
  45         88  
  45         1442  
2 45     45   195 use warnings;
  45         87  
  45         3015  
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.635';
8              
9 45     45   760 use 5.020;
  45         136  
10 45     45   207 use Moo::Role;
  45         78  
  45         309  
11 45     45   17298 use strictures 2;
  45         460  
  45         2188  
12 45     45   16653 use stable 0.031 'postderef';
  45         828  
  45         334  
13 45     45   8338 use experimental 'signatures';
  45         141  
  45         197  
14 45     45   2418 no autovivification warn => qw(fetch store exists delete);
  45         74  
  45         364  
15 45     45   2972 use if "$]" >= 5.022, experimental => 're_strict';
  45         67  
  45         1061  
16 45     45   3102 no if "$]" >= 5.031009, feature => 'indirect';
  45         81  
  45         2528  
17 45     45   211 no if "$]" >= 5.033001, feature => 'multidimensional';
  45         89  
  45         2063  
18 45     45   198 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  45         70  
  45         2066  
19 45     45   185 no if "$]" >= 5.041009, feature => 'smartmatch';
  45         73  
  45         1486  
20 45     45   156 no feature 'switch';
  45         71  
  45         1099  
21 45     45   162 use Safe::Isa;
  45         59  
  45         6436  
22 45     45   268 use Types::Standard qw(Str Undef InstanceOf);
  45         78  
  45         526  
23 45     45   111911 use Types::Common::Numeric 'PositiveOrZeroInt';
  45         90  
  45         296  
24 45     45   52341 use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type);
  45         138  
  45         3730  
25 45     45   305 use namespace::clean;
  45         74  
  45         154  
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 870542 sub BUILD ($self, $args) {
  14045         19492  
  14045         17304  
  14045         15650  
83 14045 100       129101 $self->{_uri} = $args->{_uri} if exists $args->{_uri};
84             }
85              
86 18623     18623 0 606383 sub TO_JSON ($self) {
  18623         25193  
  18623         23213  
87 18623         55189 my $thing = $self->__thing; # annotation or error
88              
89             return +{
90             # note that locations are JSON pointers, not uri fragments!
91 18623 100       269675 !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__