David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | ) |
| 6 | |
| 7 | type Storage interface { |
| 8 | Put(id string, update StatusMsg) error |
| 9 | Get(id string) (*StatusMsg, error) |
| 10 | List() ([]StatusMsg, error) |
| 11 | } |
| 12 | |
| 13 | type MemoryStorage struct { |
| 14 | Data map[string]StatusMsg |
| 15 | } |
| 16 | |
| 17 | func NewMemoryStorage() *MemoryStorage { |
| 18 | return &MemoryStorage{ |
| 19 | Data: make(map[string]StatusMsg), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func (s *MemoryStorage) Put(id string, update StatusMsg) error { |
| 24 | s.Data[id] = update |
| 25 | log.Printf("%s : %s", id, update.Status.String()) |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | func (s *MemoryStorage) Get(id string) (*StatusMsg, error) { |
| 30 | m, ok := s.Data[id] |
| 31 | if !ok { |
| 32 | return nil, nil |
| 33 | } |
| 34 | return &m, nil |
| 35 | } |
| 36 | |
| 37 | func (s *MemoryStorage) List() ([]StatusMsg, error) { |
| 38 | r := make([]StatusMsg, len(s.Data)) |
| 39 | i := 0 |
| 40 | for _, v := range s.Data { |
| 41 | r[i] = v |
| 42 | } |
| 43 | return r, nil |
| 44 | } |