-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
170 lines (138 loc) · 5.27 KB
/
convert.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
package assertion
import (
"encoding/base64"
"fmt"
"strconv"
)
// Boolean returns true if a given string is one of the following accepted forms:
// true, false, TRUE, FALSE, t, f, 1, or 0
func (a *Assertion) Boolean(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseBool(value); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "boolean string"), msgArgs...)
return false
}
// Truthy returns true if a given string is one of the following accepted forms:
// true, TRUE, t, or 1
func (a *Assertion) Truthy(value string, msgArgs ...interface{}) bool {
if b, err := strconv.ParseBool(value); err == nil {
return b
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "truthy string"), msgArgs...)
return false
}
// Falsy returns true if a given string is one of the following accepted forms:
// true, false, TRUE, FALSE, t, f, 1, and 0
func (a *Assertion) Falsy(value string, msgArgs ...interface{}) bool {
if b, err := strconv.ParseBool(value); err == nil {
return !b
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "falsy string"), msgArgs...)
return false
}
// Integer returns true if a given string can be parsed as a valid integer value
func (a *Assertion) Integer(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseInt(value, 0, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "integer"), msgArgs...)
return false
}
// IntegerBinary returns true if a given string can be parsed as a valid integer value
// in base 2
func (a *Assertion) IntegerBinary(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseInt(value, 2, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-2 integer"), msgArgs...)
return false
}
// IntegerOctal returns true if a given string can be parsed as a valid integer value
// in base 8
func (a *Assertion) IntegerOctal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseInt(value, 8, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-8 integer"), msgArgs...)
return false
}
// IntegerHexadecimal returns true if a given string can be parsed as a valid integer value
// in base 16
func (a *Assertion) IntegerHexadecimal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseInt(value, 16, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-16 integer"), msgArgs...)
return false
}
// IntegerDecimal returns true if a given string can be parsed as a valid integer value
// in base 10
func (a *Assertion) IntegerDecimal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-10 integer"), msgArgs...)
return false
}
// Unsigned returns true if a given string can be parsed as a valid unsigned integer value
func (a *Assertion) Unsigned(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseUint(value, 0, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "unsigned integer"), msgArgs...)
return false
}
// UnsignedBinary returns true if a given string can be parsed as a valid unsigned integer value
// in base 2
func (a *Assertion) UnsignedBinary(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseUint(value, 2, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-2 unsigned integer"), msgArgs...)
return false
}
// UnsignedOctal returns true if a given string can be parsed as a valid unsigned integer value
// in base 8
func (a *Assertion) UnsignedOctal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseUint(value, 8, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-8 unsigned integer"), msgArgs...)
return false
}
// UnsignedHexadecimal returns true if a given string can be parsed as a valid unsigned integer value
// in base 16
func (a *Assertion) UnsignedHexadecimal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseUint(value, 16, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-16 unsigned integer"), msgArgs...)
return false
}
// UnsignedDecimal returns true if a given string can be parsed as a valid unsigned integer value
// in base 10
func (a *Assertion) UnsignedDecimal(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseUint(value, 10, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base-10 unsigned integer"), msgArgs...)
return false
}
// Float returns true if a given string can be parsed as a valid float value
func (a *Assertion) Float(value string, msgArgs ...interface{}) bool {
if _, err := strconv.ParseFloat(value, 64); err == nil {
return true
}
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "float"), msgArgs...)
return false
}
// Base64 returns true if a given value ia a valid base64 encoded string
func (a *Assertion) Base64(value string, msgArgs ...interface{}) bool {
_, err := base64.StdEncoding.DecodeString(value)
if err != nil {
a.addErrorMsg(fmt.Sprintf(errMsgNotValid, value, "base64 encoded value"), msgArgs...)
return false
}
return true
}