-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
56 lines (43 loc) · 821 Bytes
/
output.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
package promise
import "reflect"
// Output is interface of structure for asynchronous function 's return values
type Output[T any] interface {
Payload() T
Error() error
setKey(key interface{}) Output[T]
}
type output[T any] struct {
payload *T
error error
key interface{}
}
func newOutput[T any](pay T, err error) Output[T] {
var zeroT T
if reflect.DeepEqual(pay, zeroT) {
return &output[T]{
payload: nil,
error: err,
}
}
return &output[T]{
payload: &pay,
error: err,
}
}
func (o *output[T]) Payload() T {
if o.payload == nil {
var zeroT T
return zeroT
}
return *o.payload
}
func (o *output[T]) Error() error {
return o.error
}
func (o *output[T]) Key() interface{} {
return o.key
}
func (o *output[T]) setKey(key interface{}) Output[T] {
o.key = key
return o
}