| 1 | package confscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.15 strings/" |
| 6 | ) |
| 7 | |
| 8 | type ConfigCreatorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreator# func() interface{} |
| 9 | |
| 10 | type ConfigCreatorCachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache# map[string]ConfigCreatorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreator# |
| 11 | |
| 12 | func (v ConfigCreatorCachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#) RegisterCreatorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#RegisterCreator().(id string, creator ConfigCreatorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreator#) error { |
| 13 | if _, found := v[id]; found { |
| 14 | return newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/newError().(id, " already registered.").AtErrorscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtError().() |
| 15 | } |
| 16 | |
| 17 | v[id] = creator |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func (v ConfigCreatorCachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#) CreateConfigscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#CreateConfig().(id string) (interface{}, error) { |
| 22 | creator, found := v[id] |
| 23 | if !found { |
| 24 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/newError().("unknown config id: ", id) |
| 25 | } |
| 26 | return creator(), nil |
| 27 | } |
| 28 | |
| 29 | type JSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader# struct { |
| 30 | cachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#cache. ConfigCreatorCachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache# |
| 31 | idKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#idKey. string |
| 32 | configKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#configKey. string |
| 33 | } |
| 34 | |
| 35 | func NewJSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/NewJSONConfigLoader().(cache ConfigCreatorCachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#, idKey string, configKey string) *JSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader# { |
| 36 | return &JSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#{ |
| 37 | idKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#idKey.: idKey, |
| 38 | configKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#configKey.: configKey, |
| 39 | cachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#cache.: cache, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func (v *JSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#) LoadWithIDscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#LoadWithID().(raw []byte, id string) (interface{}, error) { |
| 44 | id = stringsscip-go gomod github.com/golang/go/src go1.15 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.15 strings/ToLower().(id) |
| 45 | config, err := v.cachescip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#cache..CreateConfigscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/ConfigCreatorCache#CreateConfig().(id) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | if err := jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/Unmarshal().(raw, config); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return config, nil |
| 53 | } |
| 54 | |
| 55 | func (v *JSONConfigLoaderscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#) Loadscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#Load().(raw []byte) (interface{}, string, error) { |
| 56 | var obj map[string]jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.15 `encoding/json`/RawMessage# |
| 57 | if err := jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/Unmarshal().(raw, &obj); err != nil { |
| 58 | return nil, "", err |
| 59 | } |
| 60 | rawID, found := obj[v.idKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#idKey.] |
| 61 | if !found { |
| 62 | return nil, "", newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/newError().(v.idKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#idKey., " not found in JSON context").AtErrorscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtError().() |
| 63 | } |
| 64 | var id string |
| 65 | if err := jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/Unmarshal().(rawID, &id); err != nil { |
| 66 | return nil, "", err |
| 67 | } |
| 68 | rawConfig := jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.15 `encoding/json`/RawMessage#(raw) |
| 69 | if len(v.configKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#configKey.) > 0 { |
| 70 | configValue, found := obj[v.configKeyscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#configKey.] |
| 71 | if found { |
| 72 | rawConfig = configValue |
| 73 | } else { |
| 74 | // Default to empty json object. |
| 75 | rawConfig = jsonscip-go gomod github.com/golang/go/src go1.15 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.15 `encoding/json`/RawMessage#([]byte("{}")) |
| 76 | } |
| 77 | } |
| 78 | config, err := v.LoadWithIDscip-go gomod v2ray.com/core . `v2ray.com/core/infra/conf`/JSONConfigLoader#LoadWithID().([]byte(rawConfig), id) |
| 79 | if err != nil { |
| 80 | return nil, id, err |
| 81 | } |
| 82 | return config, id, nil |
| 83 | } |