1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 22:45:24 +00:00
v2fly/tools/conf/loader.go

80 lines
1.9 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-08 23:43:25 +00:00
return newError("Config: ", id, " already registered.")
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-08 23:43:25 +00:00
return nil, newError("Config: 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-08 23:43:25 +00:00
return nil, newError("Config: Unknown config id: ", id)
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-08 23:43:25 +00:00
return nil, "", newError("Config: ", v.idKey, " not found in JSON context.")
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-08 23:43:25 +00:00
return nil, "", newError("Config: ", v.configKey, " not found in JSON content.")
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
}