user.go 893 B

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