ducky/devices

Paddy 2015-12-14 Parent:b6494e1a499e Child:a700ede02f91

15:c24a6c5fcd8c Go to Latest

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.

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 }