forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
89 lines (66 loc) · 2.22 KB
/
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
package model
import (
"gorm.io/gorm"
)
// SchemaNameOpt schema name option
type SchemaNameOpt func(*gorm.DB) string
var defaultSchemaNameOpt = SchemaNameOpt(func(db *gorm.DB) string { return db.Migrator().CurrentDatabase() })
// Option field option
type Option interface{ OptionType() string }
const fieldType = "field"
// FieldOption ...
type FieldOption interface {
Option
Operator() func(*Field) *Field
}
const methodType = "method"
// MethodOption ...
type MethodOption interface {
Option
Methods() (methods []interface{})
}
var (
_ Option = ModifyFieldOpt(nil)
_ Option = FilterFieldOpt(nil)
_ Option = CreateFieldOpt(nil)
_ Option = AddMethodOpt(nil)
)
// ModifyFieldOpt modify field option
type ModifyFieldOpt func(*Field) *Field
// OptionType implement for interface Option
func (ModifyFieldOpt) OptionType() string { return fieldType }
// Operator implement for FieldOpt
func (o ModifyFieldOpt) Operator() func(*Field) *Field { return o }
// FilterFieldOpt filter field option
type FilterFieldOpt ModifyFieldOpt
// OptionType implement for interface Option
func (FilterFieldOpt) OptionType() string { return fieldType }
// Operator implement for FieldOpt
func (o FilterFieldOpt) Operator() func(*Field) *Field { return o }
// CreateFieldOpt create field option
type CreateFieldOpt ModifyFieldOpt
// OptionType implement for interface Option
func (CreateFieldOpt) OptionType() string { return fieldType }
// Operator implement for FieldOpt
func (o CreateFieldOpt) Operator() func(*Field) *Field { return o }
// AddMethodOpt diy method option
type AddMethodOpt func() (methods []interface{})
// OptionType implement for interface Option
func (AddMethodOpt) OptionType() string { return methodType }
// Methods ...
func (o AddMethodOpt) Methods() []interface{} { return o() }
func sortOptions(opts []Option) (modifyOpts []FieldOption, filterOpts []FieldOption, createOpts []FieldOption, methodOpt []MethodOption) {
for _, opt := range opts {
switch opt := opt.(type) {
case ModifyFieldOpt:
modifyOpts = append(modifyOpts, opt)
case FilterFieldOpt:
filterOpts = append(filterOpts, opt)
case CreateFieldOpt:
createOpts = append(createOpts, opt)
case AddMethodOpt:
methodOpt = append(methodOpt, opt)
}
}
return
}