-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmust.go
203 lines (179 loc) · 6.69 KB
/
must.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
package must
import (
"github.com/pkg/errors"
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)
// True expects the value to be true. Panics if the value is false.
// True 期望值为 true。如果值为 false,则触发 panic。
func True(v bool) {
if !v {
zaplog.ZAPS.P1.LOG.Panic("expect TRUE while got FALSE", zap.Bool("v", v))
}
}
// Done expects no error. Panics if the provided error is non-nil.
// Done 期望没有错误。如果提供的错误不为 nil,则触发 panic。
func Done(err error) {
if err != nil {
zaplog.ZAPS.P1.LOG.Panic("NO ERROR BUG", zap.Error(err))
}
}
// Must expects no error. Panics if the provided error is non-nil.
// Must 期望没有错误。如果提供的错误不为 nil,则触发 panic。
func Must(err error) {
if err != nil {
zaplog.ZAPS.P1.LOG.Panic("ERROR", zap.Error(err))
}
}
// Nice expects a non-zero value. Panics if the value is zero, returns the value if non-zero.
// Nice 期望一个非零值。如果值为零,则触发 panic;如果值非零,则返回该值。
func Nice[V comparable](a V) V {
var b V // zero value
if a == b {
zaplog.ZAPS.P1.LOG.Panic("A IS ZERO VALUE", zap.Any("a", a))
}
return a
}
// Zero expects a zero value. Panics if the value is non-zero.
// Zero 期望值为零。如果值不为零,则触发 panic。
func Zero[V comparable](a V) {
var b V // zero value
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A IS NOT ZERO VALUE", zap.Any("a", a))
}
}
// None expects a zero value (empty/none). Panics if the value is non-zero.
// None 期望值为零(空)。如果值不为零,则触发 panic。
func None[V comparable](a V) {
var b V // zero value
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A IS NOT NONE VALUE", zap.Any("a", a))
}
}
// Null expects the value to be nil. Panics if the value is non-nil.
// Null 期望值为 nil。如果值不为 nil,则触发 panic。
func Null[T any](v *T) {
if v != nil {
zaplog.ZAPS.P1.LOG.Panic("SHOULD BE NULL BUT IS FULL")
}
}
// Full expects the value to be non-nil. Panics if the value is nil.
// Full 期望值为非 nil。如果值为 nil,则触发 panic。
func Full[T any](v *T) {
if v == nil {
zaplog.ZAPS.P1.LOG.Panic("SHOULD BE FULL BUT IS NULL")
}
}
// Equal expects the values to be equal. Panics if they are not equal.
// Equal 期望值相等。如果值不相等,则触发 panic。
func Equal[V comparable](a, b V) {
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A AND B ARE NOT EQUAL", zap.Any("a", a), zap.Any("b", b))
}
}
// Equals expects the values to be equal. Panics if they are not equal.
// Equals 期望值相等。如果值不相等,则触发 panic。
func Equals[V comparable](a, b V) {
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A AND B ARE NOT EQUALS", zap.Any("a", a), zap.Any("b", b))
}
}
// Same expects the values to be same. Panics if they are not same.
// Same 期望值相等。如果值不相等,则触发 panic。
func Same[V comparable](a, b V) {
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A AND B ARE NOT SAME", zap.Any("a", a), zap.Any("b", b))
}
}
// Is expects equality, not the logic of errors.Is, but the logic of Equals. Panics if the values are not equal.
// Is 期望相等,不是 errors.Is 的逻辑,而是 Equals 的逻辑。如果值不相等,则触发 panic。
func Is[V comparable](a, b V) {
if a != b {
zaplog.ZAPS.P1.LOG.Panic("A AND B ARE NOT EQUALS", zap.Any("a", a), zap.Any("b", b))
}
}
// Ise expects the errors to be equal, similar to the behavior of errors.Is. Panics if they are not equal.
// Ise 期望错误相等,类似于 errors.Is 的行为。如果错误不相等,则触发 panic。
func Ise(err, target error) {
if !errors.Is(err, target) {
zaplog.ZAPS.P1.LOG.Panic("ERROR IS NOT SAME", zap.Error(err), zap.Error(target))
}
}
// Ok expects a non-zero value. Panics if the value is zero.
// Ok 期望一个非零值。如果值为零,则触发 panic。
func Ok[V comparable](a V) {
var b V // zero value
if a == b {
zaplog.ZAPS.P1.LOG.Panic("A IS ZERO VALUE NOT OK", zap.Any("a", a))
}
}
// OK expects a non-zero value. Panics if the value is zero. Provides an alternative name for preference.
// OK 期望一个非零值。如果值为零,则触发 panic。提供一个偏好的替代名称。
func OK[V comparable](a V) {
var b V // zero value
if a == b {
zaplog.ZAPS.P1.LOG.Panic("A IS ZERO VALUE NOT OK", zap.Any("a", a))
}
}
// TRUE expects the value to be true. Panics if the value is false.
// TRUE 期望值为 true。如果值为 false,则触发 panic。
func TRUE(v bool) {
if !v {
zaplog.ZAPS.P1.LOG.Panic("expect TRUE while got FALSE", zap.Bool("v", v))
}
}
// FALSE expects the value to be false. Panics if the value is true.
// FALSE 期望值为 false。如果值为 true,则触发 panic。
func FALSE(v bool) {
if v {
zaplog.ZAPS.P1.LOG.Panic("expect FALSE while got TRUE", zap.Bool("v", v))
}
}
// False expects the value to be false. Panics if the value is true.
// False 期望值为 false。如果值为 true,则触发 panic。
func False(v bool) {
if v {
zaplog.ZAPS.P1.LOG.Panic("expect FALSE while got TRUE", zap.Bool("v", v))
}
}
// Have checks that the slice is not empty/none. Panics if the slice is empty/none.
// Have 检查切片是否为空。如果切片为空,则触发 panic。
func Have[T any](a []T) {
if len(a) == 0 {
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH > 0 while got an empty/none slice")
}
}
// Length expects the slice to have length n. Panics if the length is not n.
// Length 期望切片的长度为 n。如果长度不是 n,则触发 panic。
func Length[T any](a []T, n int) {
if len(a) != n {
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH = n while not equals", zap.Int("len", len(a)), zap.Int("n", n))
}
}
// Len is an abbreviation of Length, serving the same purpose. Panics if the length is not n.
// Len 是 Length 的缩写,功能相同。如果长度不是 n,则触发 panic。
func Len[T any](a []T, n int) {
if len(a) != n {
zaplog.ZAPS.P1.LOG.Panic("expect LENGTH = n while not equals", zap.Int("len", len(a)), zap.Int("n", n))
}
}
// In checks if the value is in the slice. Panics if the value is not found.
// In 检查值是否在切片中。如果未找到该值,则触发 panic。
func In[T comparable](v T, a []T) {
for i := range a {
if a[i] == v {
return
}
}
zaplog.ZAPS.P1.LOG.Panic("expect value in slice while not in", zap.Any("v", v), zap.Int("len", len(a)))
}
// Contains checks if the slice contains the value. Panics if the value is not found.
// Contains 检查切片是否包含该值。如果未找到该值,则触发 panic。
func Contains[T comparable](a []T, v T) {
for i := range a {
if a[i] == v {
return
}
}
zaplog.ZAPS.P1.LOG.Panic("expect slice contains value while not contains", zap.Int("len", len(a)), zap.Any("v", v))
}