-
Notifications
You must be signed in to change notification settings - Fork 11
/
binder_formatter.go
55 lines (51 loc) · 1.39 KB
/
binder_formatter.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
package echo
import "unicode"
type (
//FieldNameFormatter 结构体字段值映射到表单时,结构体字段名称格式化处理
FieldNameFormatter func(topName, fieldName string) string
)
var (
//DefaultFieldNameFormatter 默认格式化函数(struct->form)
DefaultFieldNameFormatter FieldNameFormatter = func(topName, fieldName string) string {
var fName string
if len(topName) == 0 {
fName = fieldName
} else {
fName = topName + "." + fieldName
}
return fName
}
//ArrayFieldNameFormatter 格式化函数(struct->form)
ArrayFieldNameFormatter FieldNameFormatter = func(topName, fieldName string) string {
var fName string
if len(topName) == 0 {
fName = fieldName
} else {
fName = topName + `[` + fieldName + `]`
}
return fName
}
//LowerCaseFirstLetter 小写首字母(struct->form)
LowerCaseFirstLetter FieldNameFormatter = func(topName, fieldName string) string {
var fName string
s := []rune(fieldName)
if len(s) > 0 {
s[0] = unicode.ToLower(s[0])
fieldName = string(s)
}
if len(topName) == 0 {
fName = fieldName
} else {
fName = topName + "." + fieldName
}
return fName
}
)
func MakeArrayFieldNameFormatter(keyFormatter func(string) string) FieldNameFormatter {
return func(topName, fieldName string) string {
if keyFormatter != nil {
fieldName = keyFormatter(fieldName)
}
return ArrayFieldNameFormatter(topName, fieldName)
}
}