ducky/devices
ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go
Start testing our Storers. Set up a file to start testing our Storer implementations, and write a sample test for the happy path of GetDevices--e.g., we expect no errors. This also required us to setup a helper to compare two Device instances and see if they're equal or not, and if not, how they differ. In the future, we'll keep adding test methods to test more logical paths for the interface contract, and to test more of the methods for the interface.
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 }