-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory.go
More file actions
94 lines (77 loc) · 2.03 KB
/
factory.go
File metadata and controls
94 lines (77 loc) · 2.03 KB
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
package generators
import (
"os"
"strings"
"github.com/gertd/go-pluralize"
"github.com/iancoleman/strcase"
"golang.org/x/mod/modfile"
)
type (
Generator interface {
Generate(template Template, modulePath string, driver string)
}
Template struct {
ApiPrefix string
PackageName string
Module string
ModuleLowercase string
ModulePlural string
ModulePluralLowercase string
Columns []FieldTemplate
}
ModuleJson struct {
Name string `json:"name"`
Url string `json:"url"`
}
ModuleTemplate struct {
Name string
Fields []FieldTemplate
}
FieldTemplate struct {
Name string
NameUnderScore string
ProtobufType string
GolangType string
Index int
IsRequired bool
}
Factory struct {
ApiPrefix string
Driver string
Pluralizer pluralize.Client
Template Template
Generators []Generator
}
)
func (f *Factory) Generate(module ModuleTemplate) {
workDir, _ := os.Getwd()
packageName := f.packageName(workDir)
moduleName := strcase.ToCamel(module.Name)
modulePlural := f.Pluralizer.Plural(module.Name)
modulePluralLowercase := strcase.ToDelimited(modulePlural, '_')
var modulePath strings.Builder
modulePath.WriteString(workDir)
modulePath.WriteString("/")
modulePath.WriteString(modulePluralLowercase)
f.Template.ApiPrefix = f.ApiPrefix
f.Template.PackageName = packageName
f.Template.Module = moduleName
f.Template.ModuleLowercase = strcase.ToDelimited(module.Name, '_')
f.Template.ModulePlural = modulePlural
f.Template.ModulePluralLowercase = modulePluralLowercase
f.Template.Columns = module.Fields
os.MkdirAll(modulePath.String(), 0755)
for _, generator := range f.Generators {
generator.Generate(f.Template, modulePath.String(), f.Driver)
}
}
func (f *Factory) packageName(workDir string) string {
var path strings.Builder
path.WriteString(workDir)
path.WriteString("/go.mod")
mod, err := os.ReadFile(path.String())
if err != nil {
panic(err)
}
return modfile.ModulePath(mod)
}