/
option.go
123 lines (106 loc) · 2.2 KB
/
option.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
package parser
// NullStyle null type
type NullStyle int
// nolint
const (
NullDisable NullStyle = iota
NullInSql
NullInPointer
)
// Option function
type Option func(*options)
type options struct {
Charset string
Collation string
JSONTag bool
JSONNamedType int // json命名类型,0:默认,其他值表示驼峰
TablePrefix string
ColumnPrefix string
NoNullType bool
NullStyle NullStyle
Package string
GormType bool
ForceTableName bool
IsEmbed bool // 是否嵌入gorm.Model
}
var defaultOptions = options{
NullStyle: NullInSql,
Package: "model",
}
// WithCharset set charset
func WithCharset(charset string) Option {
return func(o *options) {
o.Charset = charset
}
}
// WithCollation set collation
func WithCollation(collation string) Option {
return func(o *options) {
o.Collation = collation
}
}
// WithTablePrefix set table prefix
func WithTablePrefix(p string) Option {
return func(o *options) {
o.TablePrefix = p
}
}
// WithColumnPrefix set column prefix
func WithColumnPrefix(p string) Option {
return func(o *options) {
o.ColumnPrefix = p
}
}
// WithJSONTag json名称命名类型,0:表示默认,其他值表示驼峰
func WithJSONTag(namedType int) Option {
return func(o *options) {
o.JSONTag = true
o.JSONNamedType = namedType
}
}
// WithNoNullType set NoNullType
func WithNoNullType() Option {
return func(o *options) {
o.NoNullType = true
}
}
// WithNullStyle set NullType
func WithNullStyle(s NullStyle) Option {
return func(o *options) {
o.NullStyle = s
}
}
// WithPackage set package name
func WithPackage(pkg string) Option {
return func(o *options) {
o.Package = pkg
}
}
// WithGormType will write type in gorm tag
func WithGormType() Option {
return func(o *options) {
o.GormType = true
}
}
// WithForceTableName set forceFloats
func WithForceTableName() Option {
return func(o *options) {
o.ForceTableName = true
}
}
// WithEmbed is embed gorm.Model
func WithEmbed() Option {
return func(o *options) {
o.IsEmbed = true
}
}
func parseOption(options []Option) options {
o := defaultOptions
for _, f := range options {
f(&o)
}
if o.NoNullType {
o.NullStyle = NullDisable
}
return o
}