-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_verify.go
100 lines (78 loc) · 2.13 KB
/
table_verify.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
// Package orm
package orm
import "fmt"
var globalVerifyObj Verify
func init() {
globalVerifyObj = newDefaultVerify()
}
func CloseVerify() {
globalVerifyObj = newCloseVerify()
}
func SetGlobalVerify(v Verify) {
if v == nil {
panic("verify cannot nil")
}
globalVerifyObj = v
}
type Verify interface {
VerifyTableName(name string) error
VerifyFieldName(name string) error
VerifyTagName(name string) error
}
type closeVerify struct {
}
func (c closeVerify) VerifyTableName(name string) error {
return nil
}
func (c closeVerify) VerifyFieldName(name string) error {
return nil
}
func (c closeVerify) VerifyTagName(name string) error {
return nil
}
func newCloseVerify() Verify {
v := new(closeVerify)
return v
}
type defaultVerify struct {
}
func (d *defaultVerify) VerifyTagName(name string) error {
if len(name) > 64 {
return fmt.Errorf("the tag name[%s] is too long. the maximum requirement is 64", name)
}
for _, ch := range name {
if !(ch == '_' || ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) {
return fmt.Errorf("tag name[%s] cannot have \"%c\", "+
"customize verify as needed to achieve interface \"Verify\"", name, ch)
}
}
return nil
}
func (d *defaultVerify) VerifyTableName(name string) error {
if len(name) > 64 {
return fmt.Errorf("the table name[%s] is too long. the maximum requirement is 64", name)
}
for _, ch := range name {
if !(ch == '_' || ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) {
return fmt.Errorf("table name[%s] cannot have \"%c\", "+
"customize verify as needed to achieve interface \"Verify\"", name, ch)
}
}
return nil
}
func (d *defaultVerify) VerifyFieldName(name string) error {
if len(name) > 64 {
return fmt.Errorf("the field name[%s] is too long. the maximum requirement is 64", name)
}
for _, ch := range name {
if !(ch == '_' || ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) {
return fmt.Errorf("field name[%s] cannot have \"%c\", "+
"customize verify as needed to achieve interface \"Verify\"", name, ch)
}
}
return nil
}
func newDefaultVerify() Verify {
v := new(defaultVerify)
return v
}