| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package JSON::Schema::Generator; |
|
2
|
2
|
|
|
2
|
|
1441
|
use 5.008001; |
|
|
2
|
|
|
|
|
8
|
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
44
|
|
|
4
|
2
|
|
|
2
|
|
16
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
99
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
|
7
|
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1128
|
use JSON::Schema::Generator::Handler::Array; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
58
|
|
|
9
|
2
|
|
|
2
|
|
1084
|
use JSON::Schema::Generator::Handler::Atom; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
56
|
|
|
10
|
2
|
|
|
2
|
|
1153
|
use JSON::Schema::Generator::Handler::Maybe::Lifted; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
57
|
|
|
11
|
2
|
|
|
2
|
|
1091
|
use JSON::Schema::Generator::Handler::Object; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
59
|
|
|
12
|
2
|
|
|
2
|
|
1196
|
use JSON::Schema::Generator::Handler::Union; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
57
|
|
|
13
|
2
|
|
|
2
|
|
1349
|
use JSON::TypeInference; |
|
|
2
|
|
|
|
|
31122
|
|
|
|
2
|
|
|
|
|
668
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
|
16
|
1
|
|
|
1
|
0
|
409
|
my ($class) = @_; |
|
17
|
1
|
|
|
|
|
4
|
return bless { learned_data => [] }, $class; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub learn { |
|
21
|
3
|
|
|
3
|
0
|
33
|
my ($self, $data) = @_; |
|
22
|
3
|
|
|
|
|
4
|
push @{$self->{learned_data}}, $data; |
|
|
3
|
|
|
|
|
15
|
|
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub generate { |
|
26
|
1
|
|
|
1
|
0
|
4
|
my ($self) = @_; |
|
27
|
1
|
|
|
|
|
4
|
my $inferred_type = JSON::TypeInference->infer($self->{learned_data}); |
|
28
|
|
|
|
|
|
|
return { |
|
29
|
|
|
|
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
30
|
|
|
|
|
|
|
title => 'TODO', |
|
31
|
|
|
|
|
|
|
description => 'TODO', |
|
32
|
1
|
|
|
|
|
3157
|
%{_dispatch($inferred_type, $self->{learned_data})}, |
|
|
1
|
|
|
|
|
4
|
|
|
33
|
|
|
|
|
|
|
}; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _dispatch { |
|
37
|
8
|
|
|
8
|
|
25
|
my ($type, $examples) = @_; |
|
38
|
8
|
|
|
|
|
13
|
my $dispatch = \&_dispatch; |
|
39
|
8
|
100
|
|
|
|
24
|
if ($type->name eq 'array') { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
11
|
return JSON::Schema::Generator::Handler::Array->process($type, $examples, $dispatch); |
|
41
|
|
|
|
|
|
|
} elsif ($type->name eq 'object') { |
|
42
|
1
|
|
|
|
|
21
|
return JSON::Schema::Generator::Handler::Object->process($type, $examples, $dispatch); |
|
43
|
|
|
|
|
|
|
} elsif ($type->name eq 'union') { |
|
44
|
1
|
|
|
|
|
21
|
return JSON::Schema::Generator::Handler::Union->process($type, $examples, $dispatch); |
|
45
|
|
|
|
|
|
|
} elsif ($type->name eq 'maybe') { |
|
46
|
1
|
|
|
|
|
28
|
return JSON::Schema::Generator::Handler::Maybe::Lifted->process($type, $examples, $dispatch); |
|
47
|
|
|
|
|
|
|
} else { |
|
48
|
4
|
|
|
|
|
75
|
return JSON::Schema::Generator::Handler::Atom->process($type, $examples, $dispatch); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
1; |
|
53
|
|
|
|
|
|
|
__END__ |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=encoding utf-8 |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
JSON::Schema::Generator - Generate JSON schema from data |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
use JSON::Schema::Generator; |
|
64
|
|
|
|
|
|
|
my $generator = JSON::Schema::Generator->new; |
|
65
|
|
|
|
|
|
|
$generator->learn({ id => 1, name => 'yuno' }); |
|
66
|
|
|
|
|
|
|
$generator->learn({ id => 2, name => 'miyako' }); |
|
67
|
|
|
|
|
|
|
$generator->learn({ id => 3, name => 'sae' }); |
|
68
|
|
|
|
|
|
|
my $schema = $generator->generate; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
JSON::Schema::Generator generate a JSON schema from actual data. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
It supports: |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
* type inference (using L<JSON::TypeInference>) |
|
77
|
|
|
|
|
|
|
* embedding example values |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 LICENSE |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Copyright (C) aereal. |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
84
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 AUTHOR |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
aereal E<lt>aereal@aereal.orgE<gt> |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|