-
Notifications
You must be signed in to change notification settings - Fork 2
/
result.go
64 lines (52 loc) · 1.56 KB
/
result.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
package dataloader
// Result is an alias for the resolved data by the batch loader
type Result struct {
Result interface{}
Err error
}
// ResultMap maps each loaded elements Result against the elements unique identifier (Key)
type ResultMap interface {
Set(string, Result)
GetValue(Key) (Result, bool)
Length() int
// Keys returns a slice of all unique identifiers used in the containing map (keys)
Keys() []string
GetValueForString(string) Result
}
type resultMap struct {
r map[string]Result
}
// NewResultMap returns a new instance of the result map with the provided capacity.
// Each value defaults to nil
func NewResultMap(capacity int) ResultMap {
r := make(map[string]Result, capacity)
return &resultMap{r: r}
}
// ===================================== public methods =====================================
// Set adds the value to the to the result set.
func (r *resultMap) Set(identifier string, value Result) {
r.r[identifier] = value
}
// GetValue returns the value from the results for the provided key and true
// if the value was found, otherwise false.
func (r *resultMap) GetValue(key Key) (Result, bool) {
if key == nil {
return Result{}, false
}
result, ok := r.r[key.String()]
return result, ok
}
func (r *resultMap) GetValueForString(key string) Result {
// No need to check ok, missing value from map[Any]interface{} is nil by default.
return r.r[key]
}
func (r *resultMap) Keys() []string {
res := make([]string, 0, len(r.r))
for k := range r.r {
res = append(res, k)
}
return res
}
func (r *resultMap) Length() int {
return len(r.r)
}