1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/common/loader/type.go
2016-12-04 09:10:47 +01:00

51 lines
1.1 KiB
Go

package loader
import (
"reflect"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/errors"
)
func NewTypedSettings(message proto.Message) *TypedSettings {
if message == nil {
return nil
}
settings, _ := proto.Marshal(message)
return &TypedSettings{
Type: GetType(message),
Settings: settings,
}
}
func GetType(message proto.Message) string {
return proto.MessageName(message)
}
func GetInstance(messageType string) (interface{}, error) {
mType := proto.MessageType(messageType).Elem()
if mType == nil {
return nil, errors.New("Unknown type: " + messageType)
}
return reflect.New(mType).Interface(), nil
}
func (v *TypedSettings) Load(message proto.Message) error {
targetType := GetType(message)
if targetType != v.Type {
return errors.New("Have type " + v.Type + ", but retrieved for " + targetType)
}
return proto.Unmarshal(v.Settings, message)
}
func (v *TypedSettings) GetInstance() (interface{}, error) {
instance, err := GetInstance(v.Type)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(v.Settings, instance.(proto.Message)); err != nil {
return nil, err
}
return instance, nil
}