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.
| paddy@0 | 1 package uuid |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "database/sql/driver" |
| paddy@0 | 5 "errors" |
| paddy@0 | 6 |
| paddy@16 | 7 "github.com/pborman/uuid" |
| paddy@0 | 8 ) |
| paddy@0 | 9 |
| paddy@0 | 10 var InvalidIDError = errors.New("Invalid ID format.") |
| paddy@0 | 11 |
| paddy@0 | 12 type ID uuid.UUID |
| paddy@0 | 13 |
| paddy@0 | 14 func NewID() ID { |
| paddy@0 | 15 return ID(uuid.NewRandom()) |
| paddy@0 | 16 } |
| paddy@0 | 17 |
| paddy@0 | 18 func (id ID) String() string { |
| paddy@0 | 19 return uuid.UUID(id).String() |
| paddy@0 | 20 } |
| paddy@0 | 21 |
| paddy@0 | 22 func (id ID) IsZero() bool { |
| paddy@0 | 23 if id == nil { |
| paddy@0 | 24 return true |
| paddy@0 | 25 } |
| paddy@0 | 26 if len(id) == 0 { |
| paddy@0 | 27 return true |
| paddy@0 | 28 } |
| paddy@0 | 29 return false |
| paddy@0 | 30 } |
| paddy@0 | 31 |
| paddy@0 | 32 func (id ID) Copy() ID { |
| paddy@0 | 33 resp, _ := Parse(id.String()) |
| paddy@0 | 34 // ignore the error because they asked for a copy of the ID, they |
| paddy@0 | 35 // never asked if it was valid or not. |
| paddy@0 | 36 // This is, overall, not the most efficient way to do this (we're |
| paddy@0 | 37 // essentially converting to a string and then back again) but the |
| paddy@0 | 38 // computational complexity involved is pretty minor, and it allows |
| paddy@0 | 39 // us to respect the boundaries between the packages, using only the |
| paddy@0 | 40 // exported interfaces to perform a copy. And that seems pretty |
| paddy@0 | 41 // valuable. |
| paddy@0 | 42 return resp |
| paddy@0 | 43 } |
| paddy@0 | 44 |
| paddy@0 | 45 func (id ID) MarshalJSON() ([]byte, error) { |
| paddy@16 | 46 return uuid.UUID(id).MarshalJSON() |
| paddy@0 | 47 } |
| paddy@0 | 48 |
| paddy@0 | 49 func (id ID) Value() (driver.Value, error) { |
| paddy@0 | 50 return id.String(), nil |
| paddy@0 | 51 } |
| paddy@0 | 52 |
| paddy@0 | 53 func (id *ID) Scan(src interface{}) error { |
| paddy@16 | 54 return (*uuid.UUID)(id).Scan(src) |
| paddy@0 | 55 } |
| paddy@0 | 56 |
| paddy@0 | 57 func (id *ID) UnmarshalJSON(in []byte) error { |
| paddy@16 | 58 return (*uuid.UUID)(id).UnmarshalJSON(in) |
| paddy@0 | 59 } |
| paddy@0 | 60 |
| paddy@0 | 61 func Parse(in string) (ID, error) { |
| paddy@0 | 62 id := ID(uuid.Parse(in)) |
| paddy@0 | 63 if id == nil { |
| paddy@0 | 64 return id, InvalidIDError |
| paddy@0 | 65 } |
| paddy@0 | 66 return id, nil |
| paddy@0 | 67 } |
| paddy@0 | 68 |
| paddy@0 | 69 func (id ID) Equal(other ID) bool { |
| paddy@0 | 70 return uuid.Equal(uuid.UUID(id), uuid.UUID(other)) |
| paddy@0 | 71 } |