Skip to content

Commit

Permalink
rename TypedSettings to TypedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Dec 15, 2016
1 parent 5bbbdc0 commit 50bc195
Show file tree
Hide file tree
Showing 52 changed files with 451 additions and 461 deletions.
4 changes: 2 additions & 2 deletions app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"

"github.com/miekg/dns"
)
Expand Down Expand Up @@ -125,5 +125,5 @@ func (v CacheServerFactory) AppId() app.ID {
}

func init() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), CacheServerFactory{})
app.RegisterApplicationFactory(serial.GetMessageType(new(Config)), CacheServerFactory{})
}
4 changes: 2 additions & 2 deletions app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
)

Expand Down Expand Up @@ -125,5 +125,5 @@ func (RouterFactory) AppId() app.ID {
}

func init() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
app.RegisterApplicationFactory(serial.GetMessageType(new(Config)), RouterFactory{})
}
48 changes: 24 additions & 24 deletions app/web/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/web/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ option go_package = "web";
option java_package = "com.v2ray.core.app.web";
option java_outer_classname = "ConfigProto";

import "v2ray.com/core/common/loader/type.proto";
import "v2ray.com/core/common/serial/typed_message.proto";

message FileServer {
message Entry {
Expand All @@ -21,7 +21,7 @@ message FileServer {

message Server {
repeated string domain = 1;
v2ray.core.common.loader.TypedSettings settings = 2;
v2ray.core.common.serial.TypedMessage settings = 2;
}

message Config {
Expand Down
50 changes: 0 additions & 50 deletions common/loader/type.go

This file was deleted.

62 changes: 0 additions & 62 deletions common/loader/type.pb.go

This file was deleted.

14 changes: 0 additions & 14 deletions common/loader/type.proto

This file was deleted.

34 changes: 17 additions & 17 deletions common/protocol/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions common/protocol/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ option go_package = "protocol";
option java_package = "com.v2ray.core.common.protocol";
option java_outer_classname = "UserProto";

import "v2ray.com/core/common/loader/type.proto";
import "v2ray.com/core/common/serial/typed_message.proto";

message User {
uint32 level = 1;
string email = 2;

// Protocol specific account information.
v2ray.core.common.loader.TypedSettings account = 3;
v2ray.core.common.serial.TypedMessage account = 3;
}
42 changes: 42 additions & 0 deletions common/serial/typed_message.go
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
}
Loading

0 comments on commit 50bc195

Please sign in to comment.