1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/tools/conf/loader.go

84 lines
1.9 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
2016-06-10 16:26:39 -04:00
import (
"encoding/json"
2017-02-02 08:42:31 -05:00
"v2ray.com/core/common/errors"
2016-10-17 08:35:13 -04:00
)
type ConfigCreator func() interface{}
type ConfigCreatorCache map[string]ConfigCreator
2016-11-27 15:39:09 -05:00
func (v ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := v[id]; found {
2017-02-02 08:42:31 -05:00
return errors.New("Config: ", id, " already registered.")
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
v[id] = creator
2016-10-17 08:35:13 -04:00
return nil
}
2016-11-27 15:39:09 -05:00
func (v ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
creator, found := v[id]
2016-10-17 08:35:13 -04:00
if !found {
2017-02-02 08:42:31 -05:00
return nil, errors.New("Config: Unknown config id: ", id)
2016-10-17 08:35:13 -04:00
}
return creator(), nil
}
2016-10-16 08:22:21 -04:00
2016-06-10 16:26:39 -04:00
type JSONConfigLoader struct {
2016-10-17 08:35:13 -04:00
cache ConfigCreatorCache
2016-06-10 16:26:39 -04:00
idKey string
configKey string
}
2016-10-17 08:35:13 -04:00
func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
2016-06-10 16:26:39 -04:00
return &JSONConfigLoader{
2016-09-21 07:52:16 -04:00
idKey: idKey,
configKey: configKey,
cache: cache,
2016-06-10 16:26:39 -04:00
}
}
2016-11-27 15:39:09 -05:00
func (v *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
creator, found := v.cache[id]
2016-10-17 08:35:13 -04:00
if !found {
2017-02-02 08:42:31 -05:00
return nil, errors.New("Config: Unknown config id: ", id)
2016-06-10 16:26:39 -04:00
}
2016-10-17 08:35:13 -04:00
config := creator()
2016-06-10 16:26:39 -04:00
if err := json.Unmarshal(raw, config); err != nil {
return nil, err
}
return config, nil
}
2016-11-27 15:39:09 -05:00
func (v *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
2016-06-10 17:01:17 -04:00
var obj map[string]json.RawMessage
if err := json.Unmarshal(raw, &obj); err != nil {
2016-08-06 15:59:22 -04:00
return nil, "", err
2016-06-10 16:26:39 -04:00
}
2016-11-27 15:39:09 -05:00
rawID, found := obj[v.idKey]
2016-06-10 16:26:39 -04:00
if !found {
2017-02-02 08:42:31 -05:00
return nil, "", errors.New("Config: ", v.idKey, " not found in JSON context.")
2016-06-10 16:26:39 -04:00
}
var id string
2016-06-10 17:01:17 -04:00
if err := json.Unmarshal(rawID, &id); err != nil {
2016-08-06 15:59:22 -04:00
return nil, "", err
2016-06-10 16:26:39 -04:00
}
rawConfig := json.RawMessage(raw)
2016-11-27 15:39:09 -05:00
if len(v.configKey) > 0 {
configValue, found := obj[v.configKey]
2016-06-10 16:26:39 -04:00
if !found {
2017-02-02 08:42:31 -05:00
return nil, "", errors.New("Config: ", v.configKey, " not found in JSON content.")
2016-06-10 16:26:39 -04:00
}
rawConfig = configValue
}
2016-11-27 15:39:09 -05:00
config, err := v.LoadWithID([]byte(rawConfig), id)
2016-08-06 15:59:22 -04:00
if err != nil {
return nil, id, err
}
return config, id, nil
2016-06-10 16:26:39 -04:00
}