diff --git a/go.mod b/go.mod index 3f9f870..6dfd72f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e2a31f1..47729fe 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/session.go b/session.go index b819e64..23fec0e 100644 --- a/session.go +++ b/session.go @@ -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 @@ -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() } diff --git a/session_test.go b/session_test.go index 0fc5569..315de7d 100644 --- a/session_test.go +++ b/session_test.go @@ -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) {