ducky/devices
2015-11-15
Parent:b6494e1a499e
ducky/devices/vendor/code.google.com/p/go-uuid/uuid/json.go
Implement ListDevicesByOwner. Write a unit test for listing Devices by their Owner property, as per our Storer interface. Basically, if we insert Devices into the datastore, we need to be able to retrieve the Devices that someone owns. Simple enough. This meant that I needed to implement the Memstore version, past the barebones stub, so the tests would continue to pass.
1 // Copyright 2014 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package uuid
7 import "errors"
9 func (u UUID) MarshalJSON() ([]byte, error) {
10 if len(u) == 0 {
11 return []byte(`""`), nil
12 }
13 return []byte(`"` + u.String() + `"`), nil
14 }
16 func (u *UUID) UnmarshalJSON(data []byte) error {
17 if len(data) == 0 || string(data) == `""` {
18 return nil
19 }
20 if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
21 return errors.New("invalid UUID format")
22 }
23 data = data[1 : len(data)-1]
24 uu := Parse(string(data))
25 if uu == nil {
26 return errors.New("invalid UUID format")
27 }
28 *u = uu
29 return nil
30 }