Skip to content

Commit

Permalink
fix: refactor store names to fix stutter
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed Sep 5, 2018
1 parent dd795d7 commit f7056d2
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 82 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Sessions can be stored to any backend by implementing the [store](/store.go) int

* [in-memory](/stores/memory)
* [redis](/stores/redis)
* [securecookie](/stores/securecookie)
* [secure cookie](/stores/securecookie)

# Usage
Check the [examples](/examples) directory for complete examples.
Expand All @@ -33,7 +33,7 @@ Stores can be registered to a session instance by using `Use` method.

```go
sess := simplesessions.New(simplesessions.Options{})
sess.UseStore(memorystore.New())
sess.UseStore(memory.New())
```

## Connecting an HTTP handler
Expand Down Expand Up @@ -94,7 +94,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
// call `Commit` method when you set all the values.
err = sess.Set("somekey", "somevalue")
err = sess.Set("someotherkey", 10)
err = sess.commit()
err = sess.Commit()

// Use `Get` method to get a field from current session. The result will be an interface
// so you can use helper methods like
Expand Down Expand Up @@ -124,7 +124,7 @@ func main() {
// cookie domain, is secure cookie etc. Check `Options` struct for more options.
sessMan := simplesessions.New(simplesessions.Options{})
// Create a new store instance and attach to session manager
sessMan.UseStore(memorystore.New())
sessMan.UseStore(memory.New())
// Register callbacks for read and write cookie
// Get cookie callback should get cookie based on cookie name and
// sent back in net/http cookie format.
Expand Down
2 changes: 1 addition & 1 deletion examples/fasthttp-inmemory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func setCookie(cookie *http.Cookie, w interface{}) error {

func main() {
sessionManager = simplesessions.New(simplesessions.Options{})
sessionManager.UseStore(memorystore.New())
sessionManager.UseStore(memory.New())
sessionManager.RegisterGetCookie(getCookie)
sessionManager.RegisterSetCookie(setCookie)

Expand Down
2 changes: 1 addition & 1 deletion examples/fasthttp-redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gomodule/redigo/redis"
"github.com/valyala/fasthttp"
"github.com/vividvilla/simplesessions"
"github.com/vividvilla/simplesessions/stores/redis"
redisstore "github.com/vividvilla/simplesessions/stores/redis"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion examples/nethttp-inmemory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func setCookie(cookie *http.Cookie, w interface{}) error {

func main() {
sessionManager = simplesessions.New(simplesessions.Options{})
sessionManager.UseStore(memorystore.New())
sessionManager.UseStore(memory.New())
sessionManager.RegisterGetCookie(getCookie)
sessionManager.RegisterSetCookie(setCookie)

Expand Down
2 changes: 1 addition & 1 deletion examples/nethttp-redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/gomodule/redigo/redis"
"github.com/vividvilla/simplesessions"
"github.com/vividvilla/simplesessions/stores/redis"
redisstore "github.com/vividvilla/simplesessions/stores/redis"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion examples/nethttp-secure-cookie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func setCookie(cookie *http.Cookie, w interface{}) error {

func main() {
sessionManager = simplesessions.New(simplesessions.Options{})
sessionManager.UseStore(securecookiestore.New(
sessionManager.UseStore(securecookie.New(
[]byte("0dIHy6S2uBuKaNnTUszB218L898ikGYA"),
[]byte("0dIHy6S2uBuKaNnTUszB218L898ikGYA"),
))
Expand Down
44 changes: 22 additions & 22 deletions stores/memory/memory_store.go → stores/memory/store.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package memorystore
package memory

import (
"sync"
Expand All @@ -10,35 +10,35 @@ const (
sessionIDLen = 32
)

// MemoryStore represents in-memory session store
type MemoryStore struct {
// Store represents in-memory session store
type Store struct {
// map to store all sessions and its values
sessions map[string]map[string]interface{}

mu sync.RWMutex
}

// New creates a new in-memory store instance
func New() *MemoryStore {
return &MemoryStore{
func New() *Store {
return &Store{
sessions: make(map[string]map[string]interface{}),
}
}

// isValidSessionID checks is the given session id is valid.
func (s *MemoryStore) isValidSessionID(sess *simplesessions.Session, id string) bool {
func (s *Store) isValidSessionID(sess *simplesessions.Session, id string) bool {
return len(id) == sessionIDLen && sess.IsValidRandomString(id)
}

// IsValid checks if the session is set for the id
func (s *MemoryStore) IsValid(sess *simplesessions.Session, id string) (bool, error) {
func (s *Store) IsValid(sess *simplesessions.Session, id string) (bool, error) {
return s.isValidSessionID(sess, id), nil
}

// Create creates a new session id and returns it. This doesn't create the session in
// sessions map since memory can be saved by not storing empty sessions and system
// can not be stressed by just creating new sessions
func (s *MemoryStore) Create(sess *simplesessions.Session) (string, error) {
func (s *Store) Create(sess *simplesessions.Session) (string, error) {
id, err := sess.GenerateRandomString(sessionIDLen)
if err != nil {
return "", err
Expand All @@ -48,7 +48,7 @@ func (s *MemoryStore) Create(sess *simplesessions.Session) (string, error) {
}

// Get gets a field in session
func (s *MemoryStore) Get(sess *simplesessions.Session, id, key string) (interface{}, error) {
func (s *Store) Get(sess *simplesessions.Session, id, key string) (interface{}, error) {
if !s.isValidSessionID(sess, id) {
return nil, simplesessions.ErrInvalidSession
}
Expand All @@ -72,7 +72,7 @@ func (s *MemoryStore) Get(sess *simplesessions.Session, id, key string) (interfa
}

// GetMulti gets a map for values for multiple keys. If key is not present in session then nil is returned.
func (s *MemoryStore) GetMulti(sess *simplesessions.Session, id string, keys ...string) (map[string]interface{}, error) {
func (s *Store) GetMulti(sess *simplesessions.Session, id string, keys ...string) (map[string]interface{}, error) {
// Check if valid session
if !s.isValidSessionID(sess, id) {
return nil, simplesessions.ErrInvalidSession
Expand Down Expand Up @@ -101,7 +101,7 @@ func (s *MemoryStore) GetMulti(sess *simplesessions.Session, id string, keys ...
}

// GetAll gets all fields in session
func (s *MemoryStore) GetAll(sess *simplesessions.Session, id string) (map[string]interface{}, error) {
func (s *Store) GetAll(sess *simplesessions.Session, id string) (map[string]interface{}, error) {
// Check if valid session
if !s.isValidSessionID(sess, id) {
return nil, simplesessions.ErrInvalidSession
Expand All @@ -115,7 +115,7 @@ func (s *MemoryStore) GetAll(sess *simplesessions.Session, id string) (map[strin
}

// Set sets a value to given session but stored only on commit
func (s *MemoryStore) Set(sess *simplesessions.Session, id, key string, val interface{}) error {
func (s *Store) Set(sess *simplesessions.Session, id, key string, val interface{}) error {
// Check if valid session
if !s.isValidSessionID(sess, id) {
return simplesessions.ErrInvalidSession
Expand All @@ -135,12 +135,12 @@ func (s *MemoryStore) Set(sess *simplesessions.Session, id, key string, val inte
}

// Commit does nothing here since Set sets the value.
func (s *MemoryStore) Commit(sess *simplesessions.Session, id string) error {
func (s *Store) Commit(sess *simplesessions.Session, id string) error {
return nil
}

// Delete deletes a key from session.
func (s *MemoryStore) Delete(sess *simplesessions.Session, id string, key string) error {
func (s *Store) Delete(sess *simplesessions.Session, id string, key string) error {
// Check if valid session
if !s.isValidSessionID(sess, id) {
return simplesessions.ErrInvalidSession
Expand All @@ -161,7 +161,7 @@ func (s *MemoryStore) Delete(sess *simplesessions.Session, id string, key string
}

// Clear clears session in redis.
func (s *MemoryStore) Clear(sess *simplesessions.Session, id string) error {
func (s *Store) Clear(sess *simplesessions.Session, id string) error {
// Check if valid session
if !s.isValidSessionID(sess, id) {
return simplesessions.ErrInvalidSession
Expand All @@ -179,7 +179,7 @@ func (s *MemoryStore) Clear(sess *simplesessions.Session, id string) error {
}

// Int is a helper method to type assert as integer
func (s *MemoryStore) Int(r interface{}, err error) (int, error) {
func (s *Store) Int(r interface{}, err error) (int, error) {
if err != nil {
return 0, err
}
Expand All @@ -193,7 +193,7 @@ func (s *MemoryStore) Int(r interface{}, err error) (int, error) {
}

// Int64 is a helper method to type assert as Int64
func (s *MemoryStore) Int64(r interface{}, err error) (int64, error) {
func (s *Store) Int64(r interface{}, err error) (int64, error) {
if err != nil {
return 0, err
}
Expand All @@ -207,7 +207,7 @@ func (s *MemoryStore) Int64(r interface{}, err error) (int64, error) {
}

// UInt64 is a helper method to type assert as UInt64
func (s *MemoryStore) UInt64(r interface{}, err error) (uint64, error) {
func (s *Store) UInt64(r interface{}, err error) (uint64, error) {
if err != nil {
return 0, err
}
Expand All @@ -221,7 +221,7 @@ func (s *MemoryStore) UInt64(r interface{}, err error) (uint64, error) {
}

// Float64 is a helper method to type assert as Float64
func (s *MemoryStore) Float64(r interface{}, err error) (float64, error) {
func (s *Store) Float64(r interface{}, err error) (float64, error) {
if err != nil {
return 0, err
}
Expand All @@ -235,7 +235,7 @@ func (s *MemoryStore) Float64(r interface{}, err error) (float64, error) {
}

// String is a helper method to type assert as String
func (s *MemoryStore) String(r interface{}, err error) (string, error) {
func (s *Store) String(r interface{}, err error) (string, error) {
if err != nil {
return "", err
}
Expand All @@ -249,7 +249,7 @@ func (s *MemoryStore) String(r interface{}, err error) (string, error) {
}

// Bytes is a helper method to type assert as Bytes
func (s *MemoryStore) Bytes(r interface{}, err error) ([]byte, error) {
func (s *Store) Bytes(r interface{}, err error) ([]byte, error) {
if err != nil {
return nil, err
}
Expand All @@ -263,7 +263,7 @@ func (s *MemoryStore) Bytes(r interface{}, err error) ([]byte, error) {
}

// Bool is a helper method to type assert as Bool
func (s *MemoryStore) Bool(r interface{}, err error) (bool, error) {
func (s *Store) Bool(r interface{}, err error) (bool, error) {
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package memorystore
package memory

import (
"errors"
Expand Down
Loading

0 comments on commit f7056d2

Please sign in to comment.