File Coverage

blib/lib/JSON/Schema/Modern/ResultNode.pm
Criterion Covered Total %
statement 63 69 91.3
branch 6 8 75.0
condition n/a
subroutine 20 21 95.2
pod 0 3 0.0
total 89 101 88.1


line stmt bran cond sub pod time code
1 46     46   1069797 use strict;
  46         87  
  46         1429  
2 46     46   172 use warnings;
  46         72  
  46         2932  
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.641';
8              
9 46     46   769 use 5.020;
  46         142  
10 46     46   217 use Moo::Role;
  46         75  
  46         284  
11 46     46   17388 use strictures 2;
  46         425  
  46         1535  
12 46     46   15806 use stable 0.031 'postderef';
  46         642  
  46         905  
13 46     46   8297 use experimental 'signatures';
  46         201  
  46         250  
14 46     46   2385 no autovivification warn => qw(fetch store exists delete);
  46         144  
  46         338  
15 46     46   3218 use if "$]" >= 5.022, experimental => 're_strict';
  46         89  
  46         1159  
16 46     46   3114 no if "$]" >= 5.031009, feature => 'indirect';
  46         68  
  46         2556  
17 46     46   204 no if "$]" >= 5.033001, feature => 'multidimensional';
  46         80  
  46         2187  
18 46     46   175 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  46         79  
  46         1915  
19 46     46   177 no if "$]" >= 5.041009, feature => 'smartmatch';
  46         77  
  46         1469  
20 46     46   163 no feature 'switch';
  46         81  
  46         1186  
21 46     46   154 use Types::Standard qw(Str Undef InstanceOf);
  46         86  
  46         497  
22 46     46   112689 use Types::Common::Numeric 'PositiveOrZeroInt';
  46         87  
  46         275  
23 46     46   53289 use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type);
  46         155  
  46         4124  
24 46     46   302 use namespace::clean;
  46         65  
  46         166  
25              
26             # not provided when Error and mode = traverse
27             has instance_location => (
28             is => 'ro',
29             isa => json_pointer_type,
30             );
31              
32             has keyword_location => (
33             is => 'ro',
34             isa => json_pointer_type,
35             required => 1,
36             );
37              
38             has absolute_keyword_location => (
39             is => 'ro',
40             isa => InstanceOf['Mojo::URL']|Undef,
41             lazy => 1,
42             default => sub ($self) {
43             # _uri contains data as populated from A() and E():
44             # [ $state->{initial_schema_uri}, $state->{keyword_path}, @extra_path ]
45             # we do the equivalent of:
46             # canonical_uri($state, @extra_path);
47             if (my $uri_bits = delete $self->{_uri}) {
48             my ($initial_schema_uri, $keyword_path, @extra_path) = @$uri_bits;
49              
50             return($initial_schema_uri eq '' && $self->{keyword_location} eq '' ? undef : $initial_schema_uri)
51             if not @extra_path and not length($keyword_path);
52              
53             my $uri = $initial_schema_uri->clone;
54             my $fragment = ($uri->fragment//'').(@extra_path ? jsonp($keyword_path, @extra_path) : $keyword_path);
55             undef $fragment if not length($fragment);
56             $uri->fragment($fragment);
57              
58             undef $uri if $uri eq '' and $self->{keyword_location} eq ''
59             or ($uri->fragment // '') eq $self->{keyword_location} and $uri->clone->fragment(undef) eq '';
60             return $uri;
61             }
62              
63             return;
64             },
65             );
66              
67             has keyword => (
68             is => 'ro',
69             isa => Str|Undef,
70             required => 1,
71             );
72              
73             has depth => (
74             is => 'ro',
75             isa => PositiveOrZeroInt,
76             required => 1,
77             );
78              
79             # TODO: maybe need to support being passed an already-blessed object
80              
81 14045     14045 0 903539 sub BUILD ($self, $args) {
  14045         19438  
  14045         17557  
  14045         17856  
82 14045 100       132816 $self->{_uri} = $args->{_uri} if exists $args->{_uri};
83             }
84              
85 18623     18623 0 611351 sub TO_JSON ($self) {
  18623         26159  
  18623         24551  
86 18623         56709 my $thing = $self->__thing; # annotation or error
87              
88             return +{
89             # note that locations are JSON pointers, not uri fragments!
90 18623 100       276869 !defined($self->instance_location) ? () : (instanceLocation => $self->instance_location),
    100          
91             keywordLocation => $self->keyword_location,
92             !defined($self->absolute_keyword_location) ? ()
93             : (absoluteKeywordLocation => $self->absolute_keyword_location->to_string),
94             $thing => $self->$thing, # TODO: allow localization in error message
95             };
96             }
97              
98 0     0 0   sub dump ($self) {
  0            
  0            
99 0           my $encoder = JSON::Schema::Modern::_JSON_BACKEND()->new
100             ->utf8(0)
101             ->convert_blessed(1)
102             ->canonical(1)
103             ->indent(1)
104             ->space_after(1);
105 0 0         $encoder->indent_length(2) if $encoder->can('indent_length');
106 0           $encoder->encode($self);
107             }
108              
109             1;
110              
111             __END__