blib/lib/YAML/PP/Schema/JSON.pm

filter: Statementstmt 100% 93 / 93 Branchbran 90.0% 18 / 20 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 97.1% 138 / 142 CC 16 SCAR 14.5
CC
16
Cov
97%
register
9.0
represent_float
5.0
represent_literal
2.0
CRAP 4.3
« blib/lib/YAML/PP/Exception.pm ↑ Summary blib/lib/YAML/PP/Loader.pm »
1 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package YAML::PP::Schema::JSON;
4  
5 51 our $VERSION = 'v0.43.0'; # VERSION
6  
7 51 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 our @EXPORT_OK = qw/
9 represent_int represent_float represent_literal represent_bool
10 represent_undef
11 /;
12  
13 51 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 51 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 51 ▶use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 51 my $RE_INT = qr{^(-?(?:0|[1-9][0-9]*))$};
19 51 my $RE_FLOAT = qr{^(-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)$};
20  
21 9703 ▶sub _to_int { 0 + $_[2]->[0] }
Subroutine _to_int: called CC=1 SCAR=0.0
22  
23 # DaTa++ && shmem++
24 76 ▶sub _to_float { unpack F => pack F => $_[2]->[0] }
Subroutine _to_float: called CC=1 SCAR=0.0
25  
26 22 ▶sub register {
Subroutine register: called CC=9 SCAR=22.0
Pod documented
27 22 my ($self, %args) = @_;
28 22 my $schema = $args{schema};
29 22 my $options = $args{options};
30 22 my $empty_null = 0;
31 22 for my $opt (@$options) {
32 2 ▶ if ($opt eq 'empty=str') {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt eq 'empty=str') { } 0 2 2
elsif ($opt eq 'empty=null') { } 1 1 2
33 }
34 elsif ($opt eq 'empty=null') {
35 1 $empty_null = 1;
36 }
37 else {
38 1 croak "Invalid option for JSON Schema: '$opt'";
39 }
40 }
41  
42 $schema->add_resolver(
43 21 tag => 'tag:yaml.org,2002:null',
44 match => [ equals => null => undef ],
45 );
46 21 ▶ if ($empty_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_null) { } 1 20 21
47 1 $schema->add_resolver(
48 tag => 'tag:yaml.org,2002:null',
49 match => [ equals => '' => undef ],
50 implicit => 1,
51 );
52 }
53 else {
54 20 $schema->add_resolver(
55 tag => 'tag:yaml.org,2002:str',
56 match => [ equals => '' => '' ],
57 implicit => 1,
58 );
59 }
60 21 $schema->add_resolver(
61 tag => 'tag:yaml.org,2002:bool',
62 match => [ equals => true => $schema->true ],
63 );
64 21 $schema->add_resolver(
65 tag => 'tag:yaml.org,2002:bool',
66 match => [ equals => false => $schema->false ],
67 );
68 21 $schema->add_resolver(
69 tag => 'tag:yaml.org,2002:int',
70 match => [ regex => $RE_INT => \&_to_int ],
71 );
72 21 $schema->add_resolver(
73 tag => 'tag:yaml.org,2002:float',
74 match => [ regex => $RE_FLOAT => \&_to_float ],
75 );
76 $schema->add_resolver(
77 tag => 'tag:yaml.org,2002:str',
78 21 ▶ match => [ all => sub { $_[1]->{value} } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
79 );
80  
81 21 $schema->add_representer(
82 undefined => \&represent_undef,
83 );
84  
85 21 my $int_flags = B::SVp_IOK;
86 21 my $float_flags = B::SVp_NOK;
87 21 $schema->add_representer(
88 flags => $int_flags,
89 code => \&represent_int,
90 );
91 21 my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' );
92 21 $schema->add_representer(
93 flags => $float_flags,
94 code => \&represent_float,
95 );
96 $schema->add_representer(
97 equals => $_,
98 code => \&represent_literal,
99 21 ) for ("", qw/ true false null /);
100 21 $schema->add_representer(
101 regex => qr{$RE_INT|$RE_FLOAT},
102 code => \&represent_literal,
103 );
104  
105 21 ▶ if ($schema->bool_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->bool_class) 21 0 21
106 21 for my $class (@{ $schema->bool_class }) {
107 21 ▶ if ($class eq 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "perl") 19 2 21
108 19 $schema->add_representer(
109 bool => 1,
110 code => \&represent_bool,
111 );
112 19 next;
113 }
114 $schema->add_representer(
115 2 class_equals => $class,
116 code => \&represent_bool,
117 );
118 }
119 }
120  
121 21 return;
122 }
123  
124 110 ▶sub represent_undef {
Subroutine represent_undef: called CC=1 SCAR=0.0
Pod documented
125 110 my ($rep, $node) = @_;
126 110 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
127 110 $node->{data} = 'null';
128 110 return 1;
129 }
130  
131 219 ▶sub represent_literal {
Subroutine represent_literal: called CC=2 SCAR=6.9
Pod documented
132 219 my ($rep, $node) = @_;
133 219 ▶ $node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE;
Condition100%2/2
$node->{"style"} ||= 2
l!l
1218
MC/DC100%1/1
$node->{"style"} ||= 2
$node->{"style"}
Truth table100%2/2
A
A= $node->{"style"}
Aresult
11
00
134 219 $node->{data} = "$node->{value}";
135 219 return 1;
136 }
137  
138  
139 386 ▶sub represent_int {
Subroutine represent_int: called CC=2 SCAR=6.9
Pod documented
140 386 my ($rep, $node) = @_;
141 386 ▶ if (int($node->{value}) ne $node->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $node->{"value"} ne $node->{"value"}) 7 379 386
142 7 return 0;
143 }
144 379 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
145 379 $node->{data} = "$node->{value}";
146 379 return 1;
147 }
148  
149 51 my %special = (
150 (0+'nan').'' => '.nan',
151 (0+'inf').'' => '.inf',
152 (0-'inf').'' => '-.inf'
153 );
154 131 ▶sub represent_float {
Subroutine represent_float: called CC=5 SCAR=16.1
Pod documented
155 131 my ($rep, $node) = @_;
156 131 ▶ if (exists $special{ $node->{value} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $special{$node->{"value"}}) 48 83 131
157 48 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
158 48 $node->{data} = $special{ $node->{value} };
159 48 return 1;
160 }
161 83 ▶ if (0.0 + $node->{value} ne $node->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $node->{"value"} ne $node->{"value"}) 4 79 83
162 4 return 0;
163 }
164 79 ▶ if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $node->{"value"} eq $node->{"value"} and not $node->{"value"} =~ /\./) 35 44 79
Condition66%2/3
int $node->{"value"} eq $node->{"value"} and not $node->{"value"} =~ /\./
!ll&&!rl&&r
44035
MC/DC50%1/2
int $node->{"value"} eq $node->{"value"} and not $node->{"value"} =~ /\./
int $node->{"value"} eq $node->{"value"} not $node->{"value"} =~ /\./
Truth table66%2/3
A and not(B)
A= int $node->{"value"} eq $node->{"value"}B= not $node->{"value"} =~ /\./
ABresult
0X0
100
111
165 35 $node->{value} .= '.0';
166 }
167 79 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
168 79 $node->{data} = "$node->{value}";
169 79 return 1;
170 }
171  
172 66 ▶sub represent_bool {
Subroutine represent_bool: called CC=2 SCAR=6.9
Pod documented
173 66 my ($rep, $node) = @_;
174 66 ▶ my $string = $node->{value} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$node->{'value'} ? : 33 33 66
175 66 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
176 66 @{ $node->{items} } = $string;
177 66 $node->{data} = $string;
178 66 return 1;
179 }
180  
181 51 1;
182  
183 __END__
« blib/lib/YAML/PP/Exception.pm ↑ Summary blib/lib/YAML/PP/Loader.pm »