-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
156 lines (128 loc) · 3.52 KB
/
logic.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package do
var (
EmptyLogicFunc = func(C, struct{}) (r struct{}, err E) { return }
NilLogicFunc Logic[struct{}, struct{}]
)
type (
Logic[P, R any] func(C, P) (R, E)
LogicWithoutParam[R any] func(C) (R, E)
LogicWithoutResult[P any] func(C, P) E
LogicWithoutPR func(C) E
LogicWithoutError[P, R any] func(C, P) R
LogicWithoutParamError[R any] func(C) R
LogicWithoutResultError[P any] func(C, P)
LogicWithoutParamResultError func(C)
)
func LogicFrom[P, R any](f func(C, P) (R, E)) Logic[P, R] {
return f
}
func LogicFromWP[R any](f func(C) (R, E)) Logic[struct{}, R] {
return LogicWP(f).ToLogic()
}
func LogicFromWR[P any](f func(C, P) E) Logic[P, struct{}] {
return LogicWR(f).ToLogic()
}
func LogicFromWPR(f func(C) E) Logic[struct{}, struct{}] {
return LogicWPR(f).ToLogic()
}
func LogicFromWE[P, R any](f func(C, P) R) Logic[P, R] {
return LogicWE(f).ToLogic()
}
func LogicFromWPE[R any](f func(C) R) Logic[struct{}, R] {
return LogicWPE(f).ToLogic()
}
func LogicFromWRE[P any](f func(C, P)) Logic[P, struct{}] {
return LogicWRE(f).ToLogic()
}
func LogicFromWPRE(f func(C)) Logic[struct{}, struct{}] {
return LogicWPRE(f).ToLogic()
}
func LogicWP[R any](f func(C) (R, E)) LogicWithoutParam[R] {
return f
}
func LogicWR[P any](f func(C, P) E) LogicWithoutResult[P] {
return f
}
func LogicWPR(f func(C) E) LogicWithoutPR {
return f
}
func LogicWE[P, R any](f func(C, P) R) LogicWithoutError[P, R] {
return f
}
func LogicWPE[R any](f func(C) R) LogicWithoutParamError[R] {
return f
}
func LogicWRE[P any](f func(C, P)) LogicWithoutResultError[P] {
return f
}
func LogicWPRE(f func(C)) LogicWithoutParamResultError {
return f
}
func (l Logic[P, R]) ToLogic() Logic[P, R] {
return l
}
func (l LogicWithoutParam[R]) ToLogic() Logic[struct{}, R] {
return func(ctx C, p struct{}) (r R, err E) {
return l(ctx)
}
}
func (l LogicWithoutResult[P]) ToLogic() Logic[P, struct{}] {
return func(ctx C, p P) (r struct{}, err E) {
err = l(ctx, p)
return
}
}
func (l LogicWithoutPR) ToLogic() Logic[struct{}, struct{}] {
return func(ctx C, p struct{}) (r struct{}, err E) {
err = l(ctx)
return
}
}
func (l LogicWithoutError[P, R]) ToLogic() Logic[P, R] {
return func(ctx C, p P) (r R, err E) {
r = l(ctx, p)
return
}
}
func (l LogicWithoutParamError[R]) ToLogic() Logic[struct{}, R] {
return func(ctx C, p struct{}) (r R, err E) {
r = l(ctx)
return
}
}
func (l LogicWithoutResultError[P]) ToLogic() Logic[P, struct{}] {
return func(ctx C, p P) (r struct{}, err E) {
l(ctx, p)
return
}
}
func (l LogicWithoutParamResultError) ToLogic() Logic[struct{}, struct{}] {
return func(ctx C, p struct{}) (r struct{}, err E) {
l(ctx)
return
}
}
type ToLogic[P, R any] interface {
ToLogic() Logic[P, R]
}
var (
_ ToLogic[int, string] = Logic[int, string](nil)
_ ToLogic[struct{}, string] = LogicWithoutParam[string](nil)
_ ToLogic[int, struct{}] = LogicWithoutResult[int](nil)
_ ToLogic[struct{}, struct{}] = LogicWithoutPR(nil)
_ ToLogic[int, string] = LogicWithoutError[int, string](nil)
_ ToLogic[struct{}, string] = LogicWithoutParamError[string](nil)
_ ToLogic[int, struct{}] = LogicWithoutResultError[int](nil)
_ ToLogic[struct{}, struct{}] = LogicWithoutParamResultError(nil)
)
type LogicSet[P, R any] interface {
func(C, P) (R, E) |
Logic[P, R] |
LogicWithoutParam[R] |
LogicWithoutResult[P] |
LogicWithoutPR |
LogicWithoutError[P, R] |
LogicWithoutParamError[R] |
LogicWithoutResultError[P] |
LogicWithoutParamResultError
}