github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/protocol/user.go (about)

     1  package protocol
     2  
     3  import "github.com/v2fly/v2ray-core/v5/common/serial"
     4  
     5  func (u *User) GetTypedAccount() (Account, error) {
     6  	if u.GetAccount() == nil {
     7  		return nil, newError("Account missing").AtWarning()
     8  	}
     9  
    10  	rawAccount, err := serial.GetInstanceOf(u.Account)
    11  	if err != nil {
    12  		return nil, err
    13  	}
    14  	if asAccount, ok := rawAccount.(AsAccount); ok {
    15  		return asAccount.AsAccount()
    16  	}
    17  	if account, ok := rawAccount.(Account); ok {
    18  		return account, nil
    19  	}
    20  	return nil, newError("Unknown account type: ", serial.V2Type(u.Account))
    21  }
    22  
    23  func (u *User) ToMemoryUser() (*MemoryUser, error) {
    24  	account, err := u.GetTypedAccount()
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return &MemoryUser{
    29  		Account: account,
    30  		Email:   u.Email,
    31  		Level:   u.Level,
    32  	}, nil
    33  }
    34  
    35  // MemoryUser is a parsed form of User, to reduce number of parsing of Account proto.
    36  type MemoryUser struct {
    37  	// Account is the parsed account of the protocol.
    38  	Account Account
    39  	Email   string
    40  	Level   uint32
    41  }