ducky/devices
ducky/devices/vendor/code.google.com/p/go-uuid/uuid/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 // Copyright 2011 Google Inc. All rights reserved. |
| paddy@0 | 2 // Use of this source code is governed by a BSD-style |
| paddy@0 | 3 // license that can be found in the LICENSE file. |
| paddy@0 | 4 |
| paddy@0 | 5 package uuid |
| paddy@0 | 6 |
| paddy@0 | 7 import ( |
| paddy@0 | 8 "bytes" |
| paddy@0 | 9 "crypto/rand" |
| paddy@0 | 10 "fmt" |
| paddy@0 | 11 "io" |
| paddy@0 | 12 "strings" |
| paddy@0 | 13 ) |
| paddy@0 | 14 |
| paddy@0 | 15 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC |
| paddy@0 | 16 // 4122. |
| paddy@0 | 17 type UUID []byte |
| paddy@0 | 18 |
| paddy@0 | 19 // A Version represents a UUIDs version. |
| paddy@0 | 20 type Version byte |
| paddy@0 | 21 |
| paddy@0 | 22 // A Variant represents a UUIDs variant. |
| paddy@0 | 23 type Variant byte |
| paddy@0 | 24 |
| paddy@0 | 25 // Constants returned by Variant. |
| paddy@0 | 26 const ( |
| paddy@0 | 27 Invalid = Variant(iota) // Invalid UUID |
| paddy@0 | 28 RFC4122 // The variant specified in RFC4122 |
| paddy@0 | 29 Reserved // Reserved, NCS backward compatibility. |
| paddy@0 | 30 Microsoft // Reserved, Microsoft Corporation backward compatibility. |
| paddy@0 | 31 Future // Reserved for future definition. |
| paddy@0 | 32 ) |
| paddy@0 | 33 |
| paddy@0 | 34 var rander = rand.Reader // random function |
| paddy@0 | 35 |
| paddy@0 | 36 // New returns a new random (version 4) UUID as a string. It is a convenience |
| paddy@0 | 37 // function for NewRandom().String(). |
| paddy@0 | 38 func New() string { |
| paddy@0 | 39 return NewRandom().String() |
| paddy@0 | 40 } |
| paddy@0 | 41 |
| paddy@0 | 42 // Parse decodes s into a UUID or returns nil. Both the UUID form of |
| paddy@0 | 43 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and |
| paddy@0 | 44 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. |
| paddy@0 | 45 func Parse(s string) UUID { |
| paddy@0 | 46 if len(s) == 36+9 { |
| paddy@0 | 47 if strings.ToLower(s[:9]) != "urn:uuid:" { |
| paddy@0 | 48 return nil |
| paddy@0 | 49 } |
| paddy@0 | 50 s = s[9:] |
| paddy@0 | 51 } else if len(s) != 36 { |
| paddy@0 | 52 return nil |
| paddy@0 | 53 } |
| paddy@0 | 54 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { |
| paddy@0 | 55 return nil |
| paddy@0 | 56 } |
| paddy@0 | 57 uuid := make([]byte, 16) |
| paddy@0 | 58 for i, x := range []int{ |
| paddy@0 | 59 0, 2, 4, 6, |
| paddy@0 | 60 9, 11, |
| paddy@0 | 61 14, 16, |
| paddy@0 | 62 19, 21, |
| paddy@0 | 63 24, 26, 28, 30, 32, 34} { |
| paddy@0 | 64 if v, ok := xtob(s[x:]); !ok { |
| paddy@0 | 65 return nil |
| paddy@0 | 66 } else { |
| paddy@0 | 67 uuid[i] = v |
| paddy@0 | 68 } |
| paddy@0 | 69 } |
| paddy@0 | 70 return uuid |
| paddy@0 | 71 } |
| paddy@0 | 72 |
| paddy@0 | 73 // Equal returns true if uuid1 and uuid2 are equal. |
| paddy@0 | 74 func Equal(uuid1, uuid2 UUID) bool { |
| paddy@0 | 75 return bytes.Equal(uuid1, uuid2) |
| paddy@0 | 76 } |
| paddy@0 | 77 |
| paddy@0 | 78 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| paddy@0 | 79 // , or "" if uuid is invalid. |
| paddy@0 | 80 func (uuid UUID) String() string { |
| paddy@0 | 81 if uuid == nil || len(uuid) != 16 { |
| paddy@0 | 82 return "" |
| paddy@0 | 83 } |
| paddy@0 | 84 b := []byte(uuid) |
| paddy@0 | 85 return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", |
| paddy@0 | 86 b[:4], b[4:6], b[6:8], b[8:10], b[10:]) |
| paddy@0 | 87 } |
| paddy@0 | 88 |
| paddy@0 | 89 // URN returns the RFC 2141 URN form of uuid, |
| paddy@0 | 90 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. |
| paddy@0 | 91 func (uuid UUID) URN() string { |
| paddy@0 | 92 if uuid == nil || len(uuid) != 16 { |
| paddy@0 | 93 return "" |
| paddy@0 | 94 } |
| paddy@0 | 95 b := []byte(uuid) |
| paddy@0 | 96 return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x", |
| paddy@0 | 97 b[:4], b[4:6], b[6:8], b[8:10], b[10:]) |
| paddy@0 | 98 } |
| paddy@0 | 99 |
| paddy@0 | 100 // Variant returns the variant encoded in uuid. It returns Invalid if |
| paddy@0 | 101 // uuid is invalid. |
| paddy@0 | 102 func (uuid UUID) Variant() Variant { |
| paddy@0 | 103 if len(uuid) != 16 { |
| paddy@0 | 104 return Invalid |
| paddy@0 | 105 } |
| paddy@0 | 106 switch { |
| paddy@0 | 107 case (uuid[8] & 0xc0) == 0x80: |
| paddy@0 | 108 return RFC4122 |
| paddy@0 | 109 case (uuid[8] & 0xe0) == 0xc0: |
| paddy@0 | 110 return Microsoft |
| paddy@0 | 111 case (uuid[8] & 0xe0) == 0xe0: |
| paddy@0 | 112 return Future |
| paddy@0 | 113 default: |
| paddy@0 | 114 return Reserved |
| paddy@0 | 115 } |
| paddy@0 | 116 panic("unreachable") |
| paddy@0 | 117 } |
| paddy@0 | 118 |
| paddy@0 | 119 // Version returns the verison of uuid. It returns false if uuid is not |
| paddy@0 | 120 // valid. |
| paddy@0 | 121 func (uuid UUID) Version() (Version, bool) { |
| paddy@0 | 122 if len(uuid) != 16 { |
| paddy@0 | 123 return 0, false |
| paddy@0 | 124 } |
| paddy@0 | 125 return Version(uuid[6] >> 4), true |
| paddy@0 | 126 } |
| paddy@0 | 127 |
| paddy@0 | 128 func (v Version) String() string { |
| paddy@0 | 129 if v > 15 { |
| paddy@0 | 130 return fmt.Sprintf("BAD_VERSION_%d", v) |
| paddy@0 | 131 } |
| paddy@0 | 132 return fmt.Sprintf("VERSION_%d", v) |
| paddy@0 | 133 } |
| paddy@0 | 134 |
| paddy@0 | 135 func (v Variant) String() string { |
| paddy@0 | 136 switch v { |
| paddy@0 | 137 case RFC4122: |
| paddy@0 | 138 return "RFC4122" |
| paddy@0 | 139 case Reserved: |
| paddy@0 | 140 return "Reserved" |
| paddy@0 | 141 case Microsoft: |
| paddy@0 | 142 return "Microsoft" |
| paddy@0 | 143 case Future: |
| paddy@0 | 144 return "Future" |
| paddy@0 | 145 case Invalid: |
| paddy@0 | 146 return "Invalid" |
| paddy@0 | 147 } |
| paddy@0 | 148 return fmt.Sprintf("BadVariant%d", int(v)) |
| paddy@0 | 149 } |
| paddy@0 | 150 |
| paddy@0 | 151 // SetRand sets the random number generator to r, which implents io.Reader. |
| paddy@0 | 152 // If r.Read returns an error when the package requests random data then |
| paddy@0 | 153 // a panic will be issued. |
| paddy@0 | 154 // |
| paddy@0 | 155 // Calling SetRand with nil sets the random number generator to the default |
| paddy@0 | 156 // generator. |
| paddy@0 | 157 func SetRand(r io.Reader) { |
| paddy@0 | 158 if r == nil { |
| paddy@0 | 159 rander = rand.Reader |
| paddy@0 | 160 return |
| paddy@0 | 161 } |
| paddy@0 | 162 rander = r |
| paddy@0 | 163 } |