blib/lib/JSON/Schema/Fit.pm

filter: Statementstmt 95.5% 128 / 134 Branchbran 86.6% 52 / 60 Conditioncond 80.0% 36 / 45 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 96.9% 32 / 33 Podpod 100% 10 / 10 totaltotal 88.6% 272 / 307 CC 51 SCAR 19.0
CC
51
Cov
90%
_get_adjusted_object
17.2
_get_adjusted_number
10.0
get_adjusted
9.0
CRAP 6.7
↑ Summary
1 package JSON::Schema::Fit;
2 2 $JSON::Schema::Fit::VERSION = '0.07';
3 # ABSTRACT: adjust data structure according to json-schema
4  
5  
6 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use List::Util qw/first none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Math::Round qw/round nearest/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21  
22 13 ▶sub booleans { return _attr('booleans', @_); }
Subroutine booleans: called CC=1 SCAR=0.0
Pod documented
23  
24  
25 52 ▶sub numbers { return _attr('numbers', @_); }
Subroutine numbers: called CC=1 SCAR=0.0
Pod documented
26  
27  
28  
29 31 ▶sub round_numbers { return _attr('round_numbers', @_); }
Subroutine round_numbers: called CC=1 SCAR=0.0
Pod documented
30  
31  
32 31 ▶sub clamp_numbers { return _attr('clamp_numbers', @_); }
Subroutine clamp_numbers: called CC=1 SCAR=0.0
Pod documented
33  
34  
35  
36 21 ▶sub strings { return _attr('strings', @_); }
Subroutine strings: called CC=1 SCAR=0.0
Pod documented
37  
38  
39 15 ▶sub hash_keys { return _attr('hash_keys', @_); }
Subroutine hash_keys: called CC=1 SCAR=0.0
Pod documented
40  
41  
42  
43 17 ▶sub fill_defaults { return _attr('fill_defaults', @_); }
Subroutine fill_defaults: called CC=1 SCAR=0.0
Pod documented
44  
45  
46 16 ▶sub replace_invalid_values { return _attr('replace_invalid_values', @_); }
Subroutine replace_invalid_values: called CC=1 SCAR=0.0
Pod documented
47  
48  
49  
50 # Store valid options as well as default values
51 my %valid_option =(
52 10 ( map { ($_ => 1) } qw!booleans numbers round_numbers strings hash_keys! ),
53 2 ( map { ($_ => 0) } qw!clamp_numbers fill_defaults replace_invalid_values! ),
54 );
55  
56 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
57 12 my ($class, %opts) = @_;
58 12 my $self = bless {}, $class;
59 12 for my $k (keys %opts) {
60 19 ▶ next unless exists $valid_option{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_option{$k} 0 19 19
61 19 _attr($k, $self, $opts{$k});
62 }
63 12 return $self
64 }
65  
66 215 ▶sub _attr {
Subroutine _attr: called CC=3 SCAR=11.0
67 215 my $attr = shift;
68 215 my $self = shift;
69  
70 215 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 19 196 215
71 19 return $self->{$attr} = shift;
72 } else {
73 196 ▶ return $self->{$attr} //= $valid_option{$attr};
Condition66%2/3
$self->{$attr} //= $valid_option{$attr}
l!l&&r!l&&!r
122740
MC/DC0%0/2
$self->{$attr} //= $valid_option{$attr}
$self->{$attr} $valid_option{$attr}
Truth table66%2/3
A //= B
A= $self->{$attr}B= $valid_option{$attr}
ABresult
1X1
011
000
74 }
75 }
76  
77  
78 96 ▶sub get_adjusted {
Subroutine get_adjusted: called CC=9 SCAR=22.0
Pod documented
79 96 my ($self, $struc, $schema, $jpath) = @_;
80  
81 96 ▶ return $struc if !ref $schema || reftype $schema ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $schema or builtin::reftype($schema) ne "HASH" 3 93 96
Condition66%2/3
not ref $schema or builtin::reftype($schema) ne "HASH"
l!l&&r!l&&!r
3093
MC/DC50%1/2
not ref $schema or builtin::reftype($schema) ne "HASH"
not ref $schema builtin::reftype($schema) ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $schemaB= builtin::reftype($schema) ne "HASH"
ABresult
1X1
011
000
82  
83 93 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"default"} and !defined($struc) && $self->fill_defaults || $self->replace_invalid_values && !$self->_is_valid($struc, $schema)) 6 87 93
Condition91%11/12
exists $schema->{"default"} and !defined($struc) && $self->fill_defaults || $self->replace_invalid_values && !$self->_is_valid($struc, $schema)
!ll&&!rl&&r
75126
not defined $struc and $self->fill_defaults or $self->replace_invalid_values and not $self->_is_valid($struc, $schema)
l!l&&r!l&&!r
2412
not defined $struc and $self->fill_defaults
!ll&&!rl&&r
1602
$self->replace_invalid_values and not $self->_is_valid($struc, $schema)
!ll&&!rl&&r
934
MC/DC80%4/5
exists $schema->{"default"} and !defined($struc) && $self->fill_defaults || $self->replace_invalid_values && !$self->_is_valid($struc, $schema)
exists $schema->{"default"} not defined $struc $self->fill_defaults $self->replace_invalid_values not $self->_is_valid($struc, $schema)
Truth table62%5/8
A and not(B) and C or D and not(E)
A= exists $schema->{"default"}B= not defined $strucC= $self->fill_defaultsD= $self->replace_invalid_valuesE= not $self->_is_valid($struc, $schema)
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
84 exists $schema->{default} && (
85 !defined $struc && $self->fill_defaults ||
86 $self->replace_invalid_values && !$self->_is_valid($struc, $schema)
87 )
88 ) {
89 6 return $self->_default_value($schema);
90 }
91  
92 87 my $method = $self->_adjuster_by_type($schema->{type});
93 87 ▶ return $struc if !$method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 2 85 87
94 85 return $self->$method($struc, $schema, $jpath);
95 }
96  
97  
98 7 ▶sub _is_valid {
Subroutine _is_valid: called CC=3 SCAR=11.3
99 7 my ($self, $struc, $schema) = @_;
100  
101 7 ▶ if (my $enum = $schema->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enum = $schema->{"enum"}) 6 1 7
102 6 ▶ return '' if none {my $v = $_; $struc eq $v} @$enum;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { my $v = $_; $struc eq $v; } , @$enum) 4 2 6
103 }
104  
105 3 return 1;
106  
107  
108 0 return grep { Compare($struc, $_) } @{$schema->{enum}};
109 }
110  
111  
112 9 ▶sub _default_value {
Subroutine _default_value: called CC=3 SCAR=11.4
113 9 my ($self, $schema) = @_;
114 9 ▶ return if !exists $schema->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"default"} 0 9 9
115  
116 9 my $default = $schema->{default};
117 9 ▶ return ref $default ? Storable::dclone($default) : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 0 9 9
118 }
119  
120  
121 87 ▶sub _adjuster_by_type {
Subroutine _adjuster_by_type: called CC=3 SCAR=11.3
122 87 my ($self, $type) = @_;
123  
124 87 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 2 85 87
125 85 my $method = "_get_adjusted_$type";
126 85 ▶ return $method if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 85 0 85
127 0 return;
128 }
129  
130  
131 13 ▶sub _get_adjusted_boolean {
Subroutine _get_adjusted_boolean: called CC=3 SCAR=11.0
132 13 my ($self, $struc, $schema, $jpath) = @_;
133  
134 13 ▶ return $struc if !$self->booleans();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->booleans 2 11 13
135 11 ▶ return JSON::true if $struc;
Branch: 1 branch
BranchTrueFalseTotal
if $struc 10 1 11
136 1 return JSON::false;
137 }
138  
139  
140 19 ▶sub _get_adjusted_integer {
Subroutine _get_adjusted_integer: called CC=2 SCAR=6.9
141 19 my ($self, $struc, $schema, $jpath) = @_;
142  
143 19 ▶ return $struc if !$self->numbers();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->numbers 2 17 19
144 17 my $result = $self->_get_adjusted_number($struc, $schema, $jpath);
145 17 return round($result);
146 }
147  
148  
149 33 ▶sub _get_adjusted_number {
Subroutine _get_adjusted_number: called CC=10 SCAR=23.0
150 33 my ($self, $struc, $schema, $jpath) = @_;
151  
152 33 ▶ return $struc if !$self->numbers();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->numbers 2 31 33
153 31 my $result = 0+$struc;
154  
155 31 ▶ if ($self->round_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->round_numbers) 29 2 31
156 29 ▶ my $quantum = $schema->{multipleOf} || $schema->{divisibleBy};
Condition66%2/3
$schema->{'multipleOf'} || $schema->{'divisibleBy'}
l!l&&r!l&&!r
19010
MC/DC50%1/2
$schema->{'multipleOf'} || $schema->{'divisibleBy'}
$schema->{'multipleOf'} $schema->{'divisibleBy'}
Truth table66%2/3
A || B
A= $schema->{'multipleOf'}B= $schema->{'divisibleBy'}
ABresult
1X1
011
000
157 29 ▶ $result = nearest $quantum, $result if $quantum;
Branch: 1 branch
BranchTrueFalseTotal
if $quantum 19 10 29
158 }
159  
160 31 ▶ if ($self->clamp_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clamp_numbers) 17 14 31
161 17 ▶ $result = $schema->{maximum} if exists $schema->{maximum} and $result > $schema->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"maximum"} and $result > $schema->{"maximum"} 3 14 17
Condition100%3/3
exists $schema->{"maximum"} and $result > $schema->{"maximum"}
!ll&&!rl&&r
1223
MC/DC100%2/2
exists $schema->{"maximum"} and $result > $schema->{"maximum"}
exists $schema->{"maximum"} $result > $schema->{"maximum"}
Truth table100%3/3
A and B
A= exists $schema->{"maximum"}B= $result > $schema->{"maximum"}
ABresult
0X0
100
111
162 17 ▶ $result = $schema->{minimum} if exists $schema->{minimum} and $result < $schema->{minimum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"minimum"} and $result < $schema->{"minimum"} 1 16 17
Condition100%3/3
exists $schema->{"minimum"} and $result < $schema->{"minimum"}
!ll&&!rl&&r
1421
MC/DC100%2/2
exists $schema->{"minimum"} and $result < $schema->{"minimum"}
exists $schema->{"minimum"} $result < $schema->{"minimum"}
Truth table100%3/3
A and B
A= exists $schema->{"minimum"}B= $result < $schema->{"minimum"}
ABresult
0X0
100
111
163 }
164  
165 31 return $result;
166 }
167  
168  
169 21 ▶sub _get_adjusted_string {
Subroutine _get_adjusted_string: called CC=2 SCAR=6.9
170 21 my ($self, $struc, $schema, $jpath) = @_;
171  
172 21 ▶ return $struc if !$self->strings();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->strings 4 17 21
173 17 return "$struc";
174 }
175  
176  
177 1 ▶sub _get_adjusted_array {
Subroutine _get_adjusted_array: called CC=3 SCAR=11.0
178 1 my ($self, $struc, $schema, $jpath) = @_;
179  
180 1 ▶ croak "Structure is not ARRAY at $jpath" if reftype $struc ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($struc) ne "ARRAY" 0 1 1
181  
182 1 my $result = [];
183 1 for my $i ( 0 .. $#$struc ) {
184 3 push @$result, $self->get_adjusted($struc->[$i], $schema->{items}, $self->_jpath($jpath, $i));
185 }
186  
187 1 return $result;
188 }
189  
190 15 ▶sub _get_adjusted_object {
Subroutine _get_adjusted_object: called CC=16 SCAR=28.4
191 15 my ($self, $struc, $schema, $jpath) = @_;
192  
193 15 ▶ croak "Structure is not HASH at $jpath" if reftype $struc ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($struc) ne "HASH" 0 15 15
194  
195 15 my $result = {};
196 15 my $keys_re;
197  
198 15 ▶ my $properties = $schema->{properties} || {};
Condition50%1/2
$schema->{'properties'} || {}
l!l
150
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table50%1/2
A
A= $schema->{'properties'}
Aresult
11
00
199 15 ▶ my $p_properties = $schema->{patternProperties} || {};
Condition50%1/2
$schema->{'patternProperties'} || {}
l!l
015
MC/DC0%0/1
$schema->{'patternProperties'} || {}
$schema->{'patternProperties'}
Truth table50%1/2
A
A= $schema->{'patternProperties'}
Aresult
11
00
200  
201 15 ▶ if ($self->hash_keys() && exists $schema->{additionalProperties} && !$schema->{additionalProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hash_keys and exists $schema->{"additionalProperties"} and not $schema->{"additionalProperties"}) 10 5 15
Condition83%5/6
$self->hash_keys and exists $schema->{"additionalProperties"} and not $schema->{"additionalProperties"}
!ll&&!rl&&r
5010
$self->hash_keys and exists $schema->{"additionalProperties"}
!ll&&!rl&&r
2310
MC/DC66%2/3
$self->hash_keys and exists $schema->{"additionalProperties"} and not $schema->{"additionalProperties"}
$self->hash_keys exists $schema->{"additionalProperties"} not $schema->{"additionalProperties"}
Truth table75%3/4
A and B and not(C)
A= $self->hash_keysB= exists $schema->{"additionalProperties"}C= not $schema->{"additionalProperties"}
ABCresult
0XX0
10X0
1100
1111
202 my $keys_re_text = join q{|}, (
203 keys %$p_properties,
204 10 map {quotemeta} keys %$properties,
205 );
206 10 $keys_re = qr{^$keys_re_text$}x;
207 }
208  
209 15 for my $key (keys %$struc) {
210 71 ▶ next if $keys_re && $key !~ $keys_re;
Branch: 1 branch
BranchTrueFalseTotal
if $keys_re and not $key =~ /$keys_re/x 10 61 71
Condition100%3/3
$keys_re and not $key =~ /$keys_re/x
!ll&&!rl&&r
154610
MC/DC100%2/2
$keys_re and not $key =~ /$keys_re/x
$keys_re not $key =~ /$keys_re/x
Truth table100%3/3
A and not(B)
A= $keys_reB= not $key =~ /$keys_re/x
ABresult
0X0
100
111
211  
212 61 my $subschema = $properties->{$key};
213 0 ▶ if (my $re_key = !$subschema && first {$key =~ /$_/x} keys %$p_properties) {
Subroutine __ANON__: not called CC=1 SCAR=0.8
Branch: 1 branch
BranchTrueFalseTotal
if (my $re_key = !$subschema && &first(sub { $key =~ /$_/x; } , keys %$p_properties)) 0 61 61
Condition66%2/3
!$subschema && &first(sub { $key =~ /$_/x; } , keys %$p_properties)
!ll&&!rl&&r
5830
MC/DC0%0/2
!$subschema && &first(sub { $key =~ /$_/x; } , keys %$p_properties)
!$subschema &first(sub { $key =~ /$_/x; } , keys %$p_properties)
Truth table66%2/3
not(A) && B
A= !$subschemaB= &first(sub { $key =~ /$_/x; } , keys %$p_properties)
ABresult
0X0
100
111
214 0 $subschema = $p_properties->{$re_key};
215 }
216  
217 61 $result->{$key} = $self->get_adjusted($struc->{$key}, $subschema, $self->_jpath($jpath, $key));
218 }
219  
220 15 ▶ if ($self->fill_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fill_defaults) 6 9 15
221 6 for my $key (keys %$properties) {
222 16 ▶ next if exists $result->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{$key} 13 3 16
223 3 my $subschema = $properties->{$key};
224 3 ▶ next if !exists $subschema->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subschema->{"default"} 0 3 3
225 3 $result->{$key} = $self->_default_value($subschema);
226 }
227 }
228  
229 15 return $result;
230 }
231  
232  
233 64 ▶sub _jpath {
Subroutine _jpath: called CC=3 SCAR=11.0
234 64 my ($self, $path, $key) = @_;
235 64 ▶ $path //= q{$};
Condition50%1/2
$path //= "\$"
l!l
064
MC/DC0%0/1
$path //= "\$"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
236  
237 64 ▶ return "$path.$key" if $key =~ /^[_A-Za-z]\w*$/x;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[_A-Za-z]\w*$/x 61 3 64
238  
239 3 $key =~ s/(['\\])/\\$1/gx;
240 3 return $path . "['$key']";
241 }
242  
243  
244 2 1;
245  
246 __END__
↑ Summary