Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
gorilla#80 add context_legacy.go and context.go for go 1.7 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnps authored and Qifan Xi committed Aug 14, 2017
1 parent b61c93c commit 6aef9df
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
22 changes: 22 additions & 0 deletions context.go
@@ -0,0 +1,22 @@
// +build go1.7

package sessions

import (
"context"
"net/http"
)

func contextGet(r *http.Request, key contextKey) interface{} {
return r.Context().Value(key)
}

func contextSave(r *http.Request, key contextKey, val interface{}) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, key, val)
return r.WithContext(ctx)
}

func contextClear(r *http.Request) {
// no-op for go1.7+
}
22 changes: 22 additions & 0 deletions context_legacy.go
@@ -0,0 +1,22 @@
// +build !go1.7

package sessions

import (
"net/http"

"github.com/gorilla/context"
)

func contextGet(r *http.Request, key contextKey) interface{} {
return context.Get(r, key)
}

func contextSave(r *http.Request, key contextKey, val interface{}) *http.Request {
context.Set(r, key, val)
return r
}

func contextClear(r *http.Request) {
context.Clear(r)
}
16 changes: 8 additions & 8 deletions sessions.go
Expand Up @@ -9,8 +9,6 @@ import (
"fmt"
"net/http"
"time"

"github.com/gorilla/context"
)

// Default flashes key.
Expand Down Expand Up @@ -122,17 +120,17 @@ type contextKey int
const registryKey contextKey = 0

// GetRegistry returns a registry instance for the current request.
func GetRegistry(r *http.Request) *Registry {
registry := context.Get(r, registryKey)
func GetRegistry(r *http.Request) (*Registry, *http.Request) {
registry := contextGet(r, registryKey)
if registry != nil {
return registry.(*Registry)
return registry.(*Registry), r
}
newRegistry := &Registry{
request: r,
sessions: make(map[string]sessionInfo),
}
context.Set(r, registryKey, newRegistry)
return newRegistry
r = contextSave(r, registryKey, newRegistry)
return newRegistry, r
}

// Registry stores sessions used during a request.
Expand Down Expand Up @@ -186,7 +184,9 @@ func init() {

// Save saves all sessions used during the current request.
func Save(r *http.Request, w http.ResponseWriter) error {
return GetRegistry(r).Save(w)
var reg *Registry
reg, r = GetRegistry(r)
return reg.Save(w)
}

// NewCookie returns an http.Cookie with the options set. It also sets
Expand Down
8 changes: 6 additions & 2 deletions store.go
Expand Up @@ -77,7 +77,9 @@ type CookieStore struct {
// It returns a new session and an error if the session exists but could
// not be decoded.
func (s *CookieStore) Get(r *http.Request, name string) (*Session, error) {
return GetRegistry(r).Get(s, name)
var reg *Registry
reg, r = GetRegistry(r)
return reg.Get(s, name)
}

// New returns a session for the given name without adding it to the registry.
Expand Down Expand Up @@ -180,7 +182,9 @@ func (s *FilesystemStore) MaxLength(l int) {
//
// See CookieStore.Get().
func (s *FilesystemStore) Get(r *http.Request, name string) (*Session, error) {
return GetRegistry(r).Get(s, name)
var reg *Registry
reg, r = GetRegistry(r)
return reg.Get(s, name)
}

// New returns a session for the given name without adding it to the registry.
Expand Down

0 comments on commit 6aef9df

Please sign in to comment.