-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_postaction2_test.go
More file actions
55 lines (47 loc) · 952 Bytes
/
example_postaction2_test.go
File metadata and controls
55 lines (47 loc) · 952 Bytes
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
package nject_test
import (
"fmt"
"github.com/muir/nject/v2"
)
type Causer interface {
Unwrap() error
Error() string
}
type MyError struct {
err error
}
func (err MyError) Error() string {
return "MY: " + err.err.Error()
}
func (err MyError) Unwrap() error {
return err.err
}
var _ Causer = MyError{}
func ExamplePostActionByTag_withInterfaces() {
type S struct {
Error Causer `nject:"print-error,print-cause"`
}
fmt.Println(nject.Run("example",
func() error {
return fmt.Errorf("an injected error")
},
func(err error) Causer {
return MyError{err: err}
},
nject.MustMakeStructBuilder(S{},
nject.PostActionByTag("print-error", func(err error) {
fmt.Println(err)
}),
nject.PostActionByTag("print-cause", func(err Causer) {
fmt.Println("Cause:", err.Unwrap())
}),
),
func(_ S) {
fmt.Println("Done")
},
))
// Output: MY: an injected error
// Cause: an injected error
// Done
// <nil>
}