-
Notifications
You must be signed in to change notification settings - Fork 0
/
must.go
60 lines (52 loc) · 905 Bytes
/
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
package goo
import (
"errors"
"fmt"
"os"
"runtime"
"strings"
)
func Ok[V any](data V, ok bool) V {
if ok {
return data
}
panic("must: bool was false")
}
func DebugOk[V any](data V, ok bool) V {
if ok {
return data
}
_, file, line, _ := runtime.Caller(1)
cache := strings.Split(file, string(os.PathSeparator))
panic(fmt.Sprintf("must: bool was false on %s:%d", cache[len(cache)-1], line))
}
func Err[V any](data V, err error) V {
if err != nil {
panic(err)
}
return data
}
type Resolver[OUT any] struct {
o OUT
e error
}
func (r Resolver[OUT]) Then(fn func(o OUT) error) error {
if r.e != nil {
return r.e
}
return fn(r.o)
}
func Do[OUT any](o OUT, err error) Resolver[OUT] {
return Resolver[OUT]{
o, err,
}
}
func Check[OUT any](o OUT, ok bool) Resolver[OUT] {
var err error
if !ok {
err = errors.New("boolean: false")
}
return Resolver[OUT]{
o, err,
}
}