-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
46 lines (37 loc) · 987 Bytes
/
loader.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
package loader
import (
"errors"
"github.com/v2ray/v2ray-core/common"
)
var (
ErrUnknownConfigID = errors.New("Unknown config ID.")
)
type ConfigCreator func() interface{}
type ConfigLoader interface {
RegisterCreator(string, ConfigCreator) error
CreateConfig(string) (interface{}, error)
Load([]byte) (interface{}, string, error)
LoadWithID([]byte, string) (interface{}, error)
}
type BaseConfigLoader struct {
creators map[string]ConfigCreator
}
func NewBaseConfigLoader() *BaseConfigLoader {
return &BaseConfigLoader{
creators: make(map[string]ConfigCreator),
}
}
func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := this.creators[id]; found {
return common.ErrDuplicatedName
}
this.creators[id] = creator
return nil
}
func (this *BaseConfigLoader) CreateConfig(id string) (interface{}, error) {
creator, found := this.creators[id]
if !found {
return nil, ErrUnknownConfigID
}
return creator(), nil
}