-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
113 lines (89 loc) · 2.3 KB
/
helpers.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
package utils
import (
"os"
"path"
"strings"
)
func (c *Config) getSQLBoilerModelsImportPath() string {
return path.Join(c.modFile.Module.Mod.Path, c.sqlBoilerConfig.Output)
}
func (c *Config) getSQLBoilerModelsFilePath(file string) string {
return path.Join(c.sqlBoilerConfig.Output, file)
}
func (c *Config) getERGDefaultImports(includeModelImports bool) []string {
var imports []string
if includeModelImports && !c.sqlBoilerConfig.Erg.Inline {
imports = append(imports, modelImports...)
}
imports = append(imports, c.getSQLBoilerModelsImportPath())
return imports
}
func (c *Config) isTSEnabled() bool {
return c.sqlBoilerConfig.Erg.OutputTS != "" || !strings.HasSuffix(c.sqlBoilerConfig.Erg.OutputTS, ".ts")
}
func (c *Config) isBlackListed(tn, cn string) bool {
for _, b := range c.sqlBoilerConfig.Erg.Blacklist {
// tn = true
if b == tn {
return true
}
// tn.cn = true
if b == tn+"."+cn {
return true
}
// *.cn = true
if b == "*."+cn {
return true
}
// *cn = cn ends with = true
if strings.HasPrefix(b, "*") && strings.HasSuffix(cn, strings.TrimPrefix(b, "*")) {
return true
}
// *.*cn = cn ends with = true
if strings.HasPrefix(b, "*.*") && strings.HasSuffix(cn, strings.TrimPrefix(b, "*.*")) {
return true
}
// tn.*cn = cn ends with = true
if strings.HasPrefix(b, tn+".*") && strings.HasSuffix(cn, strings.TrimPrefix(b, tn+".*")) {
return true
}
}
return false
}
func (c *Config) ConvertSQLBoilerModelsToApiModels() error {
tables, enums, err := c.getSQLBoilerTablesAndEnums()
if err != nil {
return err
}
if err := c.wipe(); err != nil {
return err
}
if err := c.writeERGHelperFunctionsToFile(); err != nil {
return err
}
if err := c.writeSQLBoilerEnumsToERGFiles(enums); err != nil {
return err
}
if err := c.writeSQLBoilerTablesToERGFiles(tables); err != nil {
return err
}
if err := c.writeSQLBoilerTablesToERGFile(tables); err != nil {
return err
}
if err := c.writeSQLBoilerTablesToTypeScriptFiles(tables, enums); err != nil {
return err
}
return nil
}
func (c *Config) wipe() error {
if !c.sqlBoilerConfig.Erg.Wipe {
return nil
}
if err := os.RemoveAll(c.sqlBoilerConfig.Erg.Output); err != nil {
return err
}
if err := os.MkdirAll(c.sqlBoilerConfig.Erg.Output, 0755); err != nil {
return err
}
return nil
}