| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- // +build json
- package loader
- import (
- "encoding/json"
- "v2ray.com/core/common"
- "v2ray.com/core/common/log"
- )
- type NamedTypeMap map[string]string
- type JSONConfigLoader struct {
- cache NamedTypeMap
- idKey string
- configKey string
- }
- func NewJSONConfigLoader(cache NamedTypeMap, idKey string, configKey string) *JSONConfigLoader {
- return &JSONConfigLoader{
- idKey: idKey,
- configKey: configKey,
- cache: cache,
- }
- }
- func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
- config, err := GetInstance(this.cache[id])
- if err != nil {
- return nil, err
- }
- if err := json.Unmarshal(raw, config); err != nil {
- return nil, err
- }
- return config, nil
- }
- func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
- var obj map[string]json.RawMessage
- if err := json.Unmarshal(raw, &obj); err != nil {
- return nil, "", err
- }
- rawID, found := obj[this.idKey]
- if !found {
- log.Error(this.idKey, " not found in JSON content.")
- return nil, "", common.ErrObjectNotFound
- }
- var id string
- if err := json.Unmarshal(rawID, &id); err != nil {
- return nil, "", err
- }
- rawConfig := json.RawMessage(raw)
- if len(this.configKey) > 0 {
- configValue, found := obj[this.configKey]
- if !found {
- log.Error(this.configKey, " not found in JSON content.")
- return nil, "", common.ErrObjectNotFound
- }
- rawConfig = configValue
- }
- config, err := this.LoadWithID([]byte(rawConfig), id)
- if err != nil {
- return nil, id, err
- }
- return config, id, nil
- }
|