Skip to content

Commit

Permalink
fix: incorrect store Delete() method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed May 31, 2024
1 parent 35b9c77 commit 51d4ed2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions stores/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ func (s *Store) SetMulti(id string, data map[string]interface{}) error {
}

// Delete deletes a key from session.
func (s *Store) Delete(id string, key string) error {
func (s *Store) Delete(id string, keys ...string) error {
s.mu.Lock()
defer s.mu.Unlock()

_, ok := s.sessions[id]
if !ok {
return ErrInvalidSession
}
delete(s.sessions[id], key)

for _, k := range keys {
delete(s.sessions[id], k)
}

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions stores/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"time"

"github.com/lib/pq"
_ "github.com/lib/pq"
)

Expand Down Expand Up @@ -203,8 +204,8 @@ func (s *Store) SetMulti(id string, data map[string]interface{}) (err error) {
}

// Delete deletes a key from redis session hashmap.
func (s *Store) Delete(id string, key string) error {
res, err := s.q.delete.Exec(id, key)
func (s *Store) Delete(id string, keys ...string) error {
res, err := s.q.delete.Exec(id, pq.Array(keys))
if err != nil {
return err
}
Expand Down Expand Up @@ -388,7 +389,7 @@ func (s *Store) prepareQueries() (*queries, error) {
return nil, err
}

q.delete, err = s.db.Prepare(fmt.Sprintf("UPDATE %s SET data = data - $2 WHERE id=$1", s.opt.Table))
q.delete, err = s.db.Prepare(fmt.Sprintf("UPDATE %s SET data = data #- $2 WHERE id=$1", s.opt.Table))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions stores/redis/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func (s *Store) SetMulti(id string, data map[string]interface{}) error {
}

// Delete deletes a key from redis session hashmap.
func (s *Store) Delete(id string, key string) error {
return s.client.HDel(s.clientCtx, s.prefix+id, key).Err()
func (s *Store) Delete(id string, keys ...string) error {
return s.client.HDel(s.clientCtx, s.prefix+id, keys...).Err()
}

// Clear clears session in redis.
Expand Down
8 changes: 5 additions & 3 deletions stores/securecookie/secure_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,17 @@ func (s *Store) Flush(cv string) (string, error) {
// Delete deletes a field from session. Once called, Flush() should be
// called to retrieve the updated, unflushed values and written to the cookie
// externally.
func (s *Store) Delete(cv, key string) error {
func (s *Store) Delete(cv string, keys ...string) error {
// Decode current cookie
vals, err := s.decode(cv)
if err != nil {
return ErrInvalidSession
}

// Delete given key in current values.
delete(vals, key)
for _, k := range keys {
// Delete given key in current values.
delete(vals, k)
}

// Create session map if doesn't exist.
s.mu.Lock()
Expand Down

0 comments on commit 51d4ed2

Please sign in to comment.