forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
243 lines (202 loc) · 5.75 KB
/
display.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package types
import (
"fmt"
"html"
"html/template"
"strings"
)
type DisplayFnGenerator interface {
Get(args ...interface{}) FieldFilterFn
JS() template.HTML
HTML() template.HTML
}
type BaseDisplayFnGenerator struct{}
func (base *BaseDisplayFnGenerator) JS() template.HTML { return "" }
func (base *BaseDisplayFnGenerator) HTML() template.HTML { return "" }
var displayFnGens = make(map[string]DisplayFnGenerator)
func RegisterDisplayFnGenerator(key string, gen DisplayFnGenerator) {
if _, ok := displayFnGens[key]; ok {
panic("display function generator has been registered")
}
displayFnGens[key] = gen
}
type FieldDisplay struct {
Display FieldFilterFn
DisplayProcessChains DisplayProcessFnChains
}
func (f FieldDisplay) ToDisplay(value FieldModel) interface{} {
val := f.Display(value)
if _, ok := val.(template.HTML); !ok {
if _, ok2 := val.([]string); !ok2 {
valStr := fmt.Sprintf("%v", val)
for _, process := range f.DisplayProcessChains {
valStr = process(valStr)
}
return valStr
}
}
return val
}
func (f FieldDisplay) AddLimit(limit int) DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
if limit > len(value) {
return value
} else if limit < 0 {
return ""
} else {
return value[:limit]
}
})
}
func (f FieldDisplay) AddTrimSpace() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.TrimSpace(value)
})
}
func (f FieldDisplay) AddSubstr(start int, end int) DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
if start > end || start > len(value) || end < 0 {
return ""
}
if start < 0 {
start = 0
}
if end > len(value) {
end = len(value)
}
return value[start:end]
})
}
func (f FieldDisplay) AddToTitle() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.Title(value)
})
}
func (f FieldDisplay) AddToUpper() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.ToUpper(value)
})
}
func (f FieldDisplay) AddToLower() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.ToLower(value)
})
}
type DisplayProcessFn func(string) string
type DisplayProcessFnChains []DisplayProcessFn
func (d DisplayProcessFnChains) Valid() bool {
return len(d) > 0
}
func (d DisplayProcessFnChains) Add(f DisplayProcessFn) DisplayProcessFnChains {
return append(d, f)
}
func (d DisplayProcessFnChains) Append(f DisplayProcessFnChains) DisplayProcessFnChains {
return append(d, f...)
}
func (d DisplayProcessFnChains) Copy() DisplayProcessFnChains {
if len(d) == 0 {
return make(DisplayProcessFnChains, 0)
} else {
var newDisplayProcessFnChains = make(DisplayProcessFnChains, len(d))
copy(newDisplayProcessFnChains, d)
return newDisplayProcessFnChains
}
}
func chooseDisplayProcessChains(internal DisplayProcessFnChains) DisplayProcessFnChains {
if len(internal) > 0 {
return internal
}
return globalDisplayProcessChains.Copy()
}
var globalDisplayProcessChains = make(DisplayProcessFnChains, 0)
func AddGlobalDisplayProcessFn(f DisplayProcessFn) {
globalDisplayProcessChains = globalDisplayProcessChains.Add(f)
}
func AddLimit(limit int) DisplayProcessFnChains {
return addLimit(limit, globalDisplayProcessChains)
}
func AddTrimSpace() DisplayProcessFnChains {
return addTrimSpace(globalDisplayProcessChains)
}
func AddSubstr(start int, end int) DisplayProcessFnChains {
return addSubstr(start, end, globalDisplayProcessChains)
}
func AddToTitle() DisplayProcessFnChains {
return addToTitle(globalDisplayProcessChains)
}
func AddToUpper() DisplayProcessFnChains {
return addToUpper(globalDisplayProcessChains)
}
func AddToLower() DisplayProcessFnChains {
return addToLower(globalDisplayProcessChains)
}
func AddXssFilter() DisplayProcessFnChains {
return addXssFilter(globalDisplayProcessChains)
}
func AddXssJsFilter() DisplayProcessFnChains {
return addXssJsFilter(globalDisplayProcessChains)
}
func addLimit(limit int, chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
if limit > len(value) {
return value
} else if limit < 0 {
return ""
} else {
return value[:limit]
}
})
return chains
}
func addTrimSpace(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
return strings.TrimSpace(value)
})
return chains
}
func addSubstr(start int, end int, chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
if start > end || start > len(value) || end < 0 {
return ""
}
if start < 0 {
start = 0
}
if end > len(value) {
end = len(value)
}
return value[start:end]
})
return chains
}
func addToTitle(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
return strings.Title(value)
})
return chains
}
func addToUpper(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
return strings.ToUpper(value)
})
return chains
}
func addToLower(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
return strings.ToLower(value)
})
return chains
}
func addXssFilter(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
return html.EscapeString(value)
})
return chains
}
func addXssJsFilter(chains DisplayProcessFnChains) DisplayProcessFnChains {
chains = chains.Add(func(value string) string {
replacer := strings.NewReplacer("<script>", "<script>", "</script>", "</script>")
return replacer.Replace(value)
})
return chains
}