-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
120 lines (98 loc) · 2.77 KB
/
template.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
package generator
import (
"text/template"
)
var qsTmpl = template.Must(
template.New("generator").
Parse(qsCode),
)
const qsCode = `
// ===== BEGIN of all query sets
{{ range .Configs }}
{{ $ft := printf "%s%s" .StructName "DBSchemaField" }}
// ===== BEGIN of query set {{ .Name }}
// {{ .Name }} is an queryset type for {{ .StructName }}
type {{ .Name }} struct {
db *gorm.DB
}
// New{{ .Name }} constructs new {{ .Name }}
func New{{ .Name }}(db *gorm.DB) {{ .Name }} {
return {{ .Name }}{
db: db.Model(&{{ .StructName }}{}),
}
}
func (qs {{ .Name }}) w(db *gorm.DB) {{ .Name }} {
return New{{ .Name }}(db)
}
func (qs {{ .Name }}) Select(fields ...{{ $ft }}) {{ .Name }} {
names := []string{}
for _, f := range fields {
names = append(names, f.String())
}
return qs.w(qs.db.Select(strings.Join(names, ",")))
}
{{ range .Methods }}
{{ .GetDoc .GetMethodName }}
func ({{ .GetReceiverDeclaration }}) {{ .GetMethodName }}({{ .GetArgsDeclaration }})
{{- .GetReturnValuesDeclaration }} {
{{ .GetBody }}
}
{{ end }}
// ===== END of query set {{ .Name }}
// ===== BEGIN of {{ .StructName }} modifiers
// {{ $ft }} describes database schema field. It requires for method 'Update'
type {{ $ft }} string
// String method returns string representation of field.
// nolint: dupl
func (f {{ $ft }}) String() string {
return string(f)
}
// {{ .StructName }}DBSchema stores db field names of {{ .StructName }}
var {{ .StructName }}DBSchema = struct {
{{ range .Fields }}
{{ .Name }} {{ $ft }}
{{- end }}
}{
{{ range .Fields }}
{{ .Name }}: {{ $ft }}("{{ .DBName }}"),
{{- end }}
}
// Update updates {{ .StructName }} fields by primary key
// nolint: dupl
func (o *{{ .StructName }}) Update(db *gorm.DB, fields ...{{ $ft }}) error {
dbNameToFieldName := map[string]interface{}{
{{- range .Fields }}
"{{ .DBName }}": o.{{ .Name }},
{{- end }}
}
u := map[string]interface{}{}
for _, f := range fields {
fs := f.String()
u[fs] = dbNameToFieldName[fs]
}
if err := db.Model(o).Updates(u).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return err
}
return fmt.Errorf("can't update {{ .StructName }} %v fields %v: %s",
o, fields, err)
}
return nil
}
// {{ .StructName }}Updater is an {{ .StructName }} updates manager
type {{ .StructName }}Updater struct {
fields map[string]interface{}
db *gorm.DB
}
// New{{ .StructName }}Updater creates new {{ .StructName }} updater
// nolint: dupl
func New{{ .StructName }}Updater(db *gorm.DB) {{ .StructName }}Updater {
return {{ .StructName }}Updater{
fields: map[string]interface{}{},
db: db.Model(&{{ .StructName }}{}),
}
}
// ===== END of {{ .StructName }} modifiers
{{ end }}
// ===== END of all query sets
`