1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

replace ioutil with io package and other minor improvements

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-06-17 22:03:18 +03:00
parent 20d2855a66
commit ef2643b046
108 changed files with 1069 additions and 1021 deletions

View File

@@ -78,8 +78,13 @@ func (c Config) Validate() Config {
}
if c.SessionIDGenerator == nil {
c.SessionIDGenerator = func(*context.Context) string {
id, _ := uuid.NewRandom()
c.SessionIDGenerator = func(ctx *context.Context) string {
id, err := uuid.NewRandom()
if err != nil {
ctx.StopWithError(400, err)
return ""
}
return id.String()
}
}

View File

@@ -94,24 +94,28 @@ func (s *mem) OnUpdateExpiration(string, time.Duration) error { return nil }
// immutable depends on the store, it may not implement it at all.
func (s *mem) Set(sid string, key string, value interface{}, _ time.Duration, immutable bool) error {
s.mu.RLock()
s.values[sid].Save(key, value, immutable)
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
store.Save(key, value, immutable)
}
return nil
}
func (s *mem) Get(sid string, key string) interface{} {
s.mu.RLock()
v := s.values[sid].Get(key)
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
return store.Get(key)
}
return v
return nil
}
func (s *mem) Decode(sid string, key string, outPtr interface{}) error {
s.mu.RLock()
v := s.values[sid].Get(key)
s.mu.RUnlock()
v := s.Get(sid, key)
if v != nil {
reflect.ValueOf(outPtr).Set(reflect.ValueOf(v))
}
@@ -119,29 +123,45 @@ func (s *mem) Decode(sid string, key string, outPtr interface{}) error {
}
func (s *mem) Visit(sid string, cb func(key string, value interface{})) error {
s.values[sid].Visit(cb)
s.mu.RLock()
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
store.Visit(cb)
}
return nil
}
func (s *mem) Len(sid string) int {
s.mu.RLock()
n := s.values[sid].Len()
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
return store.Len()
}
return n
return 0
}
func (s *mem) Delete(sid string, key string) (deleted bool) {
s.mu.RLock()
deleted = s.values[sid].Remove(key)
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
deleted = store.Remove(key)
}
return
}
func (s *mem) Clear(sid string) error {
s.mu.Lock()
s.values[sid].Reset()
s.mu.Unlock()
s.mu.RLock()
store, ok := s.values[sid]
s.mu.RUnlock()
if ok {
store.Reset()
}
return nil
}
@@ -150,7 +170,6 @@ func (s *mem) Release(sid string) error {
s.mu.Lock()
delete(s.values, sid)
s.mu.Unlock()
return nil
}

View File

@@ -50,7 +50,6 @@ func (p *provider) newSession(man *Sessions, sid string, expires time.Duration)
sid: sid,
Man: man,
provider: p,
flashes: make(map[string]*flashMessage),
}
onExpire := func() {
@@ -99,9 +98,10 @@ func (p *provider) EndRequest(ctx *context.Context, session *Session) {
// ErrNotFound may be returned from `UpdateExpiration` of a non-existing or
// invalid session entry from memory storage or databases.
// Usage:
// if err != nil && err.Is(err, sessions.ErrNotFound) {
// [handle error...]
// }
//
// if err != nil && err.Is(err, sessions.ErrNotFound) {
// [handle error...]
// }
var ErrNotFound = errors.New("session not found")
// UpdateExpiration resets the expiration of a session.

View File

@@ -76,6 +76,7 @@ func (s *Session) runFlashGC() {
delete(s.flashes, key)
}
}
s.mu.Unlock()
}
@@ -558,6 +559,10 @@ func (s *Session) SetImmutable(key string, value interface{}) {
// If you want to define more than one flash messages, you will have to use different keys.
func (s *Session) SetFlash(key string, value interface{}) {
s.mu.Lock()
if s.flashes == nil {
s.flashes = make(map[string]*flashMessage)
}
s.flashes[key] = &flashMessage{value: value}
s.mu.Unlock()
}

View File

@@ -35,7 +35,7 @@ func testSessions(t *testing.T, app *iris.Application) {
s := sessions.Get(ctx)
sessValues := s.GetAll()
_, err := ctx.JSON(sessValues)
err := ctx.JSON(sessValues)
if err != nil {
t.Fatal(err)
}
@@ -141,8 +141,7 @@ func TestFlashMessages(t *testing.T) {
}
writeValues := func(ctx *context.Context, values map[string]interface{}) error {
_, err := ctx.JSON(values)
return err
return ctx.JSON(values)
}
app.Post("/set", func(ctx *context.Context) {