-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_postaction_test.go
134 lines (127 loc) · 2.28 KB
/
example_postaction_test.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
package nject_test
import (
"fmt"
"github.com/muir/nject"
)
func ExamplePostActionByTag() {
type S struct {
I int `nject:"square-me"`
}
nject.Run("example",
func() int {
return 4
},
nject.MustMakeStructBuilder(&S{}, nject.PostActionByTag("square-me", func(i *int) {
*i *= *i
}, nject.WithFill(true))),
func(s *S) {
fmt.Println(s.I)
},
)
// Output: 16
}
func ExamplePostActionByTag_wihtoutPointers() {
type S struct {
I int `nject:"square-me"`
}
nject.Run("example",
func() int {
return 4
},
nject.MustMakeStructBuilder(S{}, nject.PostActionByTag("square-me", func(i int) {
fmt.Println(i * i)
})),
func(s S) {
fmt.Println(s.I)
},
)
// Output: 16
// 4
}
func ExamplePostActionByTag_conversion() {
type S struct {
I int32 `nject:"rollup"`
J int32 `nject:"rolldown"`
}
fmt.Println(nject.Run("example",
func() int32 {
return 10
},
func() *[]int {
var x []int
return &x
},
nject.MustMakeStructBuilder(S{},
nject.PostActionByTag("rollup", func(i int, a *[]int) {
*a = append(*a, i+1)
}),
nject.PostActionByTag("rolldown", func(i int64, a *[]int) {
*a = append(*a, int(i)-1)
}),
),
func(_ S, a *[]int) {
fmt.Println(*a)
},
))
// Output: [11 9]
// <nil>
}
func ExamplePostActionByName() {
type S struct {
I int32
J int32
}
fmt.Println(nject.Run("example",
func() int32 {
return 10
},
func() *[]int {
var x []int
return &x
},
nject.MustMakeStructBuilder(S{},
nject.PostActionByName("I", func(i int, a *[]int) {
*a = append(*a, i+1)
}),
nject.PostActionByName("J", func(i int64, a *[]int) {
*a = append(*a, int(i)-1)
}),
),
func(_ S, a *[]int) {
fmt.Println(*a)
},
))
// Output: [11 9]
// <nil>
}
func ExamplePostActionByType() {
type S struct {
I int32
J int64
}
fmt.Println(nject.Run("example",
func() int32 {
return 10
},
func() int64 {
return 20
},
func() *[]int {
var x []int
return &x
},
nject.MustMakeStructBuilder(&S{},
nject.PostActionByType(func(i int32, a *[]int) {
*a = append(*a, int(i))
}, nject.WithFill(true)),
nject.PostActionByType(func(i *int32, a *[]int) {
*i += 5
}, nject.WithFill(true)),
),
func(s *S, a *[]int) {
fmt.Println(*a, s.I, s.J)
},
))
// Output: [15] 15 20
// <nil>
}