Skip to content

Commit

Permalink
refactor: support get and must get function to get data
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 8, 2021
1 parent f53b9da commit 15c87db
Show file tree
Hide file tree
Showing 4 changed files with 44 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,6 +6,6 @@ require (
github.com/hashicorp/golang-lru v0.5.4
github.com/spf13/cast v1.4.1
github.com/stretchr/testify v1.7.0
github.com/vicanso/elton v1.4.3
github.com/vicanso/elton v1.5.0
github.com/vicanso/hes v0.3.9
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -18,6 +19,8 @@ github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vicanso/elton v1.4.3 h1:D7fZZteCZ+gp8kltfLgE85uEzzxurEAHjc0PQG+rXeY=
github.com/vicanso/elton v1.4.3/go.mod h1:BFhCB2ke3uPLo0Ids8wgYmNeq5nbivqvHtfwIX8PY/c=
github.com/vicanso/elton v1.5.0 h1:Id+CVDHBpvknq8AOI1dowwVVonU5JqH/jBxGCoR/PxQ=
github.com/vicanso/elton v1.5.0/go.mod h1:BFhCB2ke3uPLo0Ids8wgYmNeq5nbivqvHtfwIX8PY/c=
github.com/vicanso/hes v0.3.9 h1:IO21yElX6Xp3w+Lc1O2QIySrJj2jEhnl5dWbqbDYunc=
github.com/vicanso/hes v0.3.9/go.mod h1:B0l1NIQM/nYw7owAd+hyHuNnAD8Nsx0T6duhVxmXUBY=
github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0=
Expand Down
25 changes: 24 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,29 @@ func (s *Session) Commit(ctx context.Context, ttl time.Duration) error {
return nil
}

// Get gets the session data from context
func Get(c *elton.Context) (*Session, bool) {
value, ok := c.Get(Key)
if !ok {
return nil, false
}
s, ok := value.(*Session)
if !ok {
return nil, false
}
return s, true
}

// MustGet gets the session data from context,
// if not exists, it will be panic.
func MustGet(c *elton.Context) *Session {
se, _ := Get(c)
if se == nil {
panic("session is nil")
}
return se
}

// New create a new session middleware
func New(config Config) elton.Handler {
store := config.Store
Expand All @@ -408,7 +431,7 @@ func New(config Config) elton.Handler {
if skipper(c) {
return c.Next()
}
_, exists := c.Get(Key)
_, exists := Get(c)
if exists {
return c.Next()
}
Expand Down
16 changes: 16 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,23 @@ func TestNotFetchError(t *testing.T) {
err = s.Refresh(ctx)
assert.Nil(err)
assert.NotEmpty(s.updatedAt)
}

func TestSessionGet(t *testing.T) {
assert := assert.New(t)
c := elton.NewContext(nil, nil)
se, exists := Get(c)
assert.False(exists)
assert.Nil(se)

sess := &Session{}
c.Set(Key, sess)
se, exists = Get(c)
assert.True(exists)
assert.Equal(sess, se)

se = MustGet(c)
assert.Equal(sess, se)
}

func TestSessionMiddleware(t *testing.T) {
Expand Down

0 comments on commit 15c87db

Please sign in to comment.