-
Notifications
You must be signed in to change notification settings - Fork 4
/
export_options.go
111 lines (89 loc) · 2.85 KB
/
export_options.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
package gen
import (
"fmt"
"io/fs"
"path/filepath"
"strings"
"github.com/kataras/pg/desc"
)
// ExportOptions is the options for the GenerateColumnsFromSchema function.
type ExportOptions struct {
RootDir string
FileMode fs.FileMode
ToSingular func(string) string
GetFileName func(rootDir, tableName string) string
GetPackageName func(tableName string) string
}
func EachTableToItsOwnPackage(rootDir, tableName string) string {
if strings.HasSuffix(tableName, ".go") {
return filepath.Join(rootDir, tableName)
}
packageName := desc.Singular(tableName)
filename := filepath.Join(rootDir, packageName, packageName+".go")
return filename
}
func EachTableGroupToItsOwnPackage() func(rootDir, tableName string) string {
visitedTables := make(map[string]struct{}) // table group.
getTableGroup := func(rootDir, tableName string) string {
tableName = desc.Singular(tableName)
for t := range visitedTables {
if strings.HasPrefix(tableName, t+"_") {
return t
}
}
visitedTables[tableName] = struct{}{}
return tableName
}
return func(rootDir, tableName string) string {
if strings.HasSuffix(tableName, ".go") {
return filepath.Join(rootDir, tableName)
}
tableGroup := getTableGroup(rootDir, tableName)
return filepath.Join(rootDir, tableGroup, desc.Singular(tableName)+".go")
}
}
func (opts *ExportOptions) apply() error {
if opts.RootDir == "" {
opts.RootDir = "./"
}
if opts.FileMode <= 0 {
opts.FileMode = 0777
}
rootDir, err := filepath.Abs(opts.RootDir)
if err != nil {
return fmt.Errorf("filepath.Abs: %w", err)
}
opts.RootDir = rootDir // we need the fullpath in order to find the package name if missing.
if opts.ToSingular == nil {
opts.ToSingular = desc.Singular
}
if opts.GetFileName == nil {
opts.GetFileName = func(rootDir, tableName string) string {
filename := tableName
if filename == "" { // if empty default the filename to the last part of the root dir +.go.
filename = strings.TrimPrefix(filepath.Base(rootDir), "_")
} else if strings.HasSuffix(filename, ".go") {
return filepath.Join(rootDir, filename)
} else { // otherwise get the singular form of the tablename + .go.
filename = opts.ToSingular(tableName)
}
filename = filepath.Join(rootDir, filename)
return fmt.Sprintf("%s.go", filename)
}
}
if opts.GetPackageName == nil {
opts.GetPackageName = func(tableName string) string {
if tableName == "" {
return strings.TrimPrefix(filepath.Base(opts.RootDir), "_")
}
filename := opts.GetFileName(opts.RootDir, tableName) // contains the full path let's get the last part of it as package name.
packageName := filepath.Base(filepath.Dir(filename))
packageName = strings.TrimPrefix(packageName, "_")
if packageName == "" {
packageName = filepath.Base(opts.RootDir) // else it's current dir.
}
return packageName
}
}
return nil
}