-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
339 lines (271 loc) · 8.2 KB
/
inspector.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package gen
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"sort"
"strings"
"github.com/fatih/structtag"
"github.com/macinnir/dvc/core/lib"
"github.com/macinnir/dvc/core/lib/schema"
)
// NullPackage is the package name used for handling nulls
const NullPackage = "\"gopkg.in/guregu/null.v3\""
// buildModelNodeFromFile builds a node representation of a struct from a file
func buildGoStructFromFile(fileBytes []byte) (modelNode *lib.GoStruct, e error) {
src := string(fileBytes)
modelNode = lib.NewGoStruct()
var tree *ast.File
_, tree, e = parseFileToAST(fileBytes)
if e != nil {
return
}
ast.Inspect(tree, func(node ast.Node) bool {
// Check if this is a package
if s, ok := node.(*ast.File); ok {
modelNode.Package = s.Name.Name
if len(s.Comments) > 0 {
modelNode.Comments = s.Comments[0].Text()
}
modelNode.Imports = &lib.GoFileImports{}
for _, i := range s.Imports {
// This is a named import
if i.Name != nil {
modelNode.Imports.Append(i.Name.Name + " " + i.Path.Value)
} else {
modelNode.Imports.Append(i.Path.Value)
}
}
// for _, d := range s.Decls {
// GetReceiverTypeName
// }
}
// Declaration of our struct
if s, ok := node.(*ast.TypeSpec); ok {
modelNode.Name = s.Name.Name
}
if s, ok := node.(*ast.StructType); !ok {
return true
} else {
for _, field := range s.Fields.List {
fieldType := src[field.Type.Pos()-1 : field.Type.End()-1]
nodeField := &lib.GoStructField{
Name: field.Names[0].Name,
Tags: []*lib.GoStructFieldTag{},
DataType: fieldType,
Comments: field.Comment.Text(),
}
if field.Tag != nil {
tagString := field.Tag.Value[1 : len(field.Tag.Value)-1]
// fmt.Printf("Tag: %s\n", tagString)
tags, e := structtag.Parse(tagString)
if e != nil {
log.Fatal(e)
}
for _, tag := range tags.Tags() {
nodeField.Tags = append(nodeField.Tags, &lib.GoStructFieldTag{
Name: tag.Key,
Value: tag.Name,
Options: tag.Options,
})
}
}
modelNode.Fields.Append(nodeField)
}
}
return false
})
return
}
// buildModelNodeFromFile builds a node representation of a struct from a file
func buildModelNodeFromTable(table *schema.Table) (modelNode *lib.GoStruct, e error) {
modelNode = lib.NewGoStruct()
modelNode.Package = "models"
modelNode.Name = table.Name
modelNode.Comments = fmt.Sprintf("%s is a `%s` data model\n", table.Name, table.Name)
hasNull := false
sortedColumns := make(schema.SortedColumns, 0, len(table.Columns))
for _, column := range table.Columns {
sortedColumns = append(sortedColumns, column)
}
sort.Sort(sortedColumns)
for _, col := range sortedColumns {
fieldType := schema.DataTypeToGoTypeString(col)
if fieldDataTypeIsNull(fieldType) {
hasNull = true
}
modelNode.Fields.Append(&lib.GoStructField{
Name: col.Name,
DataType: fieldType,
Tags: []*lib.GoStructFieldTag{
{Name: "db", Value: col.Name, Options: []string{}},
{Name: "json", Value: col.Name, Options: []string{}},
},
Comments: "",
})
}
if hasNull {
modelNode.Imports.Append(NullPackage)
}
return
}
func fieldDataTypeIsNull(fieldType string) bool {
return len(fieldType) > 5 && fieldType[0:5] == "null."
}
// parseFileToAST takes a file path and parses the contents of that file into
// an AST representation
func parseFileToAST(fileBytes []byte) (fileSet *token.FileSet, tree *ast.File, e error) {
fileSet = token.NewFileSet()
tree, e = parser.ParseFile(fileSet, "", fileBytes, parser.ParseComments)
if e != nil {
return
}
return
}
func buildFileFromModelNode(table *schema.Table, modelNode *lib.GoStruct) (file []byte, e error) {
insertColumns := fetchInsertColumns(table.ToSortedColumns())
updateColumns := fetchUpdateColumns(table.ToSortedColumns())
primaryKey := fetchTablePrimaryKeyName(table)
var b strings.Builder
b.WriteString("// Generated Code; DO NOT EDIT.\n\npackage " + modelNode.Package + "\n\n")
if modelNode.Imports.Len() > 0 {
b.WriteString(modelNode.Imports.ToString() + "\n")
}
if len(modelNode.Comments) > 0 {
b.WriteString("// " + modelNode.Comments)
}
b.WriteString("type " + modelNode.Name + " struct {\n")
for _, f := range *modelNode.Fields {
b.WriteString("\t" + f.ToString())
}
b.WriteString("}\n")
b.WriteString(`
// ` + modelNode.Name + `_Column is the type used for ` + modelNode.Name + ` columns
type ` + modelNode.Name + `_Column string
// ` + modelNode.Name + `_Columns specifies the columns in the ` + modelNode.Name + ` model
var ` + modelNode.Name + `_Columns = struct {
`)
for _, f := range *modelNode.Fields {
b.WriteString("\t" + f.Name + " string\n")
}
b.WriteString("}{\n")
for _, f := range *modelNode.Fields {
b.WriteString("\t" + f.Name + ": \"" + f.Name + "\",\n")
}
b.WriteString("}\n\n")
// All Columns
b.WriteString("var (\n")
b.WriteString("\t// " + modelNode.Name + "_TableName is the name of the table \n")
b.WriteString("\t" + modelNode.Name + "_TableName = \"" + modelNode.Name + "\"\n")
b.WriteString("\t// " + modelNode.Name + "_PrimaryKey is the name of the table's primary key\n")
b.WriteString("\t" + modelNode.Name + "_PrimaryKey = \"" + primaryKey + "\"\n")
b.WriteString("\t// " + modelNode.Name + "_AllColumns is a list of all the columns\n")
b.WriteString("\t" + modelNode.Name + "_AllColumns = []string{")
for k, f := range *modelNode.Fields {
b.WriteString("\"" + f.Name + "\"")
if k < len(*modelNode.Fields)-1 {
b.WriteByte(',')
}
}
b.WriteString("}")
b.WriteByte('\n')
// Insert columns
b.WriteString("\t// " + modelNode.Name + "_InsertColumns is a list of all insert columns for this model\n")
b.WriteString("\t" + modelNode.Name + "_InsertColumns = []string{")
for k := range insertColumns {
col := insertColumns[k]
b.WriteString("\"" + col.Name + "\"")
if k < len(insertColumns)-1 {
b.WriteByte(',')
}
}
b.WriteString("}")
b.WriteByte('\n')
// Update columns
b.WriteString("\t// " + modelNode.Name + "_UpdateColumns is a list of all update columns for this model\n")
b.WriteString("\t" + modelNode.Name + "_UpdateColumns = []string{")
for k := range updateColumns {
col := updateColumns[k]
b.WriteString("\"" + col.Name + "\"")
if k < len(updateColumns)-1 {
b.WriteByte(',')
}
}
b.WriteString("}\n)")
file = []byte(b.String())
file, e = format.Source(file)
if e != nil {
log.Fatalf("FORMAT ERROR: File: %s; Error: %s\n%s", modelNode.Name, e.Error(), b.String())
}
return
}
func resolveTableToModel(modelNode *lib.GoStruct, table *schema.Table) {
fieldMap := map[string]int{}
modelFields := &lib.GoStructFields{}
nullImportIndex := -1
hasNullField := false
for k, i := range *modelNode.Imports {
if i == NullPackage {
nullImportIndex = k
break
}
}
i := 0
for _, m := range *modelNode.Fields {
// Skip any fields not in the database
if _, ok := table.Columns[m.Name]; !ok {
continue
}
fieldMap[m.Name] = i
modelFields.Append(m)
i++
}
for name, col := range table.Columns {
fieldIndex, ok := fieldMap[name]
// Add any fields not in the model
if !ok {
modelFields.Append(&lib.GoStructField{
Name: col.Name,
DataType: schema.DataTypeToGoTypeString(col),
Tags: []*lib.GoStructFieldTag{
{
Name: "db",
Value: col.Name,
Options: []string{},
},
{
Name: "json",
Value: col.Name,
Options: []string{},
},
},
})
} else {
// Check that the datatype hasn't changed
colDataType := schema.DataTypeToGoTypeString(col)
// log.Println(colDataType, fieldIndex, name)
if colDataType != (*modelFields)[fieldIndex].DataType {
(*modelFields)[fieldIndex].DataType = colDataType
}
}
}
// Finally check for nullables
for _, f := range *modelFields {
if fieldDataTypeIsNull(f.DataType) {
hasNullField = true
}
}
// If the package needs null, and it hasn't been added, add it
if hasNullField && nullImportIndex == -1 {
modelNode.Imports.Append(NullPackage)
}
// If no null import is needed, but one exists, remove it
if !hasNullField && nullImportIndex > -1 {
*modelNode.Imports = append((*modelNode.Imports)[:nullImportIndex], (*modelNode.Imports)[nullImportIndex+1:]...)
}
modelNode.Fields = modelFields
return
}