v2ray-core/tools/conf/loader.go

80 lines
1.8 KiB
Go
Raw Normal View History

2016-10-17 12:35:13 +00:00
package conf
2016-06-10 20:26:39 +00:00
2017-04-08 23:43:25 +00:00
import "encoding/json"
2016-10-17 12:35:13 +00:00
type ConfigCreator func() interface{}
type ConfigCreatorCache map[string]ConfigCreator
2016-11-27 20:39:09 +00:00
func (v ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := v[id]; found {
2017-04-09 13:04:04 +00:00
return newError(id, " already registered.").AtError()
2016-10-17 12:35:13 +00:00
}
2016-11-27 20:39:09 +00:00
v[id] = creator
2016-10-17 12:35:13 +00:00
return nil
}
2016-11-27 20:39:09 +00:00
func (v ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
creator, found := v[id]
2016-10-17 12:35:13 +00:00
if !found {
2017-04-09 13:04:04 +00:00
return nil, newError("unknown config id: ", id)
2016-10-17 12:35:13 +00:00
}
return creator(), nil
}
2016-10-16 12:22:21 +00:00
2016-06-10 20:26:39 +00:00
type JSONConfigLoader struct {
2016-10-17 12:35:13 +00:00
cache ConfigCreatorCache
2016-06-10 20:26:39 +00:00
idKey string
configKey string
}
2016-10-17 12:35:13 +00:00
func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
2016-06-10 20:26:39 +00:00
return &JSONConfigLoader{
2016-09-21 11:52:16 +00:00
idKey: idKey,
configKey: configKey,
cache: cache,
2016-06-10 20:26:39 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
creator, found := v.cache[id]
2016-10-17 12:35:13 +00:00
if !found {
2017-04-09 13:04:04 +00:00
return nil, newError("unknown config id: ", id).AtError()
2016-06-10 20:26:39 +00:00
}
2016-10-17 12:35:13 +00:00
config := creator()
2016-06-10 20:26:39 +00:00
if err := json.Unmarshal(raw, config); err != nil {
return nil, err
}
return config, nil
}
2016-11-27 20:39:09 +00:00
func (v *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
2016-06-10 21:01:17 +00:00
var obj map[string]json.RawMessage
if err := json.Unmarshal(raw, &obj); err != nil {
2016-08-06 19:59:22 +00:00
return nil, "", err
2016-06-10 20:26:39 +00:00
}
2016-11-27 20:39:09 +00:00
rawID, found := obj[v.idKey]
2016-06-10 20:26:39 +00:00
if !found {
2017-04-09 13:04:04 +00:00
return nil, "", newError(v.idKey, " not found in JSON context").AtError()
2016-06-10 20:26:39 +00:00
}
var id string
2016-06-10 21:01:17 +00:00
if err := json.Unmarshal(rawID, &id); err != nil {
2016-08-06 19:59:22 +00:00
return nil, "", err
2016-06-10 20:26:39 +00:00
}
rawConfig := json.RawMessage(raw)
2016-11-27 20:39:09 +00:00
if len(v.configKey) > 0 {
configValue, found := obj[v.configKey]
2016-06-10 20:26:39 +00:00
if !found {
2017-04-09 13:04:04 +00:00
return nil, "", newError(v.configKey, " not found in JSON content").AtError()
2016-06-10 20:26:39 +00:00
}
rawConfig = configValue
}
2016-11-27 20:39:09 +00:00
config, err := v.LoadWithID([]byte(rawConfig), id)
2016-08-06 19:59:22 +00:00
if err != nil {
return nil, id, err
}
return config, id, nil
2016-06-10 20:26:39 +00:00
}