reprogl
1package session2
3import (4"encoding/json"5"sync"6"time"7
8"xelbot.com/reprogl/security"9)
10
11type Status uint812
13const (14Unmodified Status = iota15Modified
16Destroyed
17)
18
19type deadline time.Time20
21type Store struct {22status Status
23data internalData
24mu sync.RWMutex25}
26
27type internalData struct {28values map[string]interface{}29identity security.Identity30deadline deadline
31}
32
33func newStore() *Store {34return &Store{35status: Unmodified,36data: internalData{37values: make(map[string]interface{}),38},39}40}
41
42func newStoreWithData(d internalData) *Store {43return &Store{44status: Unmodified,45data: d,46}47}
48
49func (d *deadline) UnmarshalJSON(b []byte) error {50var s string51if err := json.Unmarshal(b, &s); err != nil {52return err53}54
55t, err := time.Parse(time.RFC3339, s)56if err != nil {57return err58}59*d = deadline(t)60
61return nil62}
63
64func (d deadline) MarshalJSON() ([]byte, error) {65t := time.Time(d)66
67return json.Marshal(t.Format(time.RFC3339))68}
69
70func (s *Store) setModified() {71if s.status != Destroyed {72s.status = Modified73}74}
75
76func (d deadline) IsExpired() bool {77t := time.Time(d)78
79return t.Before(time.Now())80}
81