Long overdue initial commit
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package loader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.aiterp.net/rpdata/api/model/character"
|
||||
"github.com/graph-gophers/dataloader"
|
||||
)
|
||||
|
||||
// Character gets a character by key
|
||||
func (loader *Loader) Character(key, value string) (character.Character, error) {
|
||||
if !strings.HasPrefix(key, "Character.") {
|
||||
key = "Character." + key
|
||||
}
|
||||
|
||||
if loader.loaders[key] == nil {
|
||||
return character.Character{}, errors.New("unsupported key")
|
||||
}
|
||||
|
||||
thunk := loader.loaders[key].Load(loader.ctx, dataloader.StringKey(value))
|
||||
res, err := thunk()
|
||||
if err != nil {
|
||||
return character.Character{}, err
|
||||
}
|
||||
|
||||
char, ok := res.(character.Character)
|
||||
if !ok {
|
||||
return character.Character{}, errors.New("incorrect type")
|
||||
}
|
||||
|
||||
return char, nil
|
||||
}
|
||||
|
||||
// Characters gets characters by key
|
||||
func (loader *Loader) Characters(key string, values ...string) ([]character.Character, error) {
|
||||
if !strings.HasPrefix(key, "Character.") {
|
||||
key = "Character." + key
|
||||
}
|
||||
|
||||
if loader.loaders[key] == nil {
|
||||
return nil, errors.New("unsupported key")
|
||||
}
|
||||
|
||||
thunk := loader.loaders[key].LoadMany(loader.ctx, dataloader.NewKeysFromStrings(values))
|
||||
res, errs := thunk()
|
||||
for _, err := range errs {
|
||||
if err != nil && err != ErrNotFound {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
chars := make([]character.Character, len(res))
|
||||
|
||||
for i := range res {
|
||||
char, ok := res[i].(character.Character)
|
||||
if !ok {
|
||||
return nil, errors.New("incorrect type")
|
||||
}
|
||||
|
||||
chars[i] = char
|
||||
}
|
||||
|
||||
return chars, nil
|
||||
}
|
||||
|
||||
func characterIDBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
|
||||
results := make([]*dataloader.Result, 0, len(keys))
|
||||
ids := keys.Keys()
|
||||
|
||||
log.Println("Loading", len(ids), "characters:", strings.Join(ids, ","))
|
||||
|
||||
characters, err := character.ListIDs(ids...)
|
||||
if err != nil {
|
||||
for range ids {
|
||||
results = append(results, &dataloader.Result{Error: err})
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
found := false
|
||||
|
||||
for _, character := range characters {
|
||||
if character.ID == id {
|
||||
results = append(results, &dataloader.Result{Data: character})
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
results = append(results, &dataloader.Result{Data: character.Character{}, Error: ErrNotFound})
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
func characterNickBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
|
||||
var results []*dataloader.Result
|
||||
nicks := keys.Keys()
|
||||
|
||||
characters, err := character.ListNicks(nicks...)
|
||||
if err != nil {
|
||||
for range nicks {
|
||||
results = append(results, &dataloader.Result{Error: err})
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
for _, nick := range nicks {
|
||||
found := false
|
||||
for i := range characters {
|
||||
if characters[i].HasNick(nick) {
|
||||
results = append(results, &dataloader.Result{Data: characters[i]})
|
||||
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
results = append(results, &dataloader.Result{Data: character.Character{}, Error: err})
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package loader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
)
|
||||
|
||||
var contextKey struct{}
|
||||
|
||||
// ErrNotFound is returned in batches when one or more things weren't found. Usually harmless.
|
||||
var ErrNotFound = errors.New("not found")
|
||||
|
||||
// A Loader is a collection of data loaders and functions to act on them. It's supposed to be
|
||||
// request-scoped, and will thus keep things cached indefinitely.
|
||||
type Loader struct {
|
||||
ctx context.Context
|
||||
loaders map[string]*dataloader.Loader
|
||||
}
|
||||
|
||||
// New initializes the loader.
|
||||
func New() *Loader {
|
||||
return &Loader{
|
||||
ctx: context.Background(),
|
||||
loaders: map[string]*dataloader.Loader{
|
||||
"Character.id": dataloader.NewBatchedLoader(characterIDBatch, dataloader.WithWait(time.Millisecond*2)),
|
||||
"Character.nick": dataloader.NewBatchedLoader(characterNickBatch, dataloader.WithWait(time.Millisecond*2)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// FromContext gets the Loader from context.
|
||||
func FromContext(ctx context.Context) *Loader {
|
||||
value := ctx.Value(&contextKey)
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return value.(*Loader)
|
||||
}
|
||||
|
||||
// ToContext gets a context with the loader as a value
|
||||
func (loader *Loader) ToContext(ctx context.Context) context.Context {
|
||||
loader.ctx = ctx
|
||||
return context.WithValue(ctx, &contextKey, loader)
|
||||
}
|
||||
Reference in New Issue
Block a user