-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.go
403 lines (364 loc) · 10.1 KB
/
language.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reverse
import (
"errors"
"fmt"
"go/token"
"path/filepath"
"reflect"
"sort"
"strings"
"text/template"
"time"
"github.com/azhai/gozzo/filesystem"
"github.com/azhai/gozzo/match"
"github.com/azhai/xgen/rewrite"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)
type (
Formatter func(filename string, sourceCode []byte) ([]byte, error)
Importter func(tables map[string]*schemas.Table) map[string]string
Packager func(targetDir string) string
kind int
)
const ( // 约定大于配置
FixedStrMaxSize = 255 // 固定字符串最大长度
invalidKind kind = iota
boolKind
complexKind
intKind
floatKind
integerKind
stringKind
uintKind
XormTagName = "xorm"
XormTagNotNull = "notnull"
XormTagAutoIncr = "autoincr"
XormTagPrimaryKey = "pk"
XormTagUnique = "unique"
XormTagIndex = "index"
UnsignedFloat = "UNSIGNED FLOAT"
UnsignedDouble = "UNSIGNED DOUBLE"
)
var (
errBadComparisonType = errors.New("invalid type for comparison")
// errBadComparison = errors.New("incompatible types for comparison")
// errNoComparison = errors.New("missing argument for comparison")
TypeOfString = reflect.TypeOf("")
TypeOfBytes = reflect.TypeOf([]byte{})
TypeOfBool = reflect.TypeOf(false)
TypeOfInt = reflect.TypeOf(0)
TypeOfInt64 = reflect.TypeOf(int64(0))
TypeOfFloat64 = reflect.TypeOf(float64(0))
TypeOfTime = reflect.TypeOf(time.Time{})
golang = &Language{ // Golang represents a golang language
Name: "golang",
ExtName: ".go",
Template: nil,
Types: map[string]string{},
Funcs: template.FuncMap{
"Type": type2string,
"Tag": tag2string,
"GetSinglePKey": getSinglePKey,
"GetCreatedColumn": getCreatedColumn,
},
Formatter: rewrite.WriteGolangFilePrettify,
Importter: genGoImports,
Packager: genNameSpace,
}
)
// Language represents a languages supported when reverse codes
type Language struct {
Name string
ExtName string
Template *template.Template
Types map[string]string
Funcs template.FuncMap
Formatter Formatter
Importter Importter
Packager Packager
}
func escapeTag(value string) string {
return strings.ReplaceAll(value, "#", "")
}
func basicKind(v reflect.Value) (kind, error) {
switch v.Kind() {
case reflect.Bool:
return boolKind, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intKind, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintKind, nil
case reflect.Float32, reflect.Float64:
return floatKind, nil
case reflect.Complex64, reflect.Complex128:
return complexKind, nil
case reflect.String:
return stringKind, nil
}
return invalidKind, errBadComparisonType
}
// sqlType2Type default sql type change to go types
func sqlType2Type(st schemas.SQLType) (rtype reflect.Type, rtstr string) {
name := strings.ToUpper(st.Name)
rtype = TypeOfString
switch name {
case schemas.Bool:
rtype = TypeOfBool
case schemas.Bit, schemas.UnsignedBit, schemas.TinyInt, schemas.UnsignedTinyInt:
if st.DefaultLength == 1 {
rtype = TypeOfBool
} else {
rtype = TypeOfInt
}
case schemas.SmallInt, schemas.MediumInt, schemas.Int, schemas.Integer, schemas.Serial,
schemas.UnsignedSmallInt, schemas.UnsignedMediumInt, schemas.UnsignedInt:
rtype = TypeOfInt
case schemas.BigInt, schemas.BigSerial, schemas.UnsignedBigInt:
rtype = TypeOfInt64
case schemas.Real, schemas.Float, UnsignedFloat, schemas.Double, UnsignedDouble:
rtype = TypeOfFloat64
case schemas.DateTime, schemas.Date, schemas.Time, schemas.TimeStamp,
schemas.TimeStampz, schemas.SmallDateTime, schemas.Year:
rtype = TypeOfTime
case schemas.TinyBlob, schemas.Blob, schemas.MediumBlob, schemas.LongBlob,
schemas.Bytea, schemas.Binary, schemas.VarBinary, schemas.UniqueIdentifier:
rtype, rtstr = TypeOfBytes, "[]byte"
case schemas.Varchar, schemas.NVarchar:
if st.DefaultLength > FixedStrMaxSize {
rtstr = "xutils.NullString"
}
case schemas.TinyText, schemas.Text,
schemas.NText, schemas.MediumText, schemas.LongText:
rtstr = "xutils.NullString"
// case schemas.Char, schemas.NChar, schemas.Enum, schemas.Set, schemas.Uuid, schemas.Clob, schemas.SysName:
// rtstr = rtype.String()2
// case schemas.Decimal, schemas.Numeric, schemas.Money, schemas.SmallMoney:
// rtstr = rtype.String()
}
if rtstr == "" {
rtstr = rtype.String()
}
return
}
func getCol(cols map[string]*schemas.Column, name string) *schemas.Column {
return cols[strings.ToLower(name)]
}
func getSinglePKey(table *schemas.Table) string {
if cols := table.PKColumns(); len(cols) == 1 {
return cols[0].FieldName
}
return ""
}
func getCreatedColumn(table *schemas.Table) string {
for name, ok := range table.Created {
if ok {
return table.GetColumn(name).Name
}
}
if col := table.GetColumn("created_at"); col != nil {
if col.SQLType.IsTime() {
return "created_at"
}
}
return ""
}
func trimAnyPrefix(word string, prefixes []string) string {
if len(prefixes) == 0 {
return word
}
size := len(word)
for _, pre := range prefixes {
word = strings.TrimPrefix(word, pre)
if len(word) < size { // 成功
return word
}
}
return word
}
func convertMapper(mapname string) names.Mapper {
switch mapname {
case "gonic":
return names.LintGonicMapper
case "same":
return names.SameMapper{}
default:
return names.SnakeMapper{}
}
}
func genNameSpace(targetDir string) string {
// 先重试提取已有代码文件(排除测试代码)的包名
files, err := filesystem.FindFiles(targetDir, ".go")
if err == nil && len(files) > 0 {
for filename := range files {
if strings.HasSuffix(filename, "_test.go") {
continue
}
cp, err := rewrite.NewFileParser(filename)
if err != nil {
continue
}
if nameSpace := cp.GetPackage(); nameSpace != "" {
return nameSpace
}
}
}
// 否则直接使用目录名,需要排除Golang关键词
nameSpace := strings.ToLower(filepath.Base(targetDir))
return aliasNameSpace(nameSpace)
}
func aliasNameSpace(nameSpace string) string {
if nameSpace == "default" { // it is golang keyword
nameSpace = "db"
} else if token.IsKeyword(nameSpace) {
nameSpace = "db" + nameSpace
}
return nameSpace
}
func genGoImports(tables map[string]*schemas.Table) map[string]string {
imports := make(map[string]string)
for _, table := range tables {
for _, col := range table.Columns() {
s := type2string(col)
if s == "time.Time" || s == "xutils.NullTime" {
imports["time"] = ""
}
if strings.HasPrefix(s, "xutils.Null") {
imports["github.com/azhai/xgen/utils"] = "xutils"
}
}
}
return imports
}
func type2string(col *schemas.Column) string {
t, s := sqlType2Type(col.SQLType)
if s == "string" && col.Nullable {
s = "xutils.NullString"
}
if t != TypeOfBool {
return s
}
if strings.HasPrefix(col.Name, "is_") ||
strings.HasPrefix(col.Name, "not_") {
return s
}
return TypeOfInt.String()
}
func tag2string(table *schemas.Table, col *schemas.Column, names ...string) string {
tx := tagXorm(table, col)
if len(names) == 0 {
return tx
}
var ts []string
for _, name := range names {
ts = append(ts, tagCustom(col, name))
}
if tx != "" {
ts = append(ts, tx)
}
return strings.Join(ts, " ")
}
func tagCustom(col *schemas.Column, name string) string {
if col.Name == "" {
return ""
}
return fmt.Sprintf(`%s:"%s"`, name, col.Name)
}
func tagXorm(table *schemas.Table, col *schemas.Column) string {
isNameId := col.FieldName == "Id"
isIdPk := isNameId && type2string(col) == "int64"
var res []string
if !col.Nullable {
if !isIdPk {
res = append(res, XormTagNotNull)
}
}
if col.IsPrimaryKey {
res = append(res, XormTagPrimaryKey)
}
if col.Default != "" {
res = append(res, "default "+col.Default)
}
if col.IsAutoIncrement {
res = append(res, XormTagAutoIncr)
}
if col.SQLType.IsTime() {
lowerName := strings.ToLower(col.Name)
if strings.HasPrefix(lowerName, "created") {
res = append(res, "created")
} else if strings.HasPrefix(lowerName, "updated") {
res = append(res, "updated")
} else if strings.HasPrefix(lowerName, "deleted") {
res = append(res, "deleted")
}
}
if comm := match.TruncateText(col.Comment, 50); comm != "" {
comm = template.HTMLEscapeString(comm) // 备注脱敏
res = append(res, fmt.Sprintf("comment('%s')", comm))
}
names := make([]string, 0, len(col.Indexes))
for name := range col.Indexes {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
index := table.Indexes[name]
var uistr string
if index.Type == schemas.UniqueType {
uistr = XormTagUnique
} else if index.Type == schemas.IndexType {
uistr = XormTagIndex
}
if len(index.Cols) > 1 {
uistr += "(" + index.Name + ")"
}
res = append(res, uistr)
}
res = append(res, GetColTypeString(col))
if len(res) > 0 {
tagValue := escapeTag(strings.Join(res, " ")) // 脱敏处理
return fmt.Sprintf(`%s:"%s"`, XormTagName, tagValue)
}
return ""
}
// GetColTypeString get the col type include length, for example: VARCHAR(255)
func GetColTypeString(col *schemas.Column) string {
ctstr := col.SQLType.Name
if col.Length != 0 {
if col.Length2 != 0 { // float, decimal
ctstr += fmt.Sprintf("(%v,%v)", col.Length, col.Length2)
} else { // int, char, varchar
ctstr += fmt.Sprintf("(%v)", col.Length)
}
} else if len(col.EnumOptions) > 0 { // enum
ctstr += "("
opts := ""
enumOptions := make([]string, 0, len(col.EnumOptions))
for enumOption := range col.EnumOptions {
enumOptions = append(enumOptions, enumOption)
}
sort.Strings(enumOptions)
for _, v := range enumOptions {
opts += fmt.Sprintf(",'%v'", v)
}
ctstr += strings.TrimLeft(opts, ",")
ctstr += ")"
} else if len(col.SetOptions) > 0 { // set
ctstr += "("
opts := ""
setOptions := make([]string, 0, len(col.SetOptions))
for setOption := range col.SetOptions {
setOptions = append(setOptions, setOption)
}
sort.Strings(setOptions)
for _, v := range setOptions {
opts += fmt.Sprintf(",'%v'", v)
}
ctstr += strings.TrimLeft(opts, ",")
ctstr += ")"
}
return ctstr
}