ducky/devices
ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go
Begin implementation on apiv1. Begin implementing the apiv1 package, which will define the first iteration of our API endpoints and logic. Each API package should be self-contained and able to run without depending on each other. Think of them as interfaces into manipulating the business logic defined in the devices package. The point is to have total control over backwards compatibility, as long as our business logic doesn't change. If that happens, we're in a bad place, but not as bad as it could be. This required us to pull in all our API tools; the api package, its dependencies, the scopeTypes package (so we can define scopes for our API), the trout router, etc. We also updated uuid to the latest, which now includes a license. Hooray? The new apiv1 package consists of a few things: * The devices.go file defines the types the API will use to communicate, along with some helpers to convert from API types to devices types. There's also a stub for validating the device creation requests, which I haven't implemented yet because I'm a pretty bad person. * endpoints.go just contains a helper function that builds our routes and assigns handlers to them, giving us an http.Handler in the returns that we can listen with. * handlers.go defines our HTTP handlers, which will read requests and write responses, after doing the appropriate validation and executing the appropriating business logic. Right now, we only have a handler for creating devices, and it doesn't actually do any validation. Also, we have some user-correctable errors being returned as 500s right now, which is Bad. Fortunately, they're all marked with BUG, so I can at least come back to them. * response.go defines the Response type that will be used for returning information after a request is executed. It may eventually get some helpers, but for now it's pretty basic. * scopes.go defines the Scopes that we're going to be using in the package to control access. It should probably (eventually) include a helper to register the Scopes, or we should have a collector service that pulls in all the packages, finds all their Scopes, and registers them. I haven't decided how I want to manage Scope registration just yet. We exported the getStorer function (now GetStorer) so other packages can use it. I'm not sure how I feel about this just yet. We also had to create a WithStorer helper method that embeds the Storer into a context.Context, so we can bootstrap in devicesd. We erroneously had Created in the DeviceChange struct, but there's no reason the Created property of a Device should ever change, so it was removed from the logic, from the struct, and from the tests. Our CreateMany helper was erroneously creating the un-modified Devices that were passed in, instead of the Devices that had sensible defaults filled. We created a _very minimal_ (e.g., needs some work before it's ready for production) devicesd package that will spin up a simple server, just so we could take a peek at our apiv1 endpoints as they'd actually be used. (It worked. Yay?) We should continue to expand on this with configuration, more information being logged, etc.
| paddy@0 | 1 package uuid |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "database/sql/driver" |
| paddy@0 | 5 "encoding/json" |
| paddy@0 | 6 "encoding/xml" |
| paddy@0 | 7 "errors" |
| paddy@0 | 8 |
| paddy@0 | 9 "code.google.com/p/go-uuid/uuid" |
| paddy@0 | 10 ) |
| paddy@0 | 11 |
| paddy@0 | 12 var InvalidIDError = errors.New("Invalid ID format.") |
| paddy@0 | 13 |
| paddy@0 | 14 type ID uuid.UUID |
| paddy@0 | 15 |
| paddy@0 | 16 func NewID() ID { |
| paddy@0 | 17 return ID(uuid.NewRandom()) |
| paddy@0 | 18 } |
| paddy@0 | 19 |
| paddy@0 | 20 func (id ID) String() string { |
| paddy@0 | 21 return uuid.UUID(id).String() |
| paddy@0 | 22 } |
| paddy@0 | 23 |
| paddy@0 | 24 func (id ID) IsZero() bool { |
| paddy@0 | 25 if id == nil { |
| paddy@0 | 26 return true |
| paddy@0 | 27 } |
| paddy@0 | 28 if len(id) == 0 { |
| paddy@0 | 29 return true |
| paddy@0 | 30 } |
| paddy@0 | 31 return false |
| paddy@0 | 32 } |
| paddy@0 | 33 |
| paddy@0 | 34 func (id ID) Copy() ID { |
| paddy@0 | 35 resp, _ := Parse(id.String()) |
| paddy@0 | 36 // ignore the error because they asked for a copy of the ID, they |
| paddy@0 | 37 // never asked if it was valid or not. |
| paddy@0 | 38 // This is, overall, not the most efficient way to do this (we're |
| paddy@0 | 39 // essentially converting to a string and then back again) but the |
| paddy@0 | 40 // computational complexity involved is pretty minor, and it allows |
| paddy@0 | 41 // us to respect the boundaries between the packages, using only the |
| paddy@0 | 42 // exported interfaces to perform a copy. And that seems pretty |
| paddy@0 | 43 // valuable. |
| paddy@0 | 44 return resp |
| paddy@0 | 45 } |
| paddy@0 | 46 |
| paddy@0 | 47 func (id ID) MarshalJSON() ([]byte, error) { |
| paddy@0 | 48 return json.Marshal(id.String()) |
| paddy@0 | 49 } |
| paddy@0 | 50 |
| paddy@0 | 51 func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error { |
| paddy@0 | 52 return e.EncodeElement(id.String(), start) |
| paddy@0 | 53 } |
| paddy@0 | 54 |
| paddy@0 | 55 func (id ID) Value() (driver.Value, error) { |
| paddy@0 | 56 return id.String(), nil |
| paddy@0 | 57 } |
| paddy@0 | 58 |
| paddy@0 | 59 func (id *ID) Scan(src interface{}) error { |
| paddy@0 | 60 if src == nil { |
| paddy@0 | 61 id = nil |
| paddy@0 | 62 return nil |
| paddy@0 | 63 } |
| paddy@0 | 64 switch src.(type) { |
| paddy@0 | 65 case []byte: |
| paddy@0 | 66 if len(src.([]byte)) == 0 { |
| paddy@0 | 67 *id = (*id)[:0] |
| paddy@0 | 68 return nil |
| paddy@0 | 69 } |
| paddy@0 | 70 newID, err := Parse(string(src.([]byte))) |
| paddy@0 | 71 if err != nil { |
| paddy@0 | 72 return err |
| paddy@0 | 73 } |
| paddy@0 | 74 *id = append((*id)[:0], newID...) |
| paddy@0 | 75 return nil |
| paddy@0 | 76 case string: |
| paddy@0 | 77 if len(src.(string)) == 0 { |
| paddy@0 | 78 *id = (*id)[:0] |
| paddy@0 | 79 return nil |
| paddy@0 | 80 } |
| paddy@0 | 81 newID, err := Parse(src.(string)) |
| paddy@0 | 82 if err != nil { |
| paddy@0 | 83 return err |
| paddy@0 | 84 } |
| paddy@0 | 85 *id = append((*id)[:0], newID...) |
| paddy@0 | 86 return nil |
| paddy@0 | 87 default: |
| paddy@0 | 88 return InvalidIDError |
| paddy@0 | 89 } |
| paddy@0 | 90 return nil |
| paddy@0 | 91 } |
| paddy@0 | 92 |
| paddy@0 | 93 func (id *ID) UnmarshalJSON(in []byte) error { |
| paddy@0 | 94 var tmp string |
| paddy@0 | 95 err := json.Unmarshal(in, &tmp) |
| paddy@0 | 96 if err != nil { |
| paddy@0 | 97 return err |
| paddy@0 | 98 } |
| paddy@0 | 99 newID, err := Parse(tmp) |
| paddy@0 | 100 if err != nil { |
| paddy@0 | 101 return err |
| paddy@0 | 102 } |
| paddy@0 | 103 *id = append((*id)[:0], newID...) |
| paddy@0 | 104 return nil |
| paddy@0 | 105 } |
| paddy@0 | 106 |
| paddy@0 | 107 func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
| paddy@0 | 108 var tmp string |
| paddy@0 | 109 err := d.DecodeElement(&tmp, &start) |
| paddy@0 | 110 if err != nil { |
| paddy@0 | 111 return err |
| paddy@0 | 112 } |
| paddy@0 | 113 newID, err := Parse(tmp) |
| paddy@0 | 114 if err != nil { |
| paddy@0 | 115 return err |
| paddy@0 | 116 } |
| paddy@0 | 117 *id = append((*id)[:0], newID...) |
| paddy@0 | 118 return nil |
| paddy@0 | 119 } |
| paddy@0 | 120 |
| paddy@0 | 121 func Parse(in string) (ID, error) { |
| paddy@0 | 122 id := ID(uuid.Parse(in)) |
| paddy@0 | 123 if id == nil { |
| paddy@0 | 124 return id, InvalidIDError |
| paddy@0 | 125 } |
| paddy@0 | 126 return id, nil |
| paddy@0 | 127 } |
| paddy@0 | 128 |
| paddy@0 | 129 func (id ID) Equal(other ID) bool { |
| paddy@0 | 130 return uuid.Equal(uuid.UUID(id), uuid.UUID(other)) |
| paddy@0 | 131 } |