blib/lib/JSON/Schema/Fit.pm

filter: Statementstmt 97.2% 105 / 108 Branchbran 84.0% 37 / 44 Conditioncond 78.7% 26 / 33 MC/DCmcdc 60.0% 12 / 20 Subroutinesub 96.3% 26 / 27 Podpod 100% 8 / 8 totaltotal 89.1% 214 / 240 CC 38 SCAR 17.0
CC
38
Cov
91%
_get_adjusted_object
13.0
_get_adjusted_number
10.0
get_adjusted
4.1
CRAP 5.5
↑ Summary
1 package JSON::Schema::Fit;
2 2 $JSON::Schema::Fit::VERSION = '0.05';
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 Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Math::Round qw/round nearest/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 11 ▶sub booleans { return _attr('booleans', @_); }
Subroutine booleans: called CC=1 SCAR=0.0
Pod documented
20  
21  
22 45 ▶sub numbers { return _attr('numbers', @_); }
Subroutine numbers: called CC=1 SCAR=0.0
Pod documented
23  
24  
25  
26 26 ▶sub round_numbers { return _attr('round_numbers', @_); }
Subroutine round_numbers: called CC=1 SCAR=0.0
Pod documented
27  
28  
29 26 ▶sub clamp_numbers { return _attr('clamp_numbers', @_); }
Subroutine clamp_numbers: called CC=1 SCAR=0.0
Pod documented
30  
31  
32  
33 10 ▶sub strings { return _attr('strings', @_); }
Subroutine strings: called CC=1 SCAR=0.0
Pod documented
34  
35  
36 11 ▶sub hash_keys { return _attr('hash_keys', @_); }
Subroutine hash_keys: called CC=1 SCAR=0.0
Pod documented
37  
38 # Store valid options as well as default values
39 my %valid_option =(
40 10 ( map { ($_ => 1) } qw!booleans numbers round_numbers strings hash_keys! ),
41 2 ( map { ($_ => 0) } qw!clamp_numbers! ),
42 );
43  
44 sub new {
45 10 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
46 10 my $self = bless {}, $class;
47 10 for my $k (keys %opts) {
48 13 ▶ next unless exists $valid_option{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_option{$k} 0 13 13
49 13 _attr($k, $self, $opts{$k});
50 }
51 10 return $self
52 }
53  
54 sub _attr {
55 142 ▶ my $attr = shift;
Subroutine _attr: called CC=3 SCAR=11.0
56 142 my $self = shift;
57  
58 142 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 13 129 142
59 13 return $self->{$attr} = shift;
60 } else {
61 129 ▶ return $self->{$attr} //= $valid_option{$attr};
Condition100%3/3
$self->{$attr} //= $valid_option{$attr}
l!l&&r!l&&!r
85395
MC/DC100%2/2
$self->{$attr} //= $valid_option{$attr}
$self->{$attr} $valid_option{$attr}
Truth table100%3/3
A //= B
A= $self->{$attr}B= $valid_option{$attr}
ABresult
1X1
011
000
62 }
63 }
64  
65  
66 sub get_adjusted {
67 66 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine get_adjusted: called CC=4 SCAR=14.0
Pod documented
68  
69 66 ▶ return $struc if !ref $schema || reftype $schema ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $schema or builtin::reftype($schema) ne "HASH" 3 63 66
Condition66%2/3
not ref $schema or builtin::reftype($schema) ne "HASH"
l!l&&r!l&&!r
3063
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
70 63 my $method = $self->_adjuster_by_type($schema->{type});
71 63 ▶ return $struc if !$method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 63 63
72 63 return $self->$method($struc, $schema, $jpath);
73 }
74  
75  
76 sub _adjuster_by_type {
77 63 ▶ my ($self, $type) = @_;
Subroutine _adjuster_by_type: called CC=3 SCAR=12.0
78  
79 63 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 63 63
80 63 my $method = "_get_adjusted_$type";
81 63 ▶ return $method if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 63 0 63
82 0 return;
83 }
84  
85  
86 sub _get_adjusted_boolean {
87 11 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_boolean: called CC=3 SCAR=11.0
88  
89 11 ▶ return $struc if !$self->booleans();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->booleans 2 9 11
90 9 ▶ return JSON::true if $struc;
Branch: 1 branch
BranchTrueFalseTotal
if $struc 8 1 9
91 1 return JSON::false;
92 }
93  
94  
95 sub _get_adjusted_integer {
96 17 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_integer: called CC=2 SCAR=6.9
97  
98 17 ▶ return $struc if !$self->numbers();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->numbers 2 15 17
99 15 my $result = $self->_get_adjusted_number($struc, $schema, $jpath);
100 15 return round($result);
101 }
102  
103  
104 sub _get_adjusted_number {
105 28 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_number: called CC=10 SCAR=23.0
106  
107 28 ▶ return $struc if !$self->numbers();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->numbers 2 26 28
108 26 my $result = 0+$struc;
109  
110 26 ▶ if ($self->round_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->round_numbers) 24 2 26
111 24 ▶ my $quantum = $schema->{multipleOf} || $schema->{divisibleBy};
Condition66%2/3
$schema->{'multipleOf'} || $schema->{'divisibleBy'}
l!l&&r!l&&!r
1509
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
112 24 ▶ $result = nearest $quantum, $result if $quantum;
Branch: 1 branch
BranchTrueFalseTotal
if $quantum 15 9 24
113 }
114  
115 26 ▶ if ($self->clamp_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clamp_numbers) 16 10 26
116 16 ▶ $result = $schema->{maximum} if exists $schema->{maximum} and $result > $schema->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"maximum"} and $result > $schema->{"maximum"} 3 13 16
Condition100%3/3
exists $schema->{"maximum"} and $result > $schema->{"maximum"}
!ll&&!rl&&r
1123
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
117 16 ▶ $result = $schema->{minimum} if exists $schema->{minimum} and $result < $schema->{minimum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"minimum"} and $result < $schema->{"minimum"} 1 15 16
Condition100%3/3
exists $schema->{"minimum"} and $result < $schema->{"minimum"}
!ll&&!rl&&r
1321
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
118 }
119  
120 26 return $result;
121 }
122  
123  
124 sub _get_adjusted_string {
125 10 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_string: called CC=2 SCAR=6.9
126  
127 10 ▶ return $struc if !$self->strings();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->strings 2 8 10
128 8 return "$struc";
129 }
130  
131  
132 sub _get_adjusted_array {
133 1 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_array: called CC=3 SCAR=11.0
134  
135 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
136  
137 1 my $result = [];
138 1 for my $i ( 0 .. $#$struc ) {
139 3 push @$result, $self->get_adjusted($struc->[$i], $schema->{items}, $self->_jpath($jpath, $i));
140 }
141  
142 1 return $result;
143 }
144  
145  
146  
147 sub _get_adjusted_object {
148 11 ▶ my ($self, $struc, $schema, $jpath) = @_;
Subroutine _get_adjusted_object: called CC=12 SCAR=25.6
149  
150 11 ▶ croak "Structure is not HASH at $jpath" if reftype $struc ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($struc) ne "HASH" 0 11 11
151  
152 11 my $result = {};
153 11 my $keys_re;
154  
155 11 ▶ my $properties = $schema->{properties} || {};
Condition50%1/2
$schema->{'properties'} || {}
l!l
110
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table50%1/2
A
A= $schema->{'properties'}
Aresult
11
00
156 11 ▶ my $p_properties = $schema->{patternProperties} || {};
Condition50%1/2
$schema->{'patternProperties'} || {}
l!l
011
MC/DC0%0/1
$schema->{'patternProperties'} || {}
$schema->{'patternProperties'}
Truth table50%1/2
A
A= $schema->{'patternProperties'}
Aresult
11
00
157  
158 11 ▶ 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"}) 8 3 11
Condition83%5/6
$self->hash_keys and exists $schema->{"additionalProperties"} and not $schema->{"additionalProperties"}
!ll&&!rl&&r
308
$self->hash_keys and exists $schema->{"additionalProperties"}
!ll&&!rl&&r
218
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
159 my $keys_re_text = join q{|}, (
160 keys %$p_properties,
161 8 map {quotemeta} keys %$properties,
162 );
163 8 $keys_re = qr{^$keys_re_text$}x;
164 }
165  
166 11 for my $key (keys %$struc) {
167 49 ▶ next if $keys_re && $key !~ $keys_re;
Branch: 1 branch
BranchTrueFalseTotal
if $keys_re and not $key =~ /$keys_re/x 8 41 49
Condition100%3/3
$keys_re and not $key =~ /$keys_re/x
!ll&&!rl&&r
12298
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
168  
169 41 my $subschema = $properties->{$key};
170 41 ▶ 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 41 41
Condition66%2/3
!$subschema && &first(sub { $key =~ /$_/x; } , keys %$p_properties)
!ll&&!rl&&r
3830
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
171 0 $subschema = $p_properties->{$re_key};
172 }
173  
174 41 $result->{$key} = $self->get_adjusted($struc->{$key}, $subschema, $self->_jpath($jpath, $key));
175 }
176  
177 11 return $result;
178 }
179  
180  
181 sub _jpath {
182 44 ▶ my ($self, $path, $key) = @_;
Subroutine _jpath: called CC=3 SCAR=11.0
183 44 ▶ $path //= q{$};
Condition50%1/2
$path //= "\$"
l!l
044
MC/DC0%0/1
$path //= "\$"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
184  
185 44 ▶ return "$path.$key" if $key =~ /^[_A-Za-z]\w*$/x;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[_A-Za-z]\w*$/x 41 3 44
186
187 3 $key =~ s/(['\\])/\\$1/gx;
188 3 return $path . "['$key']";
189 }
190  
191  
192 2 1;
193  
194 __END__
↑ Summary