-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
96 lines (83 loc) · 1.98 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
90
91
92
93
94
95
96
package ngin
import "net/http"
// SessionCurrentAccount ...
const SessionCurrentAccount = "current_account"
// Options 可选参数列表
type Options struct {
StatusCode int
Layout string
PageName string
Template string
Pjax bool
PjaxLayout string
GlobalVariable map[string]interface{}
GlobalConstant map[string]interface{}
SessionCurrentAccountKey string
}
// newOptions 创建可选参数
func newOptions(opts ...Option) Options {
opt := Options{
StatusCode: http.StatusOK,
Pjax: false,
PjaxLayout: "pjax_layout.tmpl",
Layout: "layout.tmpl",
Template: "default",
SessionCurrentAccountKey: SessionCurrentAccount,
GlobalVariable: map[string]interface{}{},
GlobalConstant: map[string]interface{}{},
}
for _, o := range opts {
o(&opt)
}
return opt
}
// Option 为可选参数赋值的函数
type Option func(*Options)
// StatusCode ...
func StatusCode(statusCode int) Option {
return func(o *Options) {
o.StatusCode = statusCode
}
}
// Layout ...
func Layout(layout string) Option {
return func(o *Options) {
o.Layout = layout
}
}
// PageName ...
func PageName(pageName string) Option {
return func(o *Options) {
o.PageName = pageName
}
}
// Template ...
func Template(template string) Option {
return func(o *Options) {
o.Template = template
}
}
// GlobalVariable ...
func GlobalVariable(variable map[string]interface{}) Option {
return func(o *Options) {
o.GlobalVariable = variable
}
}
// GlobalConstant ...
func GlobalConstant(constant map[string]interface{}) Option {
return func(o *Options) {
o.GlobalConstant = constant
}
}
// Pjax ...
func Pjax(pjax bool) Option {
return func(o *Options) {
o.Pjax = pjax
}
}
// PjaxLayout ...
func PjaxLayout(pjaxLayout string) Option {
return func(o *Options) {
o.PjaxLayout = pjaxLayout
}
}