This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
99 lines (85 loc) · 1.67 KB
/
response.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
package async
//stdLib
import (
"fmt"
"sync"
)
type Response interface {
HasError() bool
Error() error
HasResult() bool
Result() interface{}
}
type SingleResponse struct {
res interface{}
err error
}
func (r *SingleResponse) HasError() bool {
return r.err != nil
}
func (r *SingleResponse) Error() error {
return r.err
}
func (r *SingleResponse) HasResult() bool {
return r.res != nil
}
func (r *SingleResponse) Result() interface{} {
return r.res
}
type MultiResponse struct {
sync.Mutex
results map[string]chan interface{}
errors map[string]chan error
resultsRead map[string]interface{}
errorsRead map[string]error
}
func (r *MultiResponse) HasError() bool {
var errors = r.Errors()
for _, err := range errors {
if err != nil {
return true
}
}
return false
}
func (r *MultiResponse) Error() (error) {
return fmt.Errorf("%v", r.Errors())
}
func (r *MultiResponse) Errors() (map[string]error) {
r.Lock()
defer r.Unlock()
if r.errorsRead != nil {
return r.errorsRead
}
r.errorsRead = make(map[string]error, len(r.errors))
for name, ch := range r.errors {
r.errorsRead[name], _ = <-ch
}
return r.errorsRead
}
func (r *MultiResponse) HasResult() bool {
var results = r.Results()
for _, res := range results {
if res != nil {
return true
}
}
return false
}
func (r *MultiResponse) Results() (map[string]interface{}) {
r.Lock()
defer r.Unlock()
if r.resultsRead != nil {
return r.resultsRead
}
r.resultsRead = make(map[string]interface{}, len(r.results))
for name, ch := range r.results {
for res := range ch {
r.resultsRead[name] = res
}
}
return r.resultsRead
}
func (r *MultiResponse) Result() (interface{}) {
return r.Results()
}