blib/lib/JSON/Schema/Generate.pm

filter: Statementstmt 20.4% 28 / 137 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 31.0% 9 / 29 Podpod 100% 3 / 3 totaltotal 14.4% 40 / 277 CC 56 SCAR 58.7
CC
56
Cov
12%
_build_props
930.0
_unique_examples
110.0
_handle_required
56.0
CRAP 354.5
↑ Summary
1 package JSON::Schema::Generate;
2 9 ▶use 5.006; use strict; use warnings; our $VERSION = '0.15';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use Tie::IxHash; use Types::Standard qw/Str HashRef Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Compiled::Params::OO qw/cpo/; use JSON; use Blessed::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our ($validate, $JSON);
7 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=4.9
8 $validate = cpo(
9 new => {
10 id => {
11 type => Str,
12 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 'http://example.com/root.json'
14 }
15 },
16 title => {
17 type => Str,
18 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 'The Root Schema'
20 }
21 },
22 description => {
23 type => Str,
24 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 'The root schema is the schema that comprises the entire JSON document.'
26 }
27 },
28 schema => {
29 type => Str,
30 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 'http://json-schema.org/draft-07/schema#'
32 }
33 },
34 spec => {
35 type => HashRef,
36 0 ▶ default => sub { { } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 },
38 merge_examples => {
39 type => Bool,
40 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 },
42 none_required => {
43 type => Bool,
44 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 },
46 no_id => {
47 type => Bool,
48 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 },
50 no_title => {
51 type => Bool,
52 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 },
54 no_description => {
55 type => Bool,
56 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 },
58 no_example => {
59 type => Bool,
60 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 },
62 minimal_schema => {
63 type => Bool,
64 0 ▶ default => sub { !!0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 },
66 }
67 9 );
68 9 $JSON = JSON->new->canonical->pretty;
69 }
70  
71 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
72 0 my $class = shift;
73 0 my $args = $validate->new->(@_);
74 0 my $self = bless {}, $class;
75 0 $self->{schema} = _tie_hash();
76 0 $self->{schema}{'$schema'} = $args->schema;
77 0 $self->{schema}{'$id'} = $args->id;
78 0 $self->{schema}{title} = $args->title;
79 0 $self->{schema}{description} = $args->description;
80 0 $self->{$_} = $args->$_ for qw/spec merge_examples none_required no_id no_title no_description no_example minimal_schema/;
81 0 ▶ if ($args->merge_examples) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->merge_examples) 0 0 0
82 0 $self->{merge} = Blessed::Merge->new(
83 blessed => 0,
84 unique_array => 1,
85 unique_hash => 1,
86 same => 0
87 );
88 }
89 0 ▶ if ($self->{minimal_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"minimal_schema"}) 0 0 0
90 # make sure all the other no_* features are turned on
91 0 $self->{$_} = 1 for qw/no_id no_title no_description no_example/;
92 }
93 0 return $self;
94 }
95  
96 0 ▶sub learn {
Subroutine learn: not called CC=2 SCAR=17.9
Pod documented
97 0 ▶ $_[0]->{data} = ref $_[1] ? $_[1] : $JSON->decode($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
98 0 $_[0]->{data_runs}++;
99 0 $_[0]->_build_props($_[0]->{schema}, $_[0]->{data}, '#');
100 0 return $_[0];
101 }
102  
103 0 ▶sub generate {
Subroutine generate: not called CC=3 SCAR=24.8
Pod documented
104 0 my ($self, $struct) = @_;
105 0 ▶ $self->_handle_required($self->{schema}) unless $self->{none_required};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"none_required"} 0 0 0
106 0 ▶ return $struct ? $self->{schema} : $JSON->encode($self->{schema});
Branch: 1 branch
BranchTrueFalseTotal
$struct ? : 0 0 0
107 }
108  
109 0 ▶sub _handle_required {
Subroutine _handle_required: not called CC=7 SCAR=40.3
110 0 my ($self, $schema) = @_;
111 0 my $total_runs = $self->{data_runs};
112 0 ▶ if ($schema->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"required"}) 0 0 0
113 0 my @required;
114 0 for my $key (keys %{$schema->{required}}) {
115 0 ▶ if ($schema->{required}->{$key} == $total_runs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"required"}{$key} == $total_runs) 0 0 0
116 0 push @required, $key;
117 }
118 }
119 0 $schema->{required} = \@required;
120 }
121 0 ▶ if ($schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"properties"}) 0 0 0
122 0 $self->_handle_required($schema->{properties}{$_}) for keys %{$schema->{properties}};
123 }
124 0 ▶ $self->_handle_required($schema->{items}) if $schema->{items};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"items"} 0 0 0
125 }
126  
127 0 ▶sub _build_props {
Subroutine _build_props: not called CC=30 SCAR=68.4
128 0 my ($self, $props, $data, $path) = @_;
129  
130 0 my $ref = ref $data;
131 0 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (not defined $data) { } 0 0 0
elsif ($ref eq 'SCALAR' or $ref =~ /Boolean$/) { } 0 0 0
elsif ($data =~ /^\d+$/) { } 0 0 0
elsif ($data =~ /^\d+\.\d+$/) { } 0 0 0
elsif ($data =~ /\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}\+\d{2}\:\d{2}/) { } 0 0 0
132 0 $self->_add_type($props, 'object');
133 0 $self->_unique_examples($props, $data);
134 0 ▶ return if ref $props->{type};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"type"} 0 0 0
135 0 ▶ if (!$props->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->{"properties"}) 0 0 0
136 0 ▶ $props->{required} = _tie_hash() unless $self->{none_required};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"none_required"} 0 0 0
137 0 $props->{properties} = _tie_hash();
138 }
139 0 for my $key (sort keys %{$data}) {
140 0 ▶ $props->{required}->{$key}++ unless $self->{none_required};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"none_required"} 0 0 0
141 0 ▶ my $id = $path . ( $path eq '#' ? '' : '-' ) . 'properties-' . $key;
Branch: 1 branch
BranchTrueFalseTotal
$path eq '#' ? : 0 0 0
142 0 ▶ unless ($props->{properties}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->{"properties"}{$key}) 0 0 0
143 0 $props->{properties}{$key} = _tie_hash();
144 0 %{$props->{properties}{$key}} = (
145 ($self->{no_id} ? () : ('$id' => $id)),
146 ($self->{no_title} ? () : (title => 'The title')),
147 ($self->{no_description} ? () : (description => 'An explanation about the purpose of this instance')),
148 0 ▶ ($self->{spec}->{$key} ? %{$self->{spec}->{$key}} : ())
Branch: 4 branches
BranchTrueFalseTotal
$self->{'no_id'} ? : 0 0 0
$self->{'no_title'} ? : 0 0 0
$self->{'no_description'} ? : 0 0 0
$self->{'spec'}{$key} ? : 0 0 0
149 );
150 }
151 0 $self->_build_props($props->{properties}{$key}, $data->{$key}, $id);
152 }
153 } elsif ($ref eq 'ARRAY') {
154 0 $self->_add_type($props, 'array');
155 0 my $id = $path . '-items';
156 0 ▶ unless ($props->{items}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->{"items"}) 0 0 0
157 0 $props->{items} = _tie_hash();
158 0 ▶ $props->{items}{'$id'} = $id unless $self->{no_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_id"} 0 0 0
159 0 ▶ $props->{items}{title} = 'The Items Schema' unless $self->{no_title};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_title"} 0 0 0
160 0 ▶ $props->{items}{description} = 'An explanation about the purpose of this instance.' unless $self->{no_description};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_description"} 0 0 0
161 }
162 map {
163 0 $self->_build_props($props->{items}, $_, $id);
164 0 } @{$data}
165 } elsif (! defined $data) {
166 0 $self->_add_type($props, 'null');
167 0 ▶ $self->_unique_examples($props, undef) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
168 ▶ } elsif ($ref eq 'SCALAR' || $ref =~ m/Boolean$/) {
Condition0%0/3
$ref eq "SCALAR" or $ref =~ /Boolean$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "SCALAR" or $ref =~ /Boolean$/
$ref eq "SCALAR" $ref =~ /Boolean$/
Truth table0%0/3
A or B
A= $ref eq "SCALAR"B= $ref =~ /Boolean$/
ABresult
1X1
011
000
169 0 $self->_add_type($props, 'boolean');
170 0 ▶ $self->_unique_examples($props, \1, \0) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
171 } elsif ($data =~ m/^\d+$/) {
172 0 $self->_add_type($props, 'integer');
173 0 ▶ $self->_unique_examples($props, $data) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
174 } elsif ($data =~ m/^\d+\.\d+$/) {
175 0 $self->_add_type($props, 'number');
176 0 ▶ $self->_unique_examples($props, $data) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
177 } elsif ($data =~ m/\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}\+\d{2}\:\d{2}/) {
178 0 $self->_add_type($props, 'date-time');
179 0 ▶ $self->_unique_examples($props, $data) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
180 } else {
181 0 $self->_add_type($props, 'string');
182 0 ▶ $self->_unique_examples($props, $data) unless $self->{no_example};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_example"} 0 0 0
183 }
184  
185 0 return $props;
186 }
187  
188 0 ▶sub _tie_hash {
Subroutine _tie_hash: not called CC=1 SCAR=6.9
189 0 my %properties;
190 0 tie %properties, 'Tie::IxHash';
191 0 return \%properties;
192 }
193  
194 0 ▶sub _add_type {
Subroutine _add_type: not called CC=5 SCAR=34.0
195 0 my ($self, $props, $type) = @_;
196 0 ▶ if (!$props->{type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $props->{'type'}) { } 0 0 0
elsif ($props->{'type'} ne $type) { } 0 0 0
197 0 $props->{type} = $type;
198 } elsif ($props->{type} ne $type) {
199 0 ▶ $props->{type} = [ $props->{type} ] unless ref $props->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $props->{"type"} 0 0 0
200 0 ▶ push @{$props->{type}}, $type unless grep { $type eq $_ } @{$props->{type}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$type eq $_;} @{$$props{"type"};} 0 0 0
201 }
202 }
203  
204 0 ▶sub _unique_examples {
Subroutine _unique_examples: not called CC=10 SCAR=47.0
205 0 my ($self, $props, @examples) = @_;
206 0 ▶ return if ($self->{no_example});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_example"} 0 0 0
207
208 0 for my $example (@examples) {
209 0 ▶ if ((ref($example) || 'SCALAR') ne 'SCALAR' && $props->{examples} && $self->{merge_examples}) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $example || 'SCALAR') ne 'SCALAR' and $props->{'examples'} and $self->{'merge_examples'}) { } 0 0 0
Condition0%0/8
(ref $example || "SCALAR") ne "SCALAR" and $props->{"examples"} and $self->{"merge_examples"}
!ll&&!rl&&r
000
(ref $example || "SCALAR") ne "SCALAR" and $props->{"examples"}
!ll&&!rl&&r
000
ref $example || 'SCALAR'
l!l
00
MC/DC0%0/3
(ref $example || "SCALAR") ne "SCALAR" and $props->{"examples"} and $self->{"merge_examples"}
ref $example $props->{"examples"} $self->{"merge_examples"}
Truth table0%0/4
A and B and C
A= ref $exampleB= $props->{"examples"}C= $self->{"merge_examples"}
ABCresult
0XX0
10X0
1100
1111
210 0 $props->{examples}->[0] = $self->{merge}->merge($props->{examples}->[0], $example);
211 } else {
212 0 ▶ unless (grep { ($_//"") eq ($example//"") } @{$props->{examples}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {($_ // "") eq ($example // "");} @{$$props{"examples"};}) 0 0 0
Condition0%0/4
$_ // ""
l!l
00
$example // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
MC/DC0%0/1
$example // ""
$example
Truth table0%0/2
A
A= $_
Aresult
11
00
Truth table0%0/2
A
A= $example
Aresult
11
00
213 0 push @{$props->{examples}}, $example;
214 }
215 }
216 }
217 }
218  
219 9 1;
220  
221 __END__
↑ Summary