-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.go
56 lines (46 loc) · 1003 Bytes
/
options.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
package loaders
import (
"context"
"github.com/graph-gophers/dataloader/v7"
"reflect"
"time"
)
func getOptions[K comparable, V any](ctx context.Context, opts ...Option) []dataloader.Option[K, V] {
var options []dataloader.Option[K, V]
var cache dataloader.Cache[K, V]
var name string
for _, opt := range opts {
switch t := opt.(type) {
case loaderName:
name = string(t)
}
}
if name == "" {
var t V
tp := reflect.TypeOf(t)
n := tp.String()
if n != "" {
name = n
}
}
for _, opt := range opts {
switch t := opt.(type) {
case MemoryCache:
cache = NewMemoryLoaderCache[K, V](ctx, name, t.expiration)
}
}
if name == "" {
var t V
tp := reflect.TypeOf(t)
n := tp.String()
if n != "" {
name = n
}
}
if cache == nil {
cache = NewMemoryLoaderCache[K, V](ctx, name, time.Minute*5)
}
options = append(options, dataloader.WithCache[K, V](cache))
options = append(options, dataloader.WithTracer[K, V](newTracer[K, V](name)))
return options
}