Skip to content

Commit

Permalink
add metadata support
Browse files Browse the repository at this point in the history
  • Loading branch information
swithek committed Nov 14, 2020
1 parent 36a047c commit 17be90b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/gomodule/redigo v1.8.2
github.com/rafaeljusto/redigomock v2.4.0+incompatible
github.com/stretchr/testify v1.5.1
github.com/swithek/sessionup v1.3.1
github.com/swithek/sessionup v1.4.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU=
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 h1:RAV05c0xOkJ3dZGS0JFybxFKZ2WMLabgx3uXnd7rpGs=
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
github.com/gomodule/redigo v1.8.2 h1:H5XSIre1MB5NbPYFp+i1NBbb5qN1W8Y8YAQoAYbkm8k=
github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -15,9 +17,13 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/swithek/sessionup v1.3.1 h1:FWDErKx4JsbG+7Z9NiUhKJfeiaArUpA6hrGe39LgIyE=
github.com/swithek/sessionup v1.3.1/go.mod h1:2Hw9qm+mH/p/6dEwqYeQl9pee8rqjrYDTJ2XhET9Oyg=
github.com/swithek/sessionup v1.4.0 h1:VEvJa+l/xj0PH15XDyXx8Bm0vcqKXhhmm1LO7FepBgU=
github.com/swithek/sessionup v1.4.0/go.mod h1:2Hw9qm+mH/p/6dEwqYeQl9pee8rqjrYDTJ2XhET9Oyg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
xojoc.pw/useragent v0.0.0-20170215185434-52903803fc66 h1:j5PlwzvW29USBoG/MvJPT5kDvX+0+lVLlOdnujOlN94=
xojoc.pw/useragent v0.0.0-20170215185434-52903803fc66/go.mod h1:71om/Qz9HbIEjbUrkrzmJiF26FSh6tcwqSFdBBkLtJQ=
xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe h1:KHyqPlOEFFT7OPh4WR7qFzNNndwj1VuwV+rZ+Tb3bio=
xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe/go.mod h1:71om/Qz9HbIEjbUrkrzmJiF26FSh6tcwqSFdBBkLtJQ=
33 changes: 33 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (r *RedisStore) Create(ctx context.Context, s sessionup.Session) error {
"ip", s.IP.String(),
"agent_os", s.Agent.OS,
"agent_browser", s.Agent.Browser,
"meta", metaToString(s.Meta),
)
if err != nil {
return err
Expand Down Expand Up @@ -356,6 +357,7 @@ func parse(vv map[string]string) (sessionup.Session, error) {
ID: vv["id"],
UserKey: vv["user_key"],
IP: net.ParseIP(vv["ip"]),
Meta: metaFromString(vv["meta"]),
}
s.Agent.OS = vv["agent_os"]
s.Agent.Browser = vv["agent_browser"]
Expand All @@ -373,3 +375,34 @@ func parse(vv map[string]string) (sessionup.Session, error) {

return s, nil
}

// metaToString converts metadata map into string.
func metaToString(mm map[string]string) string {
var b strings.Builder
for k, v := range mm {
b.WriteString(fmt.Sprintf("%s:%s;", k, v))
}

return b.String()
}

// metaFromString converts metadata string into map.
func metaFromString(s string) map[string]string {
if s == "" {
return nil
}

meta := make(map[string]string)
mm := strings.Split(s, ";")

for _, m := range mm {
vv := strings.Split(m, ":")
if len(vv) != 2 {
continue
}

meta[vv[0]] = vv[1]
}

return meta
}
46 changes: 45 additions & 1 deletion store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Test_RedisStore_Create(t *testing.T) {
ExpiresAt: time.Now().UTC().Add(time.Hour * 24),
CreatedAt: time.Now().UTC(),
IP: net.ParseIP("127.0.0.1"),
Meta: map[string]string{"test": "1"},
}
inp.Agent.OS = "gnu/linux"
inp.Agent.Browser = "firefox"
Expand Down Expand Up @@ -220,6 +221,7 @@ func Test_RedisStore_Create(t *testing.T) {
"ip", inp.IP.String(),
"agent_os", inp.Agent.OS,
"agent_browser", inp.Agent.Browser,
"meta", "test:1;",
).ExpectError(assert.AnError)
conn.GenericCommand("DISCARD")

Expand All @@ -230,7 +232,7 @@ func Test_RedisStore_Create(t *testing.T) {
},
Err: assert.AnError,
},
"Error returnde during session expiration creation": {
"Error returned during session expiration creation": {
Conn: func() (*redigomock.Conn, func(*testing.T)) {
conn := redigomock.NewConn()
conn.Command("WATCH", sKey)
Expand All @@ -250,6 +252,7 @@ func Test_RedisStore_Create(t *testing.T) {
"ip", inp.IP.String(),
"agent_os", inp.Agent.OS,
"agent_browser", inp.Agent.Browser,
"meta", "test:1;",
)
conn.Command("PEXPIREAT", sKey, inp.ExpiresAt.UnixNano()/int64(time.Millisecond)).ExpectError(assert.AnError)
conn.GenericCommand("DISCARD")
Expand Down Expand Up @@ -281,6 +284,7 @@ func Test_RedisStore_Create(t *testing.T) {
"ip", inp.IP.String(),
"agent_os", inp.Agent.OS,
"agent_browser", inp.Agent.Browser,
"meta", "test:1;",
)
conn.Command("PEXPIREAT", sKey, inp.ExpiresAt.UnixNano()/int64(time.Millisecond))
conn.GenericCommand("EXEC").ExpectError(assert.AnError)
Expand Down Expand Up @@ -312,6 +316,7 @@ func Test_RedisStore_Create(t *testing.T) {
"ip", inp.IP.String(),
"agent_os", inp.Agent.OS,
"agent_browser", inp.Agent.Browser,
"meta", "test:1;",
)
conn.Command("PEXPIREAT", sKey, inp.ExpiresAt.UnixNano()/int64(time.Millisecond))
conn.GenericCommand("EXEC")
Expand Down Expand Up @@ -342,6 +347,7 @@ func Test_RedisStore_Create(t *testing.T) {
"ip", inp.IP.String(),
"agent_os", inp.Agent.OS,
"agent_browser", inp.Agent.Browser,
"meta", "test:1;",
)
conn.Command("PEXPIREAT", sKey, inp.ExpiresAt.UnixNano()/int64(time.Millisecond))
conn.GenericCommand("EXEC")
Expand Down Expand Up @@ -405,6 +411,7 @@ func Test_RedisStore_FetchByID(t *testing.T) {
ExpiresAt: time.Now().UTC().Add(time.Hour * 24).Round(0),
CreatedAt: time.Now().UTC().Round(0),
IP: net.ParseIP("127.0.0.1"),
Meta: map[string]string{"test": "1", "": "val"},
}
inp.Agent.OS = "gnu/linux"
inp.Agent.Browser = "firefox"
Expand Down Expand Up @@ -452,6 +459,7 @@ func Test_RedisStore_FetchByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})

return conn, func(t *testing.T) {
Expand Down Expand Up @@ -494,6 +502,7 @@ func Test_RedisStore_FetchByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})

return conn, func(t *testing.T) {
Expand Down Expand Up @@ -561,6 +570,7 @@ func Test_RedisStore_FetchByUserKey(t *testing.T) {
ExpiresAt: time.Now().UTC().Add(time.Hour * 24).Round(0),
CreatedAt: time.Now().UTC().Round(0),
IP: net.ParseIP("127.0.0.1"),
Meta: map[string]string{"test": "1", "": "val"},
}
s.Agent.OS = "gnu/linux"
s.Agent.Browser = "firefox"
Expand Down Expand Up @@ -639,6 +649,7 @@ func Test_RedisStore_FetchByUserKey(t *testing.T) {
"ip": inp[0].IP.String(),
"agent_os": inp[0].Agent.OS,
"agent_browser": inp[0].Agent.Browser,
"meta": "test:1;:val;",
})

return conn, func(t *testing.T) {
Expand Down Expand Up @@ -682,6 +693,7 @@ func Test_RedisStore_FetchByUserKey(t *testing.T) {
"ip": inp[i].IP.String(),
"agent_os": inp[i].Agent.OS,
"agent_browser": inp[i].Agent.Browser,
"meta": "test:1;:val;",
})
}

Expand Down Expand Up @@ -748,6 +760,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
ExpiresAt: time.Now().UTC().Add(time.Hour * 24).Round(0),
CreatedAt: time.Now().UTC().Round(0),
IP: net.ParseIP("127.0.0.1"),
Meta: map[string]string{"test": "1", "": "val"},
}
inp.Agent.OS = "gnu/linux"
inp.Agent.Browser = "firefox"
Expand Down Expand Up @@ -810,6 +823,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.GenericCommand("UNWATCH")

Expand All @@ -832,6 +846,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey).ExpectError(assert.AnError)
conn.GenericCommand("UNWATCH")
Expand All @@ -855,6 +870,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectError(assert.AnError)
Expand All @@ -879,6 +895,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("123")
Expand All @@ -904,6 +921,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("123")
Expand All @@ -930,6 +948,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice(sKey)
Expand Down Expand Up @@ -957,6 +976,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("111", "222")
Expand Down Expand Up @@ -984,6 +1004,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("111", "222")
Expand Down Expand Up @@ -1037,6 +1058,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice(sKey)
Expand Down Expand Up @@ -1064,6 +1086,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("111")
Expand All @@ -1090,6 +1113,7 @@ func Test_RedisStore_DeleteByID(t *testing.T) {
"ip": inp.IP.String(),
"agent_os": inp.Agent.OS,
"agent_browser": inp.Agent.Browser,
"meta": "test:1;:val;",
})
conn.Command("WATCH", uKey)
conn.Command("ZRANGEBYSCORE", uKey, "-inf", "+inf").ExpectSlice("111", "222")
Expand Down Expand Up @@ -1492,3 +1516,23 @@ func Test_parse(t *testing.T) {
})
}
}

func Test_metaToString(t *testing.T) {
assert.Zero(t, metaToString(nil))

m := map[string]string{"": "1", "key": "", "test1": "2", "3": "", "hello": "hello"}
s := metaToString(m)
assert.Contains(t, s, ":1;")
assert.Contains(t, s, "test1:2;")
assert.Contains(t, s, "key:;")
assert.Contains(t, s, "hello:hello;")
}

func Test_metaFromString(t *testing.T) {
m := metaFromString("")
assert.Nil(t, m)

s := "test:1;:;3:3;"
m = metaFromString(s)
assert.Equal(t, map[string]string{"test": "1", "": "", "3": "3"}, m)
}

0 comments on commit 17be90b

Please sign in to comment.