File Coverage

blib/lib/YAML/PP/Schema/Core.pm
Criterion Covered Total %
statement 46 46 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 60 60 100.0


line stmt bran cond sub pod time code
1 27     27   183284 use strict;
  27         52  
  27         922  
2 27     27   113 use warnings;
  27         39  
  27         2589  
3             package YAML::PP::Schema::Core;
4              
5             our $VERSION = 'v0.39.0'; # VERSION
6              
7 27         2277 use YAML::PP::Schema::JSON qw/
8             represent_int represent_float represent_literal represent_bool
9             represent_undef
10 27     27   471 /;
  27         119  
11              
12 27     27   139 use B;
  27         41  
  27         688  
13              
14 27     27   94 use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE /;
  27         42  
  27         18544  
15              
16             my $RE_INT_CORE = qr{^([+-]?(?:[0-9]+))$};
17             my $RE_FLOAT_CORE = qr{^([+-]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][+-]?[0-9]+)?)$};
18             my $RE_INT_OCTAL = qr{^0o([0-7]+)$};
19             my $RE_INT_HEX = qr{^0x([0-9a-fA-F]+)$};
20              
21 6     6   68 sub _from_oct { oct $_[2]->[0] }
22 10     10   54 sub _from_hex { hex $_[2]->[0] }
23              
24             sub register {
25 729     729 1 2367 my ($self, %args) = @_;
26 729         1245 my $schema = $args{schema};
27              
28             $schema->add_resolver(
29             tag => 'tag:yaml.org,2002:null',
30             match => [ equals => $_ => undef ],
31 729         3114 ) for (qw/ null NULL Null ~ /, '');
32             $schema->add_resolver(
33             tag => 'tag:yaml.org,2002:bool',
34             match => [ equals => $_ => $schema->true ],
35 729         2271 ) for (qw/ true TRUE True /);
36             $schema->add_resolver(
37             tag => 'tag:yaml.org,2002:bool',
38             match => [ equals => $_ => $schema->false ],
39 729         2035 ) for (qw/ false FALSE False /);
40 729         2270 $schema->add_resolver(
41             tag => 'tag:yaml.org,2002:int',
42             match => [ regex => $RE_INT_CORE => \&YAML::PP::Schema::JSON::_to_int ],
43             );
44 729         2488 $schema->add_resolver(
45             tag => 'tag:yaml.org,2002:int',
46             match => [ regex => $RE_INT_OCTAL => \&_from_oct ],
47             );
48 729         2254 $schema->add_resolver(
49             tag => 'tag:yaml.org,2002:int',
50             match => [ regex => $RE_INT_HEX => \&_from_hex ],
51             );
52 729         2307 $schema->add_resolver(
53             tag => 'tag:yaml.org,2002:float',
54             match => [ regex => $RE_FLOAT_CORE => \&YAML::PP::Schema::JSON::_to_float ],
55             );
56             $schema->add_resolver(
57             tag => 'tag:yaml.org,2002:float',
58             match => [ equals => $_ => 0 + "inf" ],
59 729         2442 ) for (qw/ .inf .Inf .INF +.inf +.Inf +.INF /);
60             $schema->add_resolver(
61             tag => 'tag:yaml.org,2002:float',
62             match => [ equals => $_ => 0 - "inf" ],
63 729         2125 ) for (qw/ -.inf -.Inf -.INF /);
64             $schema->add_resolver(
65             tag => 'tag:yaml.org,2002:float',
66             match => [ equals => $_ => 0 + "nan" ],
67 729         1867 ) for (qw/ .nan .NaN .NAN /);
68             $schema->add_resolver(
69             tag => 'tag:yaml.org,2002:str',
70 729     3330   3685 match => [ all => sub { $_[1]->{value} } ],
  3330         9176  
71             );
72              
73 729         1374 my $int_flags = B::SVp_IOK;
74 729         986 my $float_flags = B::SVp_NOK;
75 729         2627 $schema->add_representer(
76             flags => $int_flags,
77             code => \&represent_int,
78             );
79 729         1918 $schema->add_representer(
80             flags => $float_flags,
81             code => \&represent_float,
82             );
83 729         1720 $schema->add_representer(
84             undefined => \&represent_undef,
85             );
86             $schema->add_representer(
87             equals => $_,
88             code => \&represent_literal,
89 729         2540 ) for ("", qw/
90             true TRUE True false FALSE False null NULL Null ~
91             .inf .Inf .INF +.inf +.Inf +.INF -.inf -.Inf -.INF .nan .NaN .NAN
92             /);
93 729         8732 $schema->add_representer(
94             regex => qr{$RE_INT_CORE|$RE_FLOAT_CORE|$RE_INT_OCTAL|$RE_INT_HEX},
95             code => \&represent_literal,
96             );
97              
98 729 100       1623 if ($schema->bool_class) {
99 725         881 for my $class (@{ $schema->bool_class }) {
  725         1211  
100 725 100       1484 if ($class eq 'perl') {
101 135         314 $schema->add_representer(
102             bool => 1,
103             code => \&represent_bool,
104             );
105 135         240 next;
106             }
107             $schema->add_representer(
108 590         1232 class_equals => $class,
109             code => \&represent_bool,
110             );
111             }
112             }
113              
114 729         2732 return;
115             }
116              
117             1;
118              
119             __END__
120              
121             =pod
122              
123             =encoding utf-8
124              
125             =head1 NAME
126              
127             YAML::PP::Schema::Core - YAML 1.2 Core Schema
128              
129             =head1 SYNOPSIS
130              
131             my $yp = YAML::PP->new( schema => ['Core'] );
132              
133             =head1 DESCRIPTION
134              
135             This schema is the official recommended Core Schema for YAML 1.2.
136             It loads additional values to the JSON schema as special types, for
137             example C<TRUE> and C<True> additional to C<true>.
138              
139             Official Schema:
140             L<https://yaml.org/spec/1.2/spec.html#id2804923>
141              
142             Here you can see all Schemas and examples implemented by YAML::PP:
143             L<https://perlpunk.github.io/YAML-PP-p5/schemas.html>
144              
145             =head1 METHODS
146              
147             =over
148              
149             =item register
150              
151             Called by YAML::PP::Schema
152              
153             =back
154              
155             =cut