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