This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
/
main.go
241 lines (210 loc) · 5.03 KB
/
main.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"go/format"
"log"
"os"
"strconv"
"strings"
"unicode"
_ "github.com/go-sql-driver/mysql"
)
var config Configuration
type dbType interface {
getSchema(config Configuration) []ColumnSchema
goType(col *ColumnSchema) (string, string, error)
getEnvMap() map[string]string
}
type Configuration struct {
DbType string `json:"db_type"`
DbUser string `json:"db_user"`
DbPassword string `json:"db_password"`
DbName string `json:"db_name"`
DbHost string `json:"db_host"`
DbPort int `json:"db_port"`
OutputFile string `json:"output_file"`
// PkgName gives name of the package using the stucts
PkgName string `json:"pkg_name"`
// SQLTag produces tags commonly used to match database field names with Go struct members
SQLTag string `json:"sql_tag"`
// StructTag produces a tag to each struct
StructTag string `json:"struct_tag"`
}
type ColumnSchema struct {
TableName string
ColumnName string
IsNullable string
DataType string
CharacterMaximumLength sql.NullInt64
NumericPrecision sql.NullInt64
NumericScale sql.NullInt64
ColumnType string
ColumnKey string
}
func getOutput(config Configuration, db dbType, schemas []ColumnSchema) ([]byte, error) {
currentTable := ""
var neededImports []string
// First, get body text into var out
out := ""
for _, cs := range schemas {
if cs.TableName != currentTable {
if currentTable != "" {
out += "}\n\n"
}
out += "// " + formatName(cs.TableName) + "\n"
if config.StructTag != "" {
out += "// " + config.StructTag + "\n"
}
out += "type " + formatName(cs.TableName) + " struct{\n"
}
goType, requiredImport, err := db.goType(&cs)
if requiredImport != "" {
neededImports = append(neededImports, requiredImport)
}
if err != nil {
return []byte{}, err
}
out += "\t" + formatName(cs.ColumnName) + " " + goType
tags := []string{"column:" + cs.ColumnName}
switch cs.ColumnKey {
case "PRI":
tags = append(tags, "primary_key")
case "UNI":
tags = append(tags, "unique")
}
if cs.IsNullable != "YES" {
tags = append(tags, "not null")
}
if len(config.SQLTag) > 0 {
out += "\t`" + fmt.Sprintf(`%s:"%s"`, config.SQLTag, strings.Join(tags, ";")) + "`"
}
out += "\n"
currentTable = cs.TableName
}
out = out + "}"
// Build the header section
headerTmpl := "package %s \n\n %s"
imports := ""
if len(neededImports) > 0 {
imports = "import (\n"
for _, imp := range neededImports {
imports += "\t\"" + imp + "\"\n"
}
imports += ")\n\n"
}
header := fmt.Sprintf(headerTmpl, config.PkgName, imports)
return format.Source([]byte(header + out))
}
func writeStructs(config Configuration, output []byte) error {
if config.OutputFile == "" { // Output stdout if not specified
fmt.Println(string(output))
return nil
}
file, err := os.Create(config.OutputFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = fmt.Fprint(file, string(output))
return err
}
func formatName(name string) string {
newName := lintName(strings.Title(name))
// If a first charactor of the table is number, add "A" to the top
if unicode.IsNumber(rune(newName[0])) {
newName = "A" + newName
}
return newName
}
var configFile = flag.String("json", "", "Config file")
func usage() {
fmt.Printf("Usage of %s:\n -json <JSON file>\n", os.Args[0])
fmt.Println(` or use these environmental variables.
MYSQL_HOST
MYSQL_PORT
MYSQL_DATABASE
MYSQL_USER
MYSQL_PASSWORD
`)
}
const (
EnvHostKey = "EnvHostKey"
EnvPortKey = "EnvPortKey"
EnvDataBaseKey = "EnvDataBaseKey"
EnvUserKey = "EnvUserKey"
EnvPasswordKey = "EnvPasswordKey"
)
func overrideByEnv(envMap map[string]string) error {
v, ok := os.LookupEnv(envMap[EnvHostKey])
if ok {
config.DbHost = v
}
v, ok = os.LookupEnv(envMap[EnvPortKey])
if ok {
p, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("parse error MYSQL_PORT, %s", err)
}
config.DbPort = p
}
v, ok = os.LookupEnv(envMap[EnvDataBaseKey])
if ok {
config.DbName = v
}
v, ok = os.LookupEnv(envMap[EnvUserKey])
if ok {
config.DbUser = v
}
v, ok = os.LookupEnv(envMap[EnvPasswordKey])
if ok {
config.DbPassword = v
}
return nil
}
// NewDB returns DBtype
func NewDB(dbType string) (dbType, error) {
return MySQL{}, nil
}
func main() {
flag.Parse()
if *configFile != "" {
f, err := os.Open(*configFile)
if err != nil {
log.Fatal(err)
}
err = json.NewDecoder(f).Decode(&config)
if err != nil {
log.Fatal(err)
}
}
if config.DbType == "" {
config.DbType = "mysql"
}
db, err := NewDB(config.DbType)
if err != nil {
log.Fatal(err)
}
err = overrideByEnv(db.getEnvMap())
if err != nil {
log.Fatal(err)
}
if config.DbHost == "" {
usage()
os.Exit(0)
}
if config.DbType == "" {
config.DbType = "mysql"
}
columns := db.getSchema(config)
output, err := getOutput(config, db, columns)
if err != nil {
log.Fatal(err)
}
err = writeStructs(config, output)
if err != nil {
log.Fatal(err)
}
}