Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use ScanStruct instead of a manual for loop to get back user informat…

…ion from redis
  • Loading branch information...
commit dd31d9f568a7022c4e4c46b0ce02be322cf019bf 1 parent dddb785
@gsempe authored
Showing with 7 additions and 16 deletions.
  1. +2 −2 model.go
  2. +5 −14 retwis.go
View
4 model.go
@@ -8,8 +8,8 @@ import (
type User struct {
Id string
- Username string
- Auth string
+ Username string `redis:"username"`
+ Auth string `redis:"auth"`
}
type Post struct {
View
19 retwis.go
@@ -30,24 +30,15 @@ func isLogin(auth string) (*User, error) {
func loadUserInfo(userId string) (*User, error) {
- v, err := redis.Strings(conn.Do("HGETALL", "user:"+userId))
+ v, err := redis.Values(conn.Do("HGETALL", "user:"+userId))
if err != nil {
return nil, err
}
- var username, auth string
- for i := 0; i < len(v); i++ {
- if v[i] == "username" {
- username = v[i+1]
- i++
- continue
- }
- if v[i] == "auth" {
- auth = v[i+1]
- i++
- continue
- }
+ u := &User{Id: userId}
+ err = redis.ScanStruct(v, u)
+ if err != nil {
+ return nil, err
}
- u := &User{Id: userId, Username: username, Auth: auth}
return u, nil
}
Please sign in to comment.
Something went wrong with that request. Please try again.