| 1 |
use strict; |
| 2 |
use warnings; |
| 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.650'; |
| 8 |
|
| 9 |
use 5.020; |
| 10 |
use Moo::Role; |
| 11 |
use strictures 2; |
| 12 |
use stable 0.031 'postderef'; |
| 13 |
use experimental 'signatures'; |
| 14 |
no autovivification warn => qw(fetch store exists delete); |
| 15 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 16 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 17 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 18 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 19 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 20 |
no feature 'switch'; |
| 21 |
use Types::Standard qw(Str Undef InstanceOf); |
| 22 |
use Types::Common::Numeric 'PositiveOrZeroInt'; |
| 23 |
use JSON::Schema::Modern::Utilities qw(jsonp json_pointer_type); |
| 24 |
use namespace::clean; |
| 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 |
sub BUILD ($self, $args) { |
| 82 |
$self->{_uri} = $args->{_uri} if exists $args->{_uri}; |
| 83 |
} |
| 84 |
|
| 85 |
sub TO_JSON ($self) { |
| 86 |
my $thing = $self->__thing; # annotation or error |
| 87 |
|
| 88 |
return +{ |
| 89 |
# note that locations are JSON pointers, not uri fragments! |
| 90 |
!defined($self->instance_location) ? () : (instanceLocation => $self->instance_location), |
| 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 |
sub dump ($self) { |
| 99 |
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 |
$encoder->indent_length(2) if $encoder->can('indent_length'); |
| 106 |
$encoder->encode($self); |
| 107 |
} |
| 108 |
|
| 109 |
1; |
| 110 |
|
| 111 |
__END__ |
| 112 |
|
| 113 |
=pod |
| 114 |
|
| 115 |
=encoding UTF-8 |
| 116 |
|
| 117 |
=head1 NAME |
| 118 |
|
| 119 |
JSON::Schema::Modern::ResultNode - Common code for nodes of a JSON::Schema::Modern::Result |
| 120 |
|
| 121 |
=head1 VERSION |
| 122 |
|
| 123 |
version 0.650 |
| 124 |
|
| 125 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 126 |
presumed or inferred from the version being less than 1.0. |
| 127 |
|
| 128 |
=head1 SYNOPSIS |
| 129 |
|
| 130 |
use Moo; |
| 131 |
with JSON::Schema::Modern::ResultNode; |
| 132 |
|
| 133 |
=head1 DESCRIPTION |
| 134 |
|
| 135 |
This module is for internal use only. |
| 136 |
|
| 137 |
=for Pod::Coverage BUILD TO_JSON absolute_keyword_location depth dump instance_location keyword keyword_location |
| 138 |
|
| 139 |
=head1 GIVING THANKS |
| 140 |
|
| 141 |
=for stopwords MetaCPAN GitHub |
| 142 |
|
| 143 |
If you found this module to be useful, please show your appreciation by |
| 144 |
adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern> |
| 145 |
and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>. |
| 146 |
|
| 147 |
=head1 SUPPORT |
| 148 |
|
| 149 |
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
| 150 |
|
| 151 |
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
| 152 |
|
| 153 |
=for stopwords OpenAPI |
| 154 |
|
| 155 |
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
| 156 |
server|https://open-api.slack.com>, which are also great resources for finding help. |
| 157 |
|
| 158 |
=head1 AUTHOR |
| 159 |
|
| 160 |
Karen Etheridge <[email protected]> |
| 161 |
|
| 162 |
=head1 COPYRIGHT AND LICENCE |
| 163 |
|
| 164 |
This software is copyright (c) 2020 by Karen Etheridge. |
| 165 |
|
| 166 |
This is free software; you can redistribute it and/or modify it under |
| 167 |
the same terms as the Perl 5 programming language system itself. |
| 168 |
|
| 169 |
Some schema files have their own licence, in share/LICENSE. |
| 170 |
|
| 171 |
=cut |