JSONSchema.pm

filter: Statementstmt 76.9% 120 / 156 Branchbran 65.1% 43 / 66 Conditioncond 70.6% 53 / 75 MC/DCmcdc 57.1% 24 / 42 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 71.8% 255 / 355 CC 65 SCAR 41.0
CC
65
Cov
73%
_get_json_schema_property_description
182.0
get_json_schema
50.3
_get_json_schema_property_min_max_value
4.8
CRAP 60.5
↑ Summary JSONSchema/Type/MySQL.pm »
1 package DBIx::Result::Convert::JSONSchema;
2  
3 4 our $VERSION = '0.06';
4  
5  
6 =head1 NAME
7  
8 DBIx::Result::Convert::JSONSchema - Convert DBIx result schema to JSON schema
9  
10 =begin html
11  
12 <a href='https://travis-ci.org/Humanstate/p5-dbix-result-convert-jsonschema?branch=master'><img src='https://travis-ci.org/Humanstate/p5-dbix-result-convert-jsonschema.svg?branch=master' alt='Build Status' /></a>
13 <a href='https://coveralls.io/github/Humanstate/p5-dbix-result-convert-jsonschema?branch=master'><img src='https://coveralls.io/repos/github/Humanstate/p5-dbix-result-convert-jsonschema/badge.svg?branch=master' alt='Coverage Status' /></a>
14  
15 =end html
16  
17 =head1 VERSION
18  
19 0.06
20  
21 =head1 SYNOPSIS
22  
23 use DBIx::Result::Convert::JSONSchema;
24  
25 my $SchemaConvert = DBIx::Result::Convert::JSONSchema->new( schema => Schema );
26 my $json_schema = $SchemaConvert->get_json_schema( DBIx::Class::ResultSource );
27  
28 =head1 DESCRIPTION
29  
30 This module attempts basic conversion of L<DBIx::Class::ResultSource> to equivalent
31 of L<http://json-schema.org/>.
32 By default the conversion assumes that the L<DBIx::Class::ResultSource> originated
33 from MySQL database. Thus all the types and defaults are set based on MySQL
34 field definitions.
35 It is, however, possible to overwrite field type map and length map to support
36 L<DBIx::Class::ResultSource> from other database solutions.
37  
38 Note, relations between tables are not taken in account!
39  
40 =cut
41  
42  
43 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use Types::Standard qw/ InstanceOf Enum HashRef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Module::Load qw/ load /;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49  
50 4 has schema => (
51 is => 'ro',
52 isa => InstanceOf['DBIx::Class::Schema'],
53 required => 1,
54 );
55  
56 4 has schema_source => (
57 is => 'lazy',
58 isa => Enum[ qw/ MySQL / ],
59 default => 'MySQL',
60 );
61  
62 has length_type_map => (
63 is => 'rw',
64 isa => HashRef,
65 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 return {
67 6 string => [ qw/ minLength maxLength / ],
68 number => [ qw/ minimum maximum / ],
69 integer => [ qw/ minimum maximum / ],
70 };
71 },
72 4 );
73  
74 has type_map => (
75 is => 'rw',
76 isa => HashRef,
77 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 4 my ( $self ) = @_;
79  
80 4 my $type_class = __PACKAGE__ . '::Type::' . $self->schema_source;
81 4 load $type_class;
82  
83 4 return $type_class->get_type_map;
84 },
85 4 );
86  
87 has length_map => (
88 is => 'rw',
89 isa => HashRef,
90 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 6 my ( $self ) = @_;
92  
93 6 my $defaults_class = __PACKAGE__ . '::Default::' . $self->schema_source;
94 6 load $defaults_class;
95  
96 6 return $defaults_class->get_length_map;
97 },
98 4 );
99  
100 has pattern_map => (
101 is => 'rw',
102 isa => HashRef,
103 lazy => 1,
104 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 return {
106 3 time => '^\d{2}:\d{2}:\d{2}$',
107 year => '^\d{4}$',
108 datetime => '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$',
109 timestamp => '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$',
110 };
111 }
112 4 );
113  
114 has format_map => (
115 is => 'rw',
116 isa => HashRef,
117 lazy => 1,
118 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 return {
120 3 date => 'date',
121 };
122 }
123 4 );
124  
125  
126 =head2 get_json_schema
127  
128 Returns somewhat equivalent JSON schema based on DBIx result source name.
129  
130 my $json_schema = $converted->get_json_schema( 'TableSource', {
131 schema_declaration => 'http://json-schema.org/draft-04/schema#',
132 decimals_to_pattern => 1,
133 has_schema_property_description => 1,
134 allow_additional_properties => 0,
135 ignore_property_defaults => 1,
136 overwrite_schema_property_keys => {
137 name => 'cat',
138 address => 'dog',
139 },
140 add_schema_properties => {
141 address => { ... },
142 bank_account => '#/definitions/bank_account',
143 },
144 overwrite_schema_properties => {
145 name => {
146 _action => 'merge', # one of - merge/overwrite
147 minimum => 10,
148 maximum => 20,
149 type => 'number',
150 },
151 },
152 include_required => [ qw/ street city / ],
153 exclude_required => [ qw/ name address / ],
154 exclude_properties => [ qw/ mouse house / ],
155  
156 dependencies => {
157 first_name => [ qw/ middle_name last_name / ],
158 },
159 });
160  
161 Optional arguments to change how JSON schema is generated:
162  
163 =over 8
164  
165 =item * schema_declaration
166  
167 Declare which version of the JSON Schema standard that the schema was written against.
168  
169 L<https://json-schema.org/understanding-json-schema/reference/schema.html>
170  
171 B<Default>: "http://json-schema.org/schema#"
172  
173 =item * decimals_to_pattern
174  
175 1/0 - value to indicate if 'number' type field should be converted to 'string' type with
176 RegExp pattern based on decimal place definition in database.
177  
178 B<Default>: 0
179  
180 =item * has_schema_property_description
181  
182 Generate schema description for fields e.g. 'Optional numeric type value for field context e.g. 1'.
183  
184 B<Default>: 0
185  
186 =item * ignore_property_defaults
187  
188 Do not set schema B<default> property field based on default in DBIx schema
189  
190 B<Default>: 0
191  
192 =item * allow_additional_properties
193  
194 Define if the schema accepts additional keys in given payload.
195  
196 B<Default>: 0
197  
198 =item * add_property_minimum_value
199  
200 If field does not have format type add minimum values for number and string types based on DB field type.
201 This might not make sense in most cases as the minimum is either 0 or the lower bound if number is signed.
202  
203 B<Default>: 0
204  
205 =item * overwrite_schema_property_keys
206  
207 HashRef representing mapping between old property name and new property name to overwrite existing schema keys,
208 Properties from old key will be assigned to the new property.
209  
210 B<Note> The key conversion is executed last, every other option e.g. C<exclude_properties> will work only on original
211 database column names.
212  
213 =item * overwrite_schema_properties
214  
215 HashRef of property name and new attributes which can be either overwritten or merged based on given B<_action> key.
216  
217 =item * exclude_required
218  
219 ArrayRef of database column names which should always be EXCLUDED from REQUIRED schema properties.
220  
221 =item * include_required
222  
223 ArrayRef of database column names which should always be INCLUDED in REQUIRED schema properties
224  
225 =item * exclude_properties
226  
227 ArrayRef of database column names which should be excluded from JSON schema AT ALL
228  
229 =item * dependencies
230  
231 L<https://json-schema.org/understanding-json-schema/reference/object.html#property-dependencies>
232  
233 =item * add_schema_properties
234  
235 HashRef of custom schema properties that must be included in final definition
236 Note that custom properties will overwrite defaults
237  
238 =item * schema_overwrite
239  
240 HashRef of top level schema properties e.g. 'required', 'properties' etc. to overwrite
241  
242 =back
243  
244 =cut
245  
246 9 ▶sub get_json_schema {
Subroutine get_json_schema: called CC=47 SCAR=39.2
Pod documented
247 9 my ( $self, $source, $args ) = @_;
248  
249 9 ▶ croak 'missing schema source' unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 1 8 9
250  
251 8 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
71
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
252  
253 # additional schema generation options
254 8 my $decimals_to_pattern = $args->{decimals_to_pattern};
255 8 my $has_schema_property_description = $args->{has_schema_property_description};
256 8 my $ignore_property_defaults = $args->{ignore_property_defaults};
257 8 ▶ my $overwrite_schema_property_keys = $args->{overwrite_schema_property_keys} // {};
Condition100%2/2
$args->{"overwrite_schema_property_keys"} // {}
l!l
17
MC/DC100%1/1
$args->{"overwrite_schema_property_keys"} // {}
$args->{"overwrite_schema_property_keys"}
Truth table100%2/2
A
A= $args->{"overwrite_schema_property_keys"}
Aresult
11
00
258 8 my $add_schema_properties = $args->{add_schema_properties};
259 8 ▶ my $overwrite_schema_properties = $args->{overwrite_schema_properties} // {};
Condition100%2/2
$args->{"overwrite_schema_properties"} // {}
l!l
17
MC/DC100%1/1
$args->{"overwrite_schema_properties"} // {}
$args->{"overwrite_schema_properties"}
Truth table100%2/2
A
A= $args->{"overwrite_schema_properties"}
Aresult
11
00
260 8 my $add_property_minimum_value = $args->{add_property_minimum_value};
261 8 ▶ my %exclude_required = map { $_ => 1 } @{ $args->{exclude_required} || [] };
Condition100%2/2
$args->{'exclude_required'} || []
l!l
17
MC/DC100%1/1
$args->{'exclude_required'} || []
$args->{'exclude_required'}
Truth table100%2/2
A
A= $args->{'exclude_required'}
Aresult
11
00
262 8 ▶ my %include_required = map { $_ => 1 } @{ $args->{include_required} || [] };
Condition100%2/2
$args->{'include_required'} || []
l!l
17
MC/DC100%1/1
$args->{'include_required'} || []
$args->{'include_required'}
Truth table100%2/2
A
A= $args->{'include_required'}
Aresult
11
00
263 8 ▶ my %exclude_properties = map { $_ => 1 } @{ $args->{exclude_properties} || [] };
Condition100%2/2
$args->{'exclude_properties'} || []
l!l
17
MC/DC100%1/1
$args->{'exclude_properties'} || []
$args->{'exclude_properties'}
Truth table100%2/2
A
A= $args->{'exclude_properties'}
Aresult
11
00
264  
265 8 my $dependencies = $args->{dependencies};
266 8 ▶ my $schema_declaration = $args->{schema_declaration} // 'http://json-schema.org/schema#';
Condition100%2/2
$args->{"schema_declaration"} // "http://json-schema.org/schema#"
l!l
17
MC/DC100%1/1
$args->{"schema_declaration"} // "http://json-schema.org/schema#"
$args->{"schema_declaration"}
Truth table100%2/2
A
A= $args->{"schema_declaration"}
Aresult
11
00
267 8 ▶ my $allow_additional_properties = $args->{allow_additional_properties} // 0;
Condition100%2/2
$args->{"allow_additional_properties"} // 0
l!l
17
MC/DC100%1/1
$args->{"allow_additional_properties"} // 0
$args->{"allow_additional_properties"}
Truth table100%2/2
A
A= $args->{"allow_additional_properties"}
Aresult
11
00
268 8 ▶ my $schema_overwrite = $args->{schema_overwrite} // {};
Condition100%2/2
$args->{"schema_overwrite"} // {}
l!l
17
MC/DC100%1/1
$args->{"schema_overwrite"} // {}
$args->{"schema_overwrite"}
Truth table100%2/2
A
A= $args->{"schema_overwrite"}
Aresult
11
00
269  
270 8 ▶ my %json_schema = (
Branch: 1 branch
BranchTrueFalseTotal
$dependencies ? : 1 7 8
271 '$schema' => $schema_declaration,
272 type => 'object',
273 required => [],
274 properties => {},
275 additionalProperties => $allow_additional_properties,
276  
277 ( $dependencies ? ( dependencies => $dependencies ) : () ),
278 );
279  
280 8 my $source_info = $self->_get_column_info( $source );
281  
282 SCHEMA_COLUMN:
283 7 foreach my $column ( keys %{ $source_info } ) {
284 196 ▶ next SCHEMA_COLUMN if $exclude_properties{ $column };
Branch: 1 branch
BranchTrueFalseTotal
if $exclude_properties{$column} 1 195 196
285  
286 195 my $column_info = $source_info->{ $column };
287  
288 # DBIx schema data type -> JSON schema data type
289 my $json_type = $self->type_map->{ $column_info->{data_type} }
290 or croak sprintf(
291 'unknown data type - %s (source: %s, column: %s)',
292 195 ▶ $column_info->{data_type}, $source, $column
Branch: 1 branch
BranchTrueFalseTotal
my $json_type = $self->type_map->{$column_info->{"data_type"}} or croak(sprintf('unknown data type - %s (source: %s, column: %s)', $column_info->{"data_type"}, $source, $column)) 195 0 195
293 );
294  
295 195 $json_schema{properties}->{ $column }->{type} = $json_type;
296  
297 # DBIx schema type -> JSON format
298 195 my $format_type = $self->format_map->{ $column_info->{data_type} };
299 195 ▶ if ( $format_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format_type) 7 188 195
300 7 $json_schema{properties}->{ $column }->{format} = $format_type;
301 }
302  
303 # DBIx schema size constraint -> JSON schema size constraint
304 195 ▶ if ( ! $format_type && $self->length_map->{ $column_info->{data_type} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $format_type and $self->length_map->{$column_info->{"data_type"}}) 139 56 195
Condition100%3/3
not $format_type and $self->length_map->{$column_info->{"data_type"}}
!ll&&!rl&&r
749139
MC/DC100%2/2
not $format_type and $self->length_map->{$column_info->{"data_type"}}
not $format_type $self->length_map->{$column_info->{"data_type"}}
Truth table100%3/3
not(A) and B
A= not $format_typeB= $self->length_map->{$column_info->{"data_type"}}
ABresult
0X0
100
111
305 139 $self->_set_json_schema_property_range( \%json_schema, $column_info, $column, $add_property_minimum_value );
306 }
307  
308 # DBIx schema required -> JSON schema required
309 195 my $is_required_field = $include_required{ $column };
310 195 ▶ if ( $is_required_field || ( ! $column_info->{default_value} && ! $column_info->{is_nullable} && ! $exclude_required{ $column } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required_field or not $column_info->{"default_value"} || $column_info->{"is_nullable"} || $exclude_required{$column}) 8 187 195
Condition100%9/9
$is_required_field or not $column_info->{"default_value"} || $column_info->{"is_nullable"} || $exclude_required{$column}
l!l&&r!l&&!r
26187
$column_info->{'default_value'} || $column_info->{'is_nullable'} || $exclude_required{$column}
l!l&&r!l&&!r
18616
$column_info->{"default_value"} or $column_info->{"is_nullable"}
l!l&&r!l&&!r
141727
MC/DC100%4/4
$is_required_field or not $column_info->{"default_value"} || $column_info->{"is_nullable"} || $exclude_required{$column}
$is_required_field $column_info->{"default_value"} $column_info->{"is_nullable"} $exclude_required{$column}
Truth table100%5/5
A or not(B or C || D)
A= $is_required_fieldB= $column_info->{"default_value"}C= $column_info->{"is_nullable"}D= $exclude_required{$column}
ABCDresult
1XXX1
00001
01XX0
001X0
00010
311 8 ▶ my $required_property = $overwrite_schema_property_keys->{ $column } // $column;
Condition33%1/3
$overwrite_schema_property_keys->{$column} // $column
l!l&&r!l&&!r
080
MC/DC0%0/2
$overwrite_schema_property_keys->{$column} // $column
$overwrite_schema_property_keys->{$column} $column
Truth table33%1/3
A // B
A= $overwrite_schema_property_keys->{$column}B= $column
ABresult
1X1
011
000
312 8 push @{ $json_schema{required} }, $required_property;
313 }
314  
315 # DBIx schema defaults -> JSON schema defaults (no refs e.g. current_timestamp)
316 195 ▶ if ( ! $ignore_property_defaults && defined $column_info->{default_value} && ! ref $column_info->{default_value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ignore_property_defaults and defined $column_info->{"default_value"} and not ref $column_info->{"default_value"}) 6 189 195
Condition100%6/6
not $ignore_property_defaults and defined $column_info->{"default_value"} and not ref $column_info->{"default_value"}
!ll&&!rl&&r
18366
not $ignore_property_defaults and defined $column_info->{"default_value"}
!ll&&!rl&&r
2815512
MC/DC100%3/3
not $ignore_property_defaults and defined $column_info->{"default_value"} and not ref $column_info->{"default_value"}
not $ignore_property_defaults defined $column_info->{"default_value"} not ref $column_info->{"default_value"}
Truth table100%4/4
not(A) and B and not(C)
A= not $ignore_property_defaultsB= defined $column_info->{"default_value"}C= not ref $column_info->{"default_value"}
ABCresult
0XX0
10X0
1100
1111
317 6 $json_schema{properties}->{ $column }->{default} = $column_info->{default_value};
318 }
319  
320 # DBIx schema list -> JSON enum list
321 195 ▶ if ( $json_type eq 'enum' && $column_info->{extra} && $column_info->{extra}->{list} ) { # no autovivification
Branch: 1 branch
BranchTrueFalseTotal
if ($json_type eq "enum" and $column_info->{"extra"} and $column_info->{"extra"}{"list"}) 14 181 195
Condition66%4/6
$json_type eq "enum" and $column_info->{"extra"} and $column_info->{"extra"}{"list"}
!ll&&!rl&&r
181014
$json_type eq "enum" and $column_info->{"extra"}
!ll&&!rl&&r
181014
MC/DC33%1/3
$json_type eq "enum" and $column_info->{"extra"} and $column_info->{"extra"}{"list"}
$json_type eq "enum" $column_info->{"extra"} $column_info->{"extra"}{"list"}
Truth table50%2/4
A and B and C
A= $json_type eq "enum"B= $column_info->{"extra"}C= $column_info->{"extra"}{"list"}
ABCresult
0XX0
10X0
1100
1111
322 14 $json_schema{properties}->{ $column }->{enum} = $column_info->{extra}->{list};
323 }
324  
325 # Consider 'is nullable' to accept 'null' values in all cases where field is not explicitly required
326 195 ▶ if ( ! $is_required_field && $column_info->{is_nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_required_field and $column_info->{"is_nullable"}) 172 23 195
Condition100%3/3
not $is_required_field and $column_info->{"is_nullable"}
!ll&&!rl&&r
221172
MC/DC100%2/2
not $is_required_field and $column_info->{"is_nullable"}
not $is_required_field $column_info->{"is_nullable"}
Truth table100%3/3
not(A) and B
A= not $is_required_fieldB= $column_info->{"is_nullable"}
ABresult
0X0
100
111
327 172 ▶ if ( $json_type eq 'enum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_type eq 'enum') { } 14 158 172
328 14 ▶ $json_schema{properties}->{ $column }->{enum} //= [];
Condition50%1/2
$json_schema{"properties"}{$column}{"enum"} //= []
l!l
140
MC/DC0%0/1
$json_schema{"properties"}{$column}{"enum"} //= []
$json_schema{"properties"}{$column}{"enum"}
Truth table50%1/2
A
A= $json_schema{"properties"}{$column}{"enum"}
Aresult
11
00
329 14 push @{ $json_schema{properties}->{ $column }->{enum} }, 'null';
330 }
331 else {
332 158 $json_schema{properties}->{ $column }->{type} = [ $json_type, 'null' ];
333 }
334 }
335  
336 # DBIx decimal numbers -> JSON schema numeric string pattern
337 195 ▶ if ( $json_type eq 'number' && $decimals_to_pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_type eq "number" and $decimals_to_pattern) 4 191 195
Condition100%3/3
$json_type eq "number" and $decimals_to_pattern
!ll&&!rl&&r
167244
MC/DC100%2/2
$json_type eq "number" and $decimals_to_pattern
$json_type eq "number" $decimals_to_pattern
Truth table100%3/3
A and B
A= $json_type eq "number"B= $decimals_to_pattern
ABresult
0X0
100
111
338 4 ▶ if ( $column_info->{size} && ref $column_info->{size} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"size"} and ref $column_info->{"size"} eq "ARRAY") 1 3 4
Condition66%2/3
$column_info->{"size"} and ref $column_info->{"size"} eq "ARRAY"
!ll&&!rl&&r
301
MC/DC50%1/2
$column_info->{"size"} and ref $column_info->{"size"} eq "ARRAY"
$column_info->{"size"} ref $column_info->{"size"} eq "ARRAY"
Truth table66%2/3
A and B
A= $column_info->{"size"}B= ref $column_info->{"size"} eq "ARRAY"
ABresult
0X0
100
111
339 1 $json_schema{properties}->{ $column }->{type} = 'string';
340 1 $json_schema{properties}->{ $column }->{pattern} = $self->_get_decimal_pattern( $column_info->{size} );
341 }
342 }
343  
344 # JSON schema field patterns
345 195 ▶ if ( $self->pattern_map->{ $column_info->{data_type} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pattern_map->{$column_info->{"data_type"}}) 30 165 195
346 30 $json_schema{properties}->{ $column }->{pattern} = $self->pattern_map->{ $column_info->{data_type} };
347 }
348  
349 # JSON schema property description
350 195 ▶ if ( ! $json_schema{properties}->{ $column }->{description} && $has_schema_property_description ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $json_schema{"properties"}{$column}{"description"} and $has_schema_property_description) 0 195 195
Condition33%1/3
not $json_schema{"properties"}{$column}{"description"} and $has_schema_property_description
!ll&&!rl&&r
01950
MC/DC0%0/2
not $json_schema{"properties"}{$column}{"description"} and $has_schema_property_description
not $json_schema{"properties"}{$column}{"description"} $has_schema_property_description
Truth table33%1/3
not(A) and B
A= not $json_schema{"properties"}{$column}{"description"}B= $has_schema_property_description
ABresult
0X0
100
111
351 my $property_description = $self->_get_json_schema_property_description(
352 $overwrite_schema_property_keys->{ $column } // $column,
353 0 ▶ $json_schema{properties}->{ $column }
Condition0%0/3
$overwrite_schema_property_keys->{$column} // $column
l!l&&r!l&&!r
000
MC/DC0%0/2
$overwrite_schema_property_keys->{$column} // $column
$overwrite_schema_property_keys->{$column} $column
Truth table0%0/3
A // B
A= $overwrite_schema_property_keys->{$column}B= $column
ABresult
1X1
011
000
354 );
355 0 $json_schema{properties}->{ $column }->{description} = $property_description;
356 }
357  
358 # JSON schema custom additional properties
359 195 ▶ if ( $add_schema_properties ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_schema_properties) 0 195 195
360 0 foreach my $property_key ( keys %{ $add_schema_properties } ) {
361 0 $json_schema{properties}->{ $property_key } = $add_schema_properties->{ $property_key };
362 }
363 }
364  
365 # Overwrites: merge JSON schema property key values with custom ones
366 195 ▶ if ( my $overwrite_property = delete $overwrite_schema_properties->{ $column } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $overwrite_property = delete $overwrite_schema_properties->{$column}) 2 193 195
367 2 ▶ my $action = delete $overwrite_property->{_action} // 'merge';
Condition50%1/2
delete $overwrite_property->{"_action"} // "merge"
l!l
20
MC/DC0%0/1
delete $overwrite_property->{"_action"} // "merge"
delete $overwrite_property->{"_action"}
Truth table50%1/2
A
A= delete $overwrite_property->{"_action"}
Aresult
11
00
368  
369 $json_schema{properties}->{ $column } = {
370 2 ▶ %{ $action eq 'merge' ? $json_schema{properties}->{ $column } : {} },
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'merge' ? : 1 1 2
371 2 %{ $overwrite_property }
372 };
373 }
374  
375 # Overwrite: replace JSON schema keys
376 195 ▶ if ( my $new_key = $overwrite_schema_property_keys->{ $column } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_key = $overwrite_schema_property_keys->{$column}) 2 193 195
377 2 $json_schema{properties}->{ $new_key } = delete $json_schema{properties}->{ $column };
378 }
379 }
380  
381 return {
382 %json_schema,
383 7 %{ $schema_overwrite },
384 };
385 }
386  
387 # Return DBIx result source column info for the given result class name
388 10 ▶sub _get_column_info {
Subroutine _get_column_info: called CC=1 SCAR=0.0
389 10 my ( $self, $source ) = @_;
390  
391 10 return $self->schema->source($source)->columns_info;
392 }
393  
394 # Returns RegExp pattern for decimal numbers based on database field definition
395 1 ▶sub _get_decimal_pattern {
Subroutine _get_decimal_pattern: called CC=1 SCAR=0.0
396 1 my ( $self, $size ) = @_;
397  
398 1 my ( $x, $y ) = @{ $size };
399 1 return sprintf '^\d{1,%s}\.\d{0,%s}$', $x - $y, $y;
400 }
401  
402 # Generates somewhat logical field description based on type and length constraints
403 0 ▶sub _get_json_schema_property_description {
Subroutine _get_json_schema_property_description: not called CC=13 SCAR=52.0
404 0 my ( $self, $column, $property ) = @_;
405  
406 0 ▶ if ( ! $property->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property->{"type"}) 0 0 0
407 0 ▶ if ( $property->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->{"enum"}) 0 0 0
408 0 return sprintf 'Enum list type, one of - %s', join( ', ', @{ $property->{enum} } );
409 }
410  
411 0 return '';
412 }
413  
414 0 ▶ return '' if $property->{type} eq 'object'; # no idea how to handle
Branch: 1 branch
BranchTrueFalseTotal
if $property->{"type"} eq "object" 0 0 0
415  
416 0 my %types;
417 0 ▶ if ( ref $property->{type} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $property->{'type'} eq 'ARRAY') { } 0 0 0
418 0 %types = map { $_ => 1 } @{ $property->{type} };
419 }
420 else {
421 0 $types{ $property->{type} } = 1;
422 }
423  
424 0 my $description = '';
425 0 ▶ $description .= 'Optional' if $types{null};
Branch: 1 branch
BranchTrueFalseTotal
if $types{"null"} 0 0 0
426  
427 0 my $type_part;
428 0 ▶ if ( grep { /^integer|number$/ } keys %types ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^integer|number$/;} keys %types) { } 0 0 0
429 0 $type_part = 'numeric';
430 }
431 else {
432 0 ( $type_part ) = grep { $_ ne 'null' } keys %types; # lucky roll, last type that isn't 'null' should be legit
433 }
434  
435 0 ▶ $description .= $description ? " $type_part" : ucfirst $type_part;
Branch: 1 branch
BranchTrueFalseTotal
$description ? : 0 0 0
436 0 $description .= sprintf ' type value for field %s', $column;
437  
438 0 ▶ if ( ( grep { /^integer$/ } keys %types ) && $property->{maximum} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {/^integer$/;} keys %types and $property->{'maximum'}) { } 0 0 0
elsif (grep {/^string$/;} keys %types and $property->{'pattern'}) { } 0 0 0
Condition0%0/3
grep {/^integer$/;} keys %types and $property->{"maximum"}
!ll&&!rl&&r
000
MC/DC0%0/2
grep {/^integer$/;} keys %types and $property->{"maximum"}
grep {/^integer$/;} keys %types $property->{"maximum"}
Truth table0%0/3
A and B
A= grep {/^integer$/;} keys %typesB= $property->{"maximum"}
ABresult
0X0
100
111
439 0 ▶ my $integer_example = $property->{default} // int rand $property->{maximum};
Condition0%0/2
$property->{"default"} // int rand $property->{"maximum"}
l!l
00
MC/DC0%0/1
$property->{"default"} // int rand $property->{"maximum"}
$property->{"default"}
Truth table0%0/2
A
A= $property->{"default"}
Aresult
11
00
440 0 $description .= ' e.g. ' . $integer_example;
441 }
442 0 ▶ elsif ( ( grep { /^string$/ } keys %types ) && $property->{pattern} ) {
Condition0%0/3
grep {/^string$/;} keys %types and $property->{"pattern"}
!ll&&!rl&&r
000
MC/DC0%0/2
grep {/^string$/;} keys %types and $property->{"pattern"}
grep {/^string$/;} keys %types $property->{"pattern"}
Truth table0%0/3
A and B
A= grep {/^string$/;} keys %typesB= $property->{"pattern"}
ABresult
0X0
100
111
443 0 $description .= sprintf ' with pattern %s ', $property->{pattern};
444 }
445  
446 0 return $description;
447 }
448  
449 # Convert from DBIx field length to JSON schema field length based on field type
450 139 ▶sub _set_json_schema_property_range {
Subroutine _set_json_schema_property_range: called CC=4 SCAR=13.9
451 139 my ( $self, $json_schema, $column_info, $column, $add_property_minimum_value ) = @_;
452  
453 139 my $json_schema_min_type = $self->length_type_map->{ $self->type_map->{ $column_info->{data_type} } }->[0];
454 139 my $json_schema_max_type = $self->length_type_map->{ $self->type_map->{ $column_info->{data_type} } }->[1];
455  
456 139 my $json_schema_min = $self->_get_json_schema_property_min_max_value( $column_info, 0 );
457 139 my $json_schema_max = $self->_get_json_schema_property_min_max_value( $column_info, 1 );
458  
459 # bump min value to 1 (don't see how this starts from negative)
460 139 ▶ $json_schema_min = 1 if $column_info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $column_info->{"is_auto_increment"} 0 139 139
461  
462 139 ▶ $json_schema->{properties}->{ $column }->{ $json_schema_min_type } = $json_schema_min
Branch: 1 branch
BranchTrueFalseTotal
if $add_property_minimum_value 40 99 139
463 if $add_property_minimum_value;
464 139 $json_schema->{properties}->{ $column }->{ $json_schema_max_type } = $json_schema_max;
465  
466 139 ▶ if ( $column_info->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"size"}) 34 105 139
467 34 $json_schema->{properties}->{ $column }->{ $json_schema_max_type } = $column_info->{size};
468 }
469  
470 139 return;
471 }
472  
473 # Returns min/max value from DBIx result field definition or lookup from defaults
474 278 ▶sub _get_json_schema_property_min_max_value {
Subroutine _get_json_schema_property_min_max_value: called CC=4 SCAR=15.6
475 278 my ( $self, $column_info, $range ) = @_;
476  
477 278 ▶ if ( $column_info->{extra} && $column_info->{extra}->{unsigned} ) { # no autovivification
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"extra"} and $column_info->{"extra"}{"unsigned"}) 0 278 278
Condition33%1/3
$column_info->{"extra"} and $column_info->{"extra"}{"unsigned"}
!ll&&!rl&&r
27800
MC/DC0%0/2
$column_info->{"extra"} and $column_info->{"extra"}{"unsigned"}
$column_info->{"extra"} $column_info->{"extra"}{"unsigned"}
Truth table33%1/3
A and B
A= $column_info->{"extra"}B= $column_info->{"extra"}{"unsigned"}
ABresult
0X0
100
111
478 0 return $self->length_map->{ $column_info->{data_type} }->{unsigned}->[ $range ];
479 }
480  
481 return ref $self->length_map->{ $column_info->{data_type} } eq 'ARRAY' ? $self->length_map->{ $column_info->{data_type} }->[ $range ]
482 278 ▶ : $self->length_map->{ $column_info->{data_type} }->{signed}->[ $range ];
Branch: 1 branch
BranchTrueFalseTotal
ref $self->length_map->{$column_info->{'data_type'}} eq 'ARRAY' ? : 180 98 278
483 }
484  
485 =head1 SEE ALSO
486  
487 L<DBIx::Class::ResultSource> - Result source object
488  
489 =head1 AUTHOR
490  
491 malishew - C<malishew@cpan.org>
492  
493 =head1 LICENSE
494  
495 This library is free software; you can redistribute it and/or modify it under
496 the same terms as Perl itself. If you would like to contribute documentation
497 or file a bug report then please raise an issue / pull request:
498  
499 https://github.com/Humanstate/p5-dbix-result-convert-jsonschema
500  
501 =cut
502  
503 4 __PACKAGE__->meta->make_immutable;
↑ Summary JSONSchema/Type/MySQL.pm »