Skip to content

Commit

Permalink
remove use of any
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Oct 16, 2016
1 parent 5d9e6b0 commit e33b7df
Show file tree
Hide file tree
Showing 72 changed files with 727 additions and 791 deletions.
8 changes: 5 additions & 3 deletions common/loader/json_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"v2ray.com/core/common/log"
)

type NamedTypeMap map[string]string

type JSONConfigLoader struct {
cache ConfigCreatorCache
cache NamedTypeMap
idKey string
configKey string
}

func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
func NewJSONConfigLoader(cache NamedTypeMap, idKey string, configKey string) *JSONConfigLoader {
return &JSONConfigLoader{
idKey: idKey,
configKey: configKey,
Expand All @@ -24,7 +26,7 @@ func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey strin
}

func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
config, err := this.cache.CreateConfig(id)
config, err := GetInstance(this.cache[id])
if err != nil {
return nil, err
}
Expand Down
50 changes: 50 additions & 0 deletions common/loader/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package loader

import (
"errors"
"reflect"

"github.com/golang/protobuf/proto"
)

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)
if mType == nil {
return nil, errors.New("Unknown type: " + messageType)
}
return reflect.New(mType).Interface(), nil
}

func (this *TypedSettings) Load(message proto.Message) error {
targetType := GetType(message)
if targetType != this.Type {
return errors.New("Have type " + this.Type + ", but retrieved for " + targetType)
}
return proto.Unmarshal(this.Settings, message)
}

func (this *TypedSettings) GetInstance() (interface{}, error) {
instance, err := GetInstance(this.Type)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(this.Settings, instance.(proto.Message)); err != nil {
return nil, err
}
return instance, nil
}
59 changes: 59 additions & 0 deletions common/loader/type.pb.go

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

11 changes: 11 additions & 0 deletions common/loader/type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package v2ray.core.common.loader;
option go_package = "loader";
option java_package = "com.v2ray.core.common.loader";
option java_outer_classname = "TypeProto";

message TypedSettings {
string type = 1;
bytes settings = 2;
}
42 changes: 0 additions & 42 deletions common/log/config_json.go

This file was deleted.

27 changes: 0 additions & 27 deletions common/net/address_json.go

This file was deleted.

40 changes: 0 additions & 40 deletions common/net/address_json_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions common/protocol/server_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func (this *ServerSpec) HasUser(user *User) bool {
this.RLock()
defer this.RUnlock()

accountA, err := user.GetTypedAccount(this.newAccount())
accountA, err := user.GetTypedAccount()
if err != nil {
return false
}
for _, u := range this.users {
accountB, err := u.GetTypedAccount(this.newAccount())
accountB, err := u.GetTypedAccount()
if err == nil && accountA.Equals(accountB) {
return true
}
Expand Down
30 changes: 15 additions & 15 deletions common/protocol/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ package protocol

import (
"errors"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
)

var (
ErrUserMissing = errors.New("User is not specified.")
ErrAccountMissing = errors.New("Account is not specified.")
ErrNonMessageType = errors.New("Not a protobuf message.")
ErrUserMissing = errors.New("User is not specified.")
ErrAccountMissing = errors.New("Account is not specified.")
ErrNonMessageType = errors.New("Not a protobuf message.")
ErrUnknownAccountType = errors.New("Unknown account type.")
)

func (this *User) GetTypedAccount(account AsAccount) (Account, error) {
anyAccount := this.GetAccount()
if anyAccount == nil {
func (this *User) GetTypedAccount() (Account, error) {
if this.GetAccount() == nil {
return nil, ErrAccountMissing
}
protoAccount, ok := account.(proto.Message)
if !ok {
return nil, ErrNonMessageType
}
err := ptypes.UnmarshalAny(anyAccount, protoAccount)

rawAccount, err := this.Account.GetInstance()
if err != nil {
return nil, err
}
return account.AsAccount()
if asAccount, ok := rawAccount.(AsAccount); ok {
return asAccount.AsAccount()
}
if account, ok := rawAccount.(Account); ok {
return account, nil
}
return nil, errors.New("Unknown account type: " + this.Account.Type)
}

func (this *User) GetSettings() UserSettings {
Expand Down
37 changes: 19 additions & 18 deletions common/protocol/user.pb.go

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

Loading

0 comments on commit e33b7df

Please sign in to comment.