ducky/devices
2015-12-19
Parent:a700ede02f91
ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go
Fix JSON pointers in errors when creating devices. Our JSON pointers used to point at the root of the document, but the properties were actually in objects in an array keyed off of "devices", so we had to update the field property of our errors to match. While we were in there, we fixed the "insufficient" error for a Device's name to be "missing" when the name is an empty string. So far, the only way for a Device's name for it to be insufficient is _for it to be the empty string_, but if in the future we raise the minimum length of the Device name, there will be a distinction and I'd like the code to recognise it.
1 package uuid
3 import (
4 "database/sql/driver"
5 "errors"
7 "github.com/pborman/uuid"
8 )
10 var InvalidIDError = errors.New("Invalid ID format.")
12 type ID uuid.UUID
14 func NewID() ID {
15 return ID(uuid.NewRandom())
16 }
18 func (id ID) String() string {
19 return uuid.UUID(id).String()
20 }
22 func (id ID) IsZero() bool {
23 if id == nil {
24 return true
25 }
26 if len(id) == 0 {
27 return true
28 }
29 return false
30 }
32 func (id ID) Copy() ID {
33 resp, _ := Parse(id.String())
34 // ignore the error because they asked for a copy of the ID, they
35 // never asked if it was valid or not.
36 // This is, overall, not the most efficient way to do this (we're
37 // essentially converting to a string and then back again) but the
38 // computational complexity involved is pretty minor, and it allows
39 // us to respect the boundaries between the packages, using only the
40 // exported interfaces to perform a copy. And that seems pretty
41 // valuable.
42 return resp
43 }
45 func (id ID) MarshalJSON() ([]byte, error) {
46 return uuid.UUID(id).MarshalJSON()
47 }
49 func (id ID) Value() (driver.Value, error) {
50 return id.String(), nil
51 }
53 func (id *ID) Scan(src interface{}) error {
54 return (*uuid.UUID)(id).Scan(src)
55 }
57 func (id *ID) UnmarshalJSON(in []byte) error {
58 return (*uuid.UUID)(id).UnmarshalJSON(in)
59 }
61 func Parse(in string) (ID, error) {
62 id := ID(uuid.Parse(in))
63 if id == nil {
64 return id, InvalidIDError
65 }
66 return id, nil
67 }
69 func (id ID) Equal(other ID) bool {
70 return uuid.Equal(uuid.UUID(id), uuid.UUID(other))
71 }