-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.go
109 lines (95 loc) · 2.85 KB
/
strings.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
package addons
import (
"gopkg.in/check.v1"
"strings"
)
type contains struct {
*check.CheckerInfo
}
var Contains check.Checker = &contains{
&check.CheckerInfo{
Name: "Contains",
Params: []string{"value", "contained"},
},
}
func (checker *contains) checkString(value string, rawContained interface{}) (bool, string) {
if contained, ok := rawContained.(string); ok {
return strings.Contains(value, contained), ""
}
if contained, ok := rawContained.(hasStringMethod); ok {
return strings.Contains(value, contained.String()), ""
}
return false, "For string values only string contained values supported."
}
func (checker *contains) Check(params []interface{}, names []string) (bool, string) {
if len(params) != 2 {
panic("Illegal number of parameters.")
}
if value, ok := params[0].(string); ok {
return checker.checkString(value, params[1])
}
if value, ok := params[0].(hasStringMethod); ok {
return checker.checkString(value.String(), params[1])
}
return false, "No compatible value."
}
type hasPrefix struct {
*check.CheckerInfo
}
var HasPrefix check.Checker = &hasPrefix{
&check.CheckerInfo{
Name: "HasPrefix",
Params: []string{"value", "prefix"},
},
}
func (checker *hasPrefix) checkString(value string, rawPrefix interface{}) (bool, string) {
if prefix, ok := rawPrefix.(string); ok {
return strings.HasPrefix(value, prefix), ""
}
if prefix, ok := rawPrefix.(hasStringMethod); ok {
return strings.HasPrefix(value, prefix.String()), ""
}
return false, "For string values only string prefix values supported."
}
func (checker *hasPrefix) Check(params []interface{}, names []string) (bool, string) {
if len(params) != 2 {
panic("Illegal number of parameters.")
}
if value, ok := params[0].(string); ok {
return checker.checkString(value, params[1])
}
if value, ok := params[0].(hasStringMethod); ok {
return checker.checkString(value.String(), params[1])
}
return false, "No compatible value."
}
type hasSuffix struct {
*check.CheckerInfo
}
var HasSuffix check.Checker = &hasSuffix{
&check.CheckerInfo{
Name: "HasSuffix",
Params: []string{"value", "suffix"},
},
}
func (checker *hasSuffix) checkString(value string, rawSuffix interface{}) (bool, string) {
if suffix, ok := rawSuffix.(string); ok {
return strings.HasSuffix(value, suffix), ""
}
if suffix, ok := rawSuffix.(hasStringMethod); ok {
return strings.HasSuffix(value, suffix.String()), ""
}
return false, "For string values only string suffix values supported."
}
func (checker *hasSuffix) Check(params []interface{}, names []string) (bool, string) {
if len(params) != 2 {
panic("Illegal number of parameters.")
}
if value, ok := params[0].(string); ok {
return checker.checkString(value, params[1])
}
if value, ok := params[0].(hasStringMethod); ok {
return checker.checkString(value.String(), params[1])
}
return false, "No compatible value."
}