-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
94 lines (85 loc) · 1.76 KB
/
controller.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
package options
import "time"
type Controller interface {
OutputController
FormatController
}
type OutputController interface {
OutputLevel(lvl string) string
OutputCursor(cursor string) string
OutputTimestamp(t *time.Time) *time.Time
OutputMessage(msg interface{}) interface{}
OutputData(data interface{}) interface{}
OutputStructName(sn string) string
IsFormatMsgRowLimitted() bool
LimitRowNum() int
}
type FormatController interface {
IsFormatReadable() bool
IsFormatJson() bool
}
type Child interface {
apply(*controller) *controller
}
type controller struct {
output outputOption
format formatOptions
}
func NewController(options ...Child) Controller {
c := &controller{
output: NewOutputOption(),
format: NewFormatOption(),
}
for i := range options {
options[i].apply(c)
}
return c
}
func (o *controller) OutputLevel(lvl string) string {
if o.output.level {
return lvl
}
return lvl
}
func (o *controller) OutputCursor(cursor string) string {
if o.output.cursor {
return cursor
}
return ""
}
func (o *controller) OutputTimestamp(t *time.Time) *time.Time {
if o.output.timestamp {
return t
}
return nil
}
func (o *controller) OutputMessage(msg interface{}) interface{} {
if o.output.message {
return msg
}
return ""
}
func (o *controller) OutputData(data interface{}) interface{} {
if o.output.data {
return data
}
return ""
}
func (o *controller) OutputStructName(sn string) string {
if o.output.structName {
return sn
}
return ""
}
func (o *controller) IsFormatJson() bool {
return o.format.json
}
func (o *controller) IsFormatReadable() bool {
return o.format.readable
}
func (o *controller) IsFormatMsgRowLimitted() bool {
return o.format.msgRowsLimit > 0
}
func (o *controller) LimitRowNum() int {
return o.format.msgRowsLimit
}