forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename TypedSettings to TypedMessage
- Loading branch information
1 parent
5bbbdc0
commit 50bc195
Showing
52 changed files
with
451 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package serial | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
func ToTypedMessage(message proto.Message) *TypedMessage { | ||
if message == nil { | ||
return nil | ||
} | ||
settings, _ := proto.Marshal(message) | ||
return &TypedMessage{ | ||
Type: GetMessageType(message), | ||
Value: settings, | ||
} | ||
} | ||
|
||
func GetMessageType(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 *TypedMessage) GetInstance() (interface{}, error) { | ||
instance, err := GetInstance(v.Type) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := proto.Unmarshal(v.Value, instance.(proto.Message)); err != nil { | ||
return nil, err | ||
} | ||
return instance, nil | ||
} |
Oops, something went wrong.