-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
125 lines (103 loc) · 2.49 KB
/
check.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
package check
import (
"golang.org/x/exp/constraints"
bt "github.com/wrmsr/bane/core/types"
)
//
func NotZero[T comparable](v T, details ...any) T {
if v == bt.Zero[T]() {
panic(checkError("cannot be zero", []any{v}, details...))
}
return v
}
func NotNil(v any, details ...any) any {
if v == nil {
panic(checkError("cannot be nil", []any{v}, details...))
}
return v
}
func Nil(v any, details ...any) any {
if v != nil {
panic(checkError("must be nil", []any{v}, details...))
}
return v
}
func Condition(b bool, details ...any) {
if !b {
panic(checkError("condition not met", nil, details...))
}
}
func Equal[T comparable](actual, expected T, details ...any) T {
if actual != expected {
panic(checkError("must be equal", []any{actual, expected}, details...))
}
return actual
}
func NotEqual[T comparable](actual, expected T, details ...any) T {
if actual == expected {
panic(checkError("must not be equal", []any{actual, expected}, details...))
}
return actual
}
func Single[T any](s []T, details ...any) T {
if len(s) != 1 {
panic(checkError("must be single", []any{s}, details...))
}
return s[0]
}
func Pair[T any](s []T, details ...any) (T, T) {
if len(s) != 2 {
panic(checkError("must be pair", []any{s}, details...))
}
return s[0], s[1]
}
func NotEmptySlice[T any](s []T, details ...any) []T {
if len(s) < 1 {
panic(checkError("must not be empty slice", []any{s}, details...))
}
return s
}
func EmptySlice[T any](s []T, details ...any) []T {
if len(s) > 0 {
panic(checkError("must be empty slice", []any{s}, details...))
}
return s
}
func NotEmptyStr(s string, details ...any) string {
if s == "" {
panic(checkError("must not be empty string", []any{s}, details...))
}
return s
}
func Between[T constraints.Ordered](v, lo, hi T, details ...any) T {
if v < lo || v > hi {
panic(checkError("must be between", []any{v, lo, hi}, details...))
}
return v
}
//
type HasLen interface {
Len() int
}
func NotEmpty[T HasLen](ctr T, details ...any) T {
if ctr.Len() < 1 {
panic(checkError("must not be empty", []any{ctr}, details...))
}
return ctr
}
//
type Container[T any] interface {
Contains(v T) bool
}
func Contains[T any](ctr Container[T], v T, details ...any) T {
if !ctr.Contains(v) {
panic(checkError("container must contain", []any{ctr, v}, details...))
}
return v
}
func NotContains[T any](ctr Container[T], v T, details ...any) T {
if ctr.Contains(v) {
panic(checkError("container must not contain", []any{ctr, v}, details...))
}
return v
}