forked from joubu/koha-misc4dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha_schema_to_swagger.pl
executable file
·145 lines (112 loc) · 3.57 KB
/
koha_schema_to_swagger.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2016 KohaSuomi
# Copyright 2016 Theke Solutions
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::Database;
use Data::Printer colored => 1;
use Getopt::Long;
use Pod::Usage;
use Template;
use Cwd qw(abs_path getcwd);
my $class;
my $help;
GetOptions(
"class=s" => \$class,
"h|help" => \$help
);
# If we were asked for usage instructions, do it
pod2usage(1) if defined $help or !defined $class;
my @columns
= Koha::Database->new->schema->resultset($class)->result_source->columns;
my $columns_info = Koha::Database->new->schema->resultset($class)
->result_source->columns_info;
my @properties;
foreach my $column (@columns) {
my $type
= "[\""
. column_type_to_swagger_type( $columns_info->{$column}->{data_type} )
. "\"";
$type .= ",\"null\""
if $columns_info->{$column}->{is_nullable};
$type .= "]";
push @properties,
{
name => $column,
type => $type,
description => $columns_info->{$column}->{koha_description} // "REPLACE WITH A PROPER DESCRIPTION"
};
}
my $cwd = abs_path($0);
$cwd =~ s/\/koha_object_to_swagger\.pl$//;
my $tt = Template->new(
{ INCLUDE_PATH => $cwd,
INTERPOLATE => 1
}
) || die "$Template::ERROR\n";
my $vars = { properties => \@properties };
$tt->process( 'tt/swagger-definition.tt', $vars ) || die "$Template::ERROR";
sub column_type_to_swagger_type {
my ($column_type) = @_;
my $mapping = {
############ BOOLEAN ############
'bool' => 'boolean',
'boolean' => 'boolean',
'tinyint' => 'boolean',
############ INTEGERS ###########
'bigint' => 'integer',
'integer' => 'integer',
'int' => 'integer',
'mediumint' => 'integer',
'smallint' => 'integer',
############ NUMBERS ############
'decimal' => 'number',
'double precision' => 'number',
'float' => 'number',
############ STRINGS ############
'blob' => 'string',
'char' => 'string',
'date' => 'string',
'datetime' => 'string',
'enum' => 'string',
'longblob' => 'string',
'longtext' => 'string',
'mediumblob' => 'string',
'mediumtext' => 'string',
'text' => 'string',
'tinyblob' => 'string',
'tinytext' => 'string',
'timestamp' => 'string',
'varchar' => 'string'
};
return $mapping->{$column_type} if exists $mapping->{$column_type};
}
1;
=head1 NAME
misc/devel/koha_schema_to_swagger.pl
=head1 SYNOPSIS
koha_schema_to_swagger.pl --class 'Koha::...'
The command in usually called from the root directory for the Koha source tree.
If you are running from another directory, use the --path switch to specify
a different path.
=head1 OPTIONS
=over 8
=item B<--class>
DBIC schema. (mandatory)
=item B<-h|--help>
prints this help text
=back