Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Created January 20, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwojciec/f3a3b30112cf94253dcc49826f1124a4 to your computer and use it in GitHub Desktop.
Save fwojciec/f3a3b30112cf94253dcc49826f1124a4 to your computer and use it in GitHub Desktop.
func newAuthorsByAgentID(ctx context.Context, repo pg.Repository) *AuthorSliceLoader {
return NewAuthorSliceLoader(AuthorSliceLoaderConfig{
MaxBatch: 100,
Wait: 5 * time.Millisecond,
Fetch: func(agentIDs []int64) ([][]pg.Author, []error) {
// db query
res, err := repo.ListAuthorsByAgentIDs(ctx, agentIDs)
if err != nil {
return nil, []error{err}
}
// group
groupByAgentID := make(map[int64][]pg.Author, len(agentIDs))
for _, r := range res {
groupByAgentID[r.AgentID] = append(groupByAgentID[r.AgentID], r)
}
// order
result := make([][]pg.Author, len(agentIDs))
for i, agentID := range agentIDs {
result[i] = groupByAgentID[agentID]
}
return result, nil
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment