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