-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
87 lines (51 loc) · 1.04 KB
/
example_test.go
File metadata and controls
87 lines (51 loc) · 1.04 KB
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
package try_test
import (
"github.com/go-libfp/try"
"encoding/json"
"fmt"
"testing"
"log"
"errors"
)
type Foo struct { Bar string}
func jsonDecode[T any](data []byte) (T, error) {
var out T
err := json.Unmarshal(data, &out)
return out, err
}
func ExampleUsage() {
t := try.WrapErr( json.Marshal(Foo{"hello"}) )
// bind ET will run a function that returns err tuple
t1 := try.BindET(t, jsonDecode[Foo])
t2 := try.Map(t1, func(x Foo) string {
return x.Bar
} )
t2.OnSuccess(func(x string) {
fmt.Println(x)
} )
t2.OnErr(func(e error) {
log.Println(e)
})
}
func ExampleConstructorsGetters() {
t := try.Ok("hello buddeh")
x, e := t.Get()
fmt.Println(x)
//maybe rename try.Err to Fail in the future
dam := errors.New("damn")
fail_t := try.Err[string](dam)
t.IsOk()
t.IsErr()
e = t.GetErr()
x = t.GetValue()
if e != nil {
log.Println(e)
}
fail_t.OnErr(
func(e error) { log.Println(e) },
)
}
func TestExamples(t *testing.T) {
ExampleUsage()
ExampleConstructorsGetters()
}