-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.go
55 lines (43 loc) · 1.24 KB
/
utils.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
package bfldb
import (
"context"
"golang.org/x/sync/errgroup"
)
type aggregatedNickname struct {
Nickname string // Nickmane this struct belongs to
UIDs []string // User IDs associated with the nickname
}
// NicknamesToUIDs gets a list of UIDs for the nicknames provided.
// Returns a map with nicknames mapped to the UIDs and also any errors that might've occured.
//
// It fires up one goroutine for each nickname and fetches the UIDs.
func NicknamesToUIDs(pCtx context.Context, nicks []string) (map[string][]string, error) {
idC := make(chan aggregatedNickname)
g, ctx := errgroup.WithContext(pCtx)
for _, n := range nicks {
n := n
g.Go(func() error {
res, err := SearchNickname(ctx, n)
if err == nil {
// map the response to the UIDs only
nIds := make([]string, 0, len(res.Data))
for _, data := range res.Data {
nIds = append(nIds, data.EncryptedUID)
}
idC <- aggregatedNickname{Nickname: n, UIDs: nIds}
}
return err
})
}
// close the channel once the requests are done
go func() {
g.Wait()
close(idC)
}()
// agregate it into a map of usernames to the ids
aRes := make(map[string][]string, len(nicks))
for id := range idC {
aRes[id.Nickname] = id.UIDs
}
return aRes, g.Wait()
}