auth

Paddy 2014-12-13 Parent:d43c3fbf00f3 Child:09c47387e455

89:229422395721 Go to Latest

auth/session.go

Sort sessions before returning them. When using the memstore, sort sessions by the date they were created, descending, before returning them.

History
     1.1 --- a/session.go	Sat Dec 13 19:05:06 2014 -0500
     1.2 +++ b/session.go	Sat Dec 13 19:11:38 2014 -0500
     1.3 @@ -2,6 +2,7 @@
     1.4  
     1.5  import (
     1.6  	"errors"
     1.7 +	"sort"
     1.8  	"time"
     1.9  
    1.10  	"code.secondbit.org/uuid"
    1.11 @@ -30,6 +31,20 @@
    1.12  	Active    bool
    1.13  }
    1.14  
    1.15 +type sortedSessions []Session
    1.16 +
    1.17 +func (s sortedSessions) Len() int {
    1.18 +	return len(s)
    1.19 +}
    1.20 +
    1.21 +func (s sortedSessions) Less(i, j int) bool {
    1.22 +	return s[i].Created.After(s[j].Created)
    1.23 +}
    1.24 +
    1.25 +func (s sortedSessions) Swap(i, j int) {
    1.26 +	s[i], s[j] = s[j], s[i]
    1.27 +}
    1.28 +
    1.29  type sessionStore interface {
    1.30  	createSession(session Session) error
    1.31  	getSession(id string) (Session, error)
    1.32 @@ -82,6 +97,8 @@
    1.33  		}
    1.34  		res = append(res, session)
    1.35  	}
    1.36 -	// BUG(paddy): sessions should return sorted by date created
    1.37 +	sorted := sortedSessions(res)
    1.38 +	sort.Sort(sorted)
    1.39 +	res = []Session(sorted)
    1.40  	return res, nil
    1.41  }