ducky/devices

Paddy 2015-11-15 Parent:408abf6e48d3 Child:f5a9d5f8f28d

7:1e2743834209 Go to Latest

ducky/devices/devices.go

Add ToMap and ToSlice helpers. Add helper functions that convert a map of ID-keyed Devices into a slice, and back. We'll use these a lot (especially with GetMany) as shorthand helper functions.

History
     1.1 --- a/devices.go	Sun Nov 15 03:02:34 2015 -0800
     1.2 +++ b/devices.go	Sun Nov 15 03:05:41 2015 -0800
     1.3 @@ -210,3 +210,19 @@
     1.4  	devices, err := storer.ListDevicesByOwner(user, c)
     1.5  	return devices, err
     1.6  }
     1.7 +
     1.8 +func ToMap(devices []Device) map[string]Device {
     1.9 +	results := make(map[string]Device, len(devices))
    1.10 +	for _, device := range devices {
    1.11 +		results[device.ID.String()] = device
    1.12 +	}
    1.13 +	return results
    1.14 +}
    1.15 +
    1.16 +func ToSlice(devices map[string]Device) []Device {
    1.17 +	results := make([]Device, 0, len(devices))
    1.18 +	for _, device := range devices {
    1.19 +		results = append(results, device)
    1.20 +	}
    1.21 +	return results
    1.22 +}