v2ray-core/common/loader/loader.go

37 lines
717 B
Go
Raw Normal View History

2016-06-10 20:26:39 +00:00
package loader
import (
"errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common"
2016-06-10 20:26:39 +00:00
)
var (
2016-08-18 06:34:21 +00:00
ErrUnknownConfigID = errors.New("Unknown config ID.")
2016-06-10 20:26:39 +00:00
)
type ConfigCreator func() interface{}
2016-09-21 11:52:16 +00:00
type ConfigCreatorCache map[string]ConfigCreator
2016-06-10 20:26:39 +00:00
2016-09-21 11:52:16 +00:00
func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := this[id]; found {
2016-08-18 06:34:21 +00:00
return common.ErrDuplicatedName
2016-06-10 20:26:39 +00:00
}
2016-09-21 11:52:16 +00:00
this[id] = creator
2016-06-10 20:26:39 +00:00
return nil
}
2016-09-21 11:52:16 +00:00
func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
creator, found := this[id]
2016-06-10 20:26:39 +00:00
if !found {
return nil, ErrUnknownConfigID
}
return creator(), nil
}
2016-09-21 11:52:16 +00:00
type ConfigLoader interface {
Load([]byte) (interface{}, string, error)
LoadWithID([]byte, string) (interface{}, error)
}