/
schema.go
127 lines (101 loc) · 2.78 KB
/
schema.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
package generate
import (
"bytes"
"context"
"fmt"
"github.com/gin-admin/gin-admin-cli/v5/util"
)
type schemaField struct {
Name string
Comment string
Type string
IsRequired bool
BindingOptions string
}
func getSchemaFileName(dir, name string) string {
fullname := fmt.Sprintf("%s/internal/app/schema/%s.go", dir, util.ToLowerUnderlinedNamer(name))
return fullname
}
func genSchema(ctx context.Context, pkgName, dir, name, comment string, excludeStatus, excludeCreate bool, fields ...schemaField) error {
var tfields []schemaField
tfields = append(tfields, schemaField{Name: "ID", Type: "uint64"})
tfields = append(tfields, fields...)
if !excludeStatus {
tfields = append(tfields, schemaField{Name: "Status", Comment: "1:enable,2:disable", Type: "int"})
}
if !excludeCreate {
tfields = append(tfields, schemaField{Name: "Creator", Type: "uint64"})
}
tfields = append(tfields, schemaField{Name: "CreatedAt", Type: "time.Time"})
tfields = append(tfields, schemaField{Name: "UpdatedAt", Type: "time.Time"})
buf := new(bytes.Buffer)
for _, field := range tfields {
buf.WriteString(fmt.Sprintf("%s \t %s \t", field.Name, field.Type))
buf.WriteByte('`')
if field.Name == "ID" {
buf.WriteString(fmt.Sprintf(`json:"%s,string"`, util.ToLowerUnderlinedNamer(field.Name)))
} else {
buf.WriteString(fmt.Sprintf(`json:"%s"`, util.ToLowerUnderlinedNamer(field.Name)))
}
bindingOpts := ""
if field.IsRequired {
bindingOpts = "required"
}
if v := field.BindingOptions; v != "" {
if bindingOpts != "" {
bindingOpts += ","
}
bindingOpts = bindingOpts + v
}
if bindingOpts != "" {
buf.WriteByte(' ')
buf.WriteString(fmt.Sprintf(`binding:"%s"`, bindingOpts))
}
buf.WriteByte('`')
if field.Comment != "" {
buf.WriteString(fmt.Sprintf("// %s", field.Comment))
}
buf.WriteString(delimiter)
}
tbuf, err := execParseTpl(schemaTpl, map[string]interface{}{
"PkgName": pkgName,
"Name": name,
"PluralName": util.ToPlural(name),
"Fields": buf.String(),
"Comment": comment,
})
if err != nil {
return err
}
fullname := getSchemaFileName(dir, name)
err = createFile(ctx, fullname, tbuf)
if err != nil {
return err
}
fmt.Printf("File write success: %s\n", fullname)
return execGoFmt(fullname)
}
const schemaTpl = `
package schema
import "time"
// {{.Comment}}
type {{.Name}} struct {
{{.Fields}}
}
// Query parameters for db
type {{.Name}}QueryParam struct {
PaginationParam
}
// Query options for db (order or select fields)
type {{.Name}}QueryOptions struct {
OrderFields []*OrderField
SelectFields []string
}
// Query result from db
type {{.Name}}QueryResult struct {
Data {{.PluralName}}
PageResult *PaginationResult
}
// {{.Comment}} Object List
type {{.PluralName}} []*{{.Name}}
`