2021-09-07 08:02:09 -04:00
|
|
|
package taggedfeatures
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2021-09-07 08:02:09 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/v5cfg"
|
|
|
|
)
|
|
|
|
|
2021-11-27 04:16:41 -05:00
|
|
|
func LoadJSONConfig(ctx context.Context, interfaceType, defaultImpl string, message json.RawMessage) (*Config, error) {
|
2021-09-07 08:02:09 -04:00
|
|
|
type ItemStub struct {
|
|
|
|
MemberType string `json:"type"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Value json.RawMessage `json:"settings"`
|
|
|
|
}
|
|
|
|
type namedStub []ItemStub
|
|
|
|
var stub namedStub
|
|
|
|
err := json.Unmarshal(message, &stub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := &Config{Features: map[string]*anypb.Any{}}
|
|
|
|
for _, v := range stub {
|
|
|
|
if v.MemberType == "" {
|
|
|
|
v.MemberType = defaultImpl
|
|
|
|
}
|
2021-11-27 04:16:41 -05:00
|
|
|
pack, err := v5cfg.LoadHeterogeneousConfigFromRawJSON(ctx, interfaceType, v.MemberType, v.Value)
|
2021-09-07 08:02:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Features[v.Tag] = serial.ToTypedMessage(pack)
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|