ducky/devices

Paddy 2015-11-29 Parent:b6494e1a499e Child:a700ede02f91

14:1ae5bae472c1 Go to Latest

ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go

Set up DeleteDevices in Memstore. Implement the DeleteDevices method for our Memstore, removing the Devices specified by the passed IDs. Also, create a simple test that verifies that when Devices are deleted, only the Devices you intend to delete are actually deleted. Further tests should be written to verify that this extends to ListDevicesByOwner (e.g., deleted devices will not be returned when listing them), CreateDevices (e.g., will not result in an ErrDeviceAlreadyExists error), and UpdateDevice (e.g., will return an ErrDeviceNotFound error after the Device is deleted). But for now, we're confident that it works in the simplest possible case.

History
1 package uuid
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "encoding/xml"
7 "errors"
9 "code.google.com/p/go-uuid/uuid"
10 )
12 var InvalidIDError = errors.New("Invalid ID format.")
14 type ID uuid.UUID
16 func NewID() ID {
17 return ID(uuid.NewRandom())
18 }
20 func (id ID) String() string {
21 return uuid.UUID(id).String()
22 }
24 func (id ID) IsZero() bool {
25 if id == nil {
26 return true
27 }
28 if len(id) == 0 {
29 return true
30 }
31 return false
32 }
34 func (id ID) Copy() ID {
35 resp, _ := Parse(id.String())
36 // ignore the error because they asked for a copy of the ID, they
37 // never asked if it was valid or not.
38 // This is, overall, not the most efficient way to do this (we're
39 // essentially converting to a string and then back again) but the
40 // computational complexity involved is pretty minor, and it allows
41 // us to respect the boundaries between the packages, using only the
42 // exported interfaces to perform a copy. And that seems pretty
43 // valuable.
44 return resp
45 }
47 func (id ID) MarshalJSON() ([]byte, error) {
48 return json.Marshal(id.String())
49 }
51 func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
52 return e.EncodeElement(id.String(), start)
53 }
55 func (id ID) Value() (driver.Value, error) {
56 return id.String(), nil
57 }
59 func (id *ID) Scan(src interface{}) error {
60 if src == nil {
61 id = nil
62 return nil
63 }
64 switch src.(type) {
65 case []byte:
66 if len(src.([]byte)) == 0 {
67 *id = (*id)[:0]
68 return nil
69 }
70 newID, err := Parse(string(src.([]byte)))
71 if err != nil {
72 return err
73 }
74 *id = append((*id)[:0], newID...)
75 return nil
76 case string:
77 if len(src.(string)) == 0 {
78 *id = (*id)[:0]
79 return nil
80 }
81 newID, err := Parse(src.(string))
82 if err != nil {
83 return err
84 }
85 *id = append((*id)[:0], newID...)
86 return nil
87 default:
88 return InvalidIDError
89 }
90 return nil
91 }
93 func (id *ID) UnmarshalJSON(in []byte) error {
94 var tmp string
95 err := json.Unmarshal(in, &tmp)
96 if err != nil {
97 return err
98 }
99 newID, err := Parse(tmp)
100 if err != nil {
101 return err
102 }
103 *id = append((*id)[:0], newID...)
104 return nil
105 }
107 func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
108 var tmp string
109 err := d.DecodeElement(&tmp, &start)
110 if err != nil {
111 return err
112 }
113 newID, err := Parse(tmp)
114 if err != nil {
115 return err
116 }
117 *id = append((*id)[:0], newID...)
118 return nil
119 }
121 func Parse(in string) (ID, error) {
122 id := ID(uuid.Parse(in))
123 if id == nil {
124 return id, InvalidIDError
125 }
126 return id, nil
127 }
129 func (id ID) Equal(other ID) bool {
130 return uuid.Equal(uuid.UUID(id), uuid.UUID(other))
131 }