ducky/devices
12:03c49b4d3d9f Browse Files
Add doc comments to all our exported types. It makes golint happy, and it's a good thing to do. It's kind of shameful that we went so long without them. Oops.
1.1 --- a/devices.go Fri Nov 27 11:32:15 2015 -0800 1.2 +++ b/devices.go Sat Nov 28 18:26:03 2015 -0800 1.3 @@ -78,6 +78,9 @@ 1.4 ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) 1.5 } 1.6 1.7 +// ErrDeviceAlreadyExists is returned when a Device is added to a Storer, but a 1.8 +// Device with the same ID already exists in the Storer. The ID underlying 1.9 +// ErrDeviceAlreadyExists will hold the ID that already exists. 1.10 type ErrDeviceAlreadyExists uuid.ID 1.11 1.12 func (e ErrDeviceAlreadyExists) Error() string { 1.13 @@ -207,6 +210,8 @@ 1.14 return devices, err 1.15 } 1.16 1.17 +// ToMap transforms the passed slice into a map by using the String method of 1.18 +// each Device's ID as the key, and the Device as the value. 1.19 func ToMap(devices []Device) map[string]Device { 1.20 results := make(map[string]Device, len(devices)) 1.21 for _, device := range devices { 1.22 @@ -215,6 +220,7 @@ 1.23 return results 1.24 } 1.25 1.26 +// ToSlice transforms the passed map of Devices into a slice of Devices. 1.27 func ToSlice(devices map[string]Device) []Device { 1.28 results := make([]Device, 0, len(devices)) 1.29 for _, device := range devices {
2.1 --- a/memstore.go Fri Nov 27 11:32:15 2015 -0800 2.2 +++ b/memstore.go Sat Nov 28 18:26:03 2015 -0800 2.3 @@ -7,17 +7,29 @@ 2.4 "golang.org/x/net/context" 2.5 ) 2.6 2.7 +// Memstore is an in-memory implementation of Storer, and should 2.8 +// only be used for testing or for temporary local servers. 2.9 type Memstore struct { 2.10 devices map[string]Device 2.11 lock sync.RWMutex 2.12 } 2.13 2.14 +// NewMemstore returns a Memstore that is ready to be used as a 2.15 +// Storer implementation. 2.16 func NewMemstore() *Memstore { 2.17 return &Memstore{ 2.18 devices: map[string]Device{}, 2.19 } 2.20 } 2.21 2.22 +// GetDevices returns any Devices in the Memstore that match the 2.23 +// passed IDs. If an ID cannot be matched to a Device in the 2.24 +// Memstore, it is ignored. The result is a map, with the values 2.25 +// being the Devices that could be found, and the keys being the 2.26 +// result of the String() method for each Device's ID. 2.27 +// 2.28 +// An empty map is a possible response, if none of the IDs could 2.29 +// be found. 2.30 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) { 2.31 m.lock.RLock() 2.32 defer m.lock.RUnlock() 2.33 @@ -34,6 +46,11 @@ 2.34 return results, nil 2.35 } 2.36 2.37 +// UpdateDevice applies the passed DeviceChange to the Device 2.38 +// in the Memstore specified by the DeviceChange's DeviceID 2.39 +// property. If no Device in the Memstore matches the DeviceChange's 2.40 +// DeviceID property, then an ErrDeviceNotFound error will be 2.41 +// returned. 2.42 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error { 2.43 m.lock.Lock() 2.44 defer m.lock.Unlock() 2.45 @@ -49,10 +66,17 @@ 2.46 return nil 2.47 } 2.48 2.49 +// DeleteDevices will remove any Devices from the Memstore that match 2.50 +// the IDs passed in. If an ID can't be matched to a Device in the 2.51 +// Memstore, then it is ignored. 2.52 func (m *Memstore) DeleteDevices(id []uuid.ID, c context.Context) error { 2.53 return nil 2.54 } 2.55 2.56 +// CreateDevices stores the passed devices in the Memstore as a single 2.57 +// transaction. If a Device's ID already exists in the Memstore, an 2.58 +// ErrDeviceAlreadyExists error with that Device's ID is returned, and 2.59 +// none of the passed Devices are stored. 2.60 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error { 2.61 m.lock.Lock() 2.62 defer m.lock.Unlock() 2.63 @@ -69,6 +93,11 @@ 2.64 return nil 2.65 } 2.66 2.67 +// ListDevicesByOwner returns all the Devices in the Memstore that have an 2.68 +// Owner property that matches the passed ID. If no Devices have an Owner 2.69 +// property matching the passed ID, an empty slice is returned. 2.70 +// 2.71 +// ListDevicesByOwner does not guarantee any sort order for the Devices. 2.72 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) { 2.73 var devices []Device 2.74 for _, device := range m.devices {