-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
203 lines (158 loc) · 4.3 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
package gen
import (
"go/ast"
"go/parser"
"go/token"
"log"
"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
}
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 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
}