-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
45 lines (36 loc) · 839 Bytes
/
service.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
package loader
import (
"github.com/pkg/errors"
"admini.dev/admini/app/lib/schema"
)
type loaderFn = func(key string, cfg []byte) (Loader, error)
type Service struct {
loaders map[schema.Origin]loaderFn
cache map[string]Loader
}
func NewService() *Service {
return &Service{
loaders: map[schema.Origin]loaderFn{},
cache: map[string]Loader{},
}
}
func (s *Service) Set(o schema.Origin, f loaderFn) {
s.loaders[o] = f
}
func (s *Service) Get(origin schema.Origin, key string, cfg []byte) (Loader, error) {
check := origin.Key + "-" + key
curr, ok := s.cache[check]
if ok {
return curr, nil
}
o := s.loaders[origin]
l, err := o(key, cfg)
if err != nil {
return nil, errors.Wrap(err, "can't create loader")
}
s.cache[check] = l
return l, nil
}
func (s *Service) Clear() {
s.cache = map[string]Loader{}
}