blob: db64df250224a258572581a3ada52afe7c97d92c [file] [log] [blame]
David K. Bainbridgef0da8732016-06-01 16:15:37 -07001package main
2
3import (
4 "log"
5)
6
7type Storage interface {
8 Put(id string, update StatusMsg) error
9 Get(id string) (*StatusMsg, error)
10 List() ([]StatusMsg, error)
11}
12
13type MemoryStorage struct {
14 Data map[string]StatusMsg
15}
16
17func NewMemoryStorage() *MemoryStorage {
18 return &MemoryStorage{
19 Data: make(map[string]StatusMsg),
20 }
21}
22
23func (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
29func (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
37func (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}