ducky/devices
0:b6494e1a499e Browse Files
Initial attempt. Create the basic types (Device, DeviceType, DeviceChange) that we'll be using in the service. Also, create our Storer interface, and the helper methods to store, retrieve, and update information in the datastore. Also, we're using Godep, so check in our Godeps folder and the vendor folder. Note that this only works on Go 1.5 and later, with the GO15VENDOREXPERIMENT environment variable set to 1.
Godeps/Godeps.json Godeps/Readme context.go devices.go vendor/code.google.com/p/go-uuid/uuid/LICENSE vendor/code.google.com/p/go-uuid/uuid/dce.go vendor/code.google.com/p/go-uuid/uuid/doc.go vendor/code.google.com/p/go-uuid/uuid/hash.go vendor/code.google.com/p/go-uuid/uuid/json.go vendor/code.google.com/p/go-uuid/uuid/node.go vendor/code.google.com/p/go-uuid/uuid/time.go vendor/code.google.com/p/go-uuid/uuid/util.go vendor/code.google.com/p/go-uuid/uuid/uuid.go vendor/code.google.com/p/go-uuid/uuid/version1.go vendor/code.google.com/p/go-uuid/uuid/version4.go vendor/code.secondbit.org/uuid.hg/uuid.go vendor/golang.org/x/net/LICENSE vendor/golang.org/x/net/PATENTS vendor/golang.org/x/net/context/context.go vendor/golang.org/x/net/context/ctxhttp/cancelreq.go vendor/golang.org/x/net/context/ctxhttp/cancelreq_go14.go vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Godeps/Godeps.json Thu Nov 12 20:33:26 2015 -0800 1.3 @@ -0,0 +1,23 @@ 1.4 +{ 1.5 + "ImportPath": "code.secondbit.org/ducky/devices.hg", 1.6 + "GoVersion": "go1.5.1", 1.7 + "Packages": [ 1.8 + "." 1.9 + ], 1.10 + "Deps": [ 1.11 + { 1.12 + "ImportPath": "code.google.com/p/go-uuid/uuid", 1.13 + "Comment": "null-15", 1.14 + "Rev": "'35bc42037350f0078e3c974c6ea690f1926603ab'" 1.15 + }, 1.16 + { 1.17 + "ImportPath": "code.secondbit.org/uuid.hg", 1.18 + "Comment": "null-5", 1.19 + "Rev": "'cda03b52c8c99d986df4e7601c7b071bb7bd6333'" 1.20 + }, 1.21 + { 1.22 + "ImportPath": "golang.org/x/net/context", 1.23 + "Rev": "01256db42e5106196a3473ec1620727a383500e8" 1.24 + } 1.25 + ] 1.26 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Godeps/Readme Thu Nov 12 20:33:26 2015 -0800 2.3 @@ -0,0 +1,5 @@ 2.4 +This directory tree is generated automatically by godep. 2.5 + 2.6 +Please do not edit. 2.7 + 2.8 +See https://github.com/tools/godep for more information.
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/context.go Thu Nov 12 20:33:26 2015 -0800 3.3 @@ -0,0 +1,30 @@ 3.4 +package devices 3.5 + 3.6 +import ( 3.7 + "errors" 3.8 + 3.9 + "golang.org/x/net/context" 3.10 +) 3.11 + 3.12 +const ( 3.13 + storerKey = "code.secondbit.org/ducky/devices.hg#Storer" 3.14 +) 3.15 + 3.16 +var ( 3.17 + // ErrNoStorerSet is returned when the Context has no Storer set in it. 3.18 + ErrNoStorerSet = errors.New("storerKey not set in Context") 3.19 + // ErrStorerKeyNotStorer is returned when there's a value in the Context for storerKey, but it's not a Storer. 3.20 + ErrStorerKeyNotStorer = errors.New("the value for storerKey does not fulfill the Storer interface") 3.21 +) 3.22 + 3.23 +func getStorer(c context.Context) (Storer, error) { 3.24 + val := c.Value(storerKey) 3.25 + if val == nil { 3.26 + return nil, ErrNoStorerSet 3.27 + } 3.28 + storer, ok := val.(Storer) 3.29 + if !ok { 3.30 + return nil, ErrStorerKeyNotStorer 3.31 + } 3.32 + return storer, nil 3.33 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/devices.go Thu Nov 12 20:33:26 2015 -0800 4.3 @@ -0,0 +1,209 @@ 4.4 +package devices 4.5 + 4.6 +import ( 4.7 + "errors" 4.8 + "log" 4.9 + "time" 4.10 + 4.11 + "golang.org/x/net/context" 4.12 + 4.13 + "code.secondbit.org/uuid.hg" 4.14 +) 4.15 + 4.16 +var ( 4.17 + // ErrDeviceNotFound is returned when the specified device couldn't be found. 4.18 + ErrDeviceNotFound = errors.New("device not found") 4.19 +) 4.20 + 4.21 +// Device represents a specific device that updates can be pushed to. 4.22 +type Device struct { 4.23 + ID uuid.ID 4.24 + Name string 4.25 + Owner uuid.ID 4.26 + Type DeviceType 4.27 + Created time.Time 4.28 + LastSeen time.Time 4.29 + PushToken string 4.30 +} 4.31 + 4.32 +// ApplyChange returns a Device that is a copy of the passed Device, 4.33 +// but with the passed DeviceChange applied. 4.34 +func ApplyChange(d Device, change DeviceChange) Device { 4.35 + result := d 4.36 + if change.Name != nil { 4.37 + result.Name = *change.Name 4.38 + } 4.39 + if change.Owner != nil { 4.40 + result.Owner = *change.Owner 4.41 + } else { 4.42 + // We don't want to accidentally leave a slice that 4.43 + // is owned by both behind. 4.44 + result.Owner = d.Owner.Copy() 4.45 + } 4.46 + if change.Type != nil { 4.47 + result.Type = *change.Type 4.48 + } 4.49 + if change.Created != nil { 4.50 + result.Created = *change.Created 4.51 + } 4.52 + if change.LastSeen != nil { 4.53 + result.LastSeen = *change.LastSeen 4.54 + } 4.55 + if change.PushToken != nil { 4.56 + result.PushToken = *change.PushToken 4.57 + } 4.58 + return result 4.59 +} 4.60 + 4.61 +// DeviceType is an enum specifying which type of Device it is. Usually, 4.62 +// this is something like `android_phone` or `android_tablet`. 4.63 +type DeviceType string 4.64 + 4.65 +// DeviceChange represents a set of changes to a Device that will be used 4.66 +// to update a Device. 4.67 +type DeviceChange struct { 4.68 + DeviceID uuid.ID 4.69 + Name *string 4.70 + Owner *uuid.ID 4.71 + Type *DeviceType 4.72 + Created *time.Time 4.73 + LastSeen *time.Time 4.74 + PushToken *string 4.75 +} 4.76 + 4.77 +// Storer is an interface to control how data is stored in and retrieved from 4.78 +// the datastore. 4.79 +type Storer interface { 4.80 + GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) 4.81 + UpdateDevice(change DeviceChange, c context.Context) error 4.82 + DeleteDevices(ids []uuid.ID, c context.Context) error 4.83 + CreateDevices(devices []Device, c context.Context) error 4.84 + ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) 4.85 + 4.86 + // These are used for testing only. 4.87 + Factory(c context.Context) (Storer, error) 4.88 + Destroy(c context.Context) error 4.89 +} 4.90 + 4.91 +// GetMany returns as many of the Devices specified by the passed IDs as possible. 4.92 +// They are returned as a map, with the key being the string version of the ID. 4.93 +// No error will be returned if a Device can't be found. 4.94 +func GetMany(ids []uuid.ID, c context.Context) (map[string]Device, error) { 4.95 + results := map[string]Device{} 4.96 + storer, err := getStorer(c) 4.97 + if err != nil { 4.98 + log.Printf("Error retrieving Storer: %+v\n", err) 4.99 + return results, err 4.100 + } 4.101 + results, err = storer.GetDevices(ids, c) 4.102 + if err != nil { 4.103 + log.Printf("Error retrieving Devices from %T: %+v\n", storer, err) 4.104 + return results, err 4.105 + } 4.106 + return results, nil 4.107 +} 4.108 + 4.109 +// Get returns the Device specified by the passed ID. If the Device can't be found, 4.110 +// an ErrDeviceNotFound error is returned. 4.111 +func Get(id uuid.ID, c context.Context) (Device, error) { 4.112 + results, err := GetMany([]uuid.ID{id}, c) 4.113 + if err != nil { 4.114 + return Device{}, err 4.115 + } 4.116 + result, ok := results[id.String()] 4.117 + if !ok { 4.118 + return Device{}, ErrDeviceNotFound 4.119 + } 4.120 + return result, nil 4.121 +} 4.122 + 4.123 +// Update applies the DeviceChange to the passed Device, and returns the result. If 4.124 +// the Device can't be found, an ErrDeviceNotFound error was returned. 4.125 +func Update(device Device, change DeviceChange, c context.Context) (Device, error) { 4.126 + storer, err := getStorer(c) 4.127 + if err != nil { 4.128 + log.Printf("Error retrieving Storer: %+v\n", err) 4.129 + return Device{}, err 4.130 + } 4.131 + change.DeviceID = device.ID 4.132 + err = storer.UpdateDevice(change, c) 4.133 + if err != nil { 4.134 + return Device{}, err 4.135 + } 4.136 + return ApplyChange(device, change), nil 4.137 +} 4.138 + 4.139 +// DeleteMany removes the passed IDs from the datastore. No error is returned if the 4.140 +// ID doesn't correspond to a Device in the datastore. 4.141 +func DeleteMany(ids []uuid.ID, c context.Context) error { 4.142 + storer, err := getStorer(c) 4.143 + if err != nil { 4.144 + log.Printf("Error retrieving Storer: %+v\n", err) 4.145 + return err 4.146 + } 4.147 + return storer.DeleteDevices(ids, c) 4.148 +} 4.149 + 4.150 +// Delete removes the passed ID from the datastore. No error is returned if the ID doesn't 4.151 +// correspond to a Device in the datastore. 4.152 +func Delete(id uuid.ID, c context.Context) error { 4.153 + return DeleteMany([]uuid.ID{id}, c) 4.154 +} 4.155 + 4.156 +// CreateMany stores the passed Devices in the datastore, assigning default values if 4.157 +// necessary. The Devices that were ultimately stored (including any default values, if 4.158 +// applicable) are returned. 4.159 +func CreateMany(devices []Device, c context.Context) ([]Device, error) { 4.160 + storer, err := getStorer(c) 4.161 + if err != nil { 4.162 + log.Printf("Error retrieving Storer: %+v\n", err) 4.163 + return []Device{}, err 4.164 + } 4.165 + modified := make([]Device, 0, len(devices)) 4.166 + for _, device := range devices { 4.167 + if device.ID.IsZero() { 4.168 + device.ID = uuid.NewID() 4.169 + } 4.170 + if device.Created.IsZero() { 4.171 + device.Created = time.Now() 4.172 + } 4.173 + if device.LastSeen.IsZero() { 4.174 + device.LastSeen = time.Now() 4.175 + } 4.176 + modified = append(modified, device) 4.177 + } 4.178 + err = storer.CreateDevices(devices, c) 4.179 + if err != nil { 4.180 + return []Device{}, err 4.181 + } 4.182 + return modified, nil 4.183 +} 4.184 + 4.185 +// Create stores the passed Device in the datastore, assigning default values if 4.186 +// necessary. The Devices that were ultimately stored (including any default values, if 4.187 +// applicable) are returned. 4.188 +func Create(device Device, c context.Context) (Device, error) { 4.189 + devices, err := CreateMany([]Device{device}, c) 4.190 + if err != nil { 4.191 + return Device{}, err 4.192 + } 4.193 + // There should never be a case where we don't return a result. 4.194 + // Ideally, we'd return an error here instead of letting the panic 4.195 + // happen, but seeing as I can't come up with a reason the error would 4.196 + // occur, I'm having trouble coming up with a reasonable error to return. 4.197 + return devices[0], nil 4.198 +} 4.199 + 4.200 +// ListByOwner returns a slice of all the Devices with an Owner property that 4.201 +// matches the passed ID. There's no guarantee on the order the Devices will be 4.202 +// returned in. 4.203 +func ListByOwner(user uuid.ID, c context.Context) ([]Device, error) { 4.204 + // BUG(paddy): Eventually, we'll need to support paging for devices. But right now, I don't foresee any user creating enough of them to make pagination worthwhile. 4.205 + storer, err := getStorer(c) 4.206 + if err != nil { 4.207 + log.Printf("Error retrieving Storer: %+v\n", err) 4.208 + return []Device{}, err 4.209 + } 4.210 + devices, err := storer.ListDevicesByOwner(user, c) 4.211 + return devices, err 4.212 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/LICENSE Thu Nov 12 20:33:26 2015 -0800 5.3 @@ -0,0 +1,27 @@ 5.4 +Copyright (c) 2009,2014 Google Inc. All rights reserved. 5.5 + 5.6 +Redistribution and use in source and binary forms, with or without 5.7 +modification, are permitted provided that the following conditions are 5.8 +met: 5.9 + 5.10 + * Redistributions of source code must retain the above copyright 5.11 +notice, this list of conditions and the following disclaimer. 5.12 + * Redistributions in binary form must reproduce the above 5.13 +copyright notice, this list of conditions and the following disclaimer 5.14 +in the documentation and/or other materials provided with the 5.15 +distribution. 5.16 + * Neither the name of Google Inc. nor the names of its 5.17 +contributors may be used to endorse or promote products derived from 5.18 +this software without specific prior written permission. 5.19 + 5.20 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 5.21 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 5.22 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 5.23 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 5.24 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5.25 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 5.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5.27 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5.28 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5.29 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 5.30 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/dce.go Thu Nov 12 20:33:26 2015 -0800 6.3 @@ -0,0 +1,84 @@ 6.4 +// Copyright 2011 Google Inc. All rights reserved. 6.5 +// Use of this source code is governed by a BSD-style 6.6 +// license that can be found in the LICENSE file. 6.7 + 6.8 +package uuid 6.9 + 6.10 +import ( 6.11 + "encoding/binary" 6.12 + "fmt" 6.13 + "os" 6.14 +) 6.15 + 6.16 +// A Domain represents a Version 2 domain 6.17 +type Domain byte 6.18 + 6.19 +// Domain constants for DCE Security (Version 2) UUIDs. 6.20 +const ( 6.21 + Person = Domain(0) 6.22 + Group = Domain(1) 6.23 + Org = Domain(2) 6.24 +) 6.25 + 6.26 +// NewDCESecurity returns a DCE Security (Version 2) UUID. 6.27 +// 6.28 +// The domain should be one of Person, Group or Org. 6.29 +// On a POSIX system the id should be the users UID for the Person 6.30 +// domain and the users GID for the Group. The meaning of id for 6.31 +// the domain Org or on non-POSIX systems is site defined. 6.32 +// 6.33 +// For a given domain/id pair the same token may be returned for up to 6.34 +// 7 minutes and 10 seconds. 6.35 +func NewDCESecurity(domain Domain, id uint32) UUID { 6.36 + uuid := NewUUID() 6.37 + if uuid != nil { 6.38 + uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2 6.39 + uuid[9] = byte(domain) 6.40 + binary.BigEndian.PutUint32(uuid[0:], id) 6.41 + } 6.42 + return uuid 6.43 +} 6.44 + 6.45 +// NewDCEPerson returns a DCE Security (Version 2) UUID in the person 6.46 +// domain with the id returned by os.Getuid. 6.47 +// 6.48 +// NewDCEPerson(Person, uint32(os.Getuid())) 6.49 +func NewDCEPerson() UUID { 6.50 + return NewDCESecurity(Person, uint32(os.Getuid())) 6.51 +} 6.52 + 6.53 +// NewDCEGroup returns a DCE Security (Version 2) UUID in the group 6.54 +// domain with the id returned by os.Getgid. 6.55 +// 6.56 +// NewDCEGroup(Group, uint32(os.Getgid())) 6.57 +func NewDCEGroup() UUID { 6.58 + return NewDCESecurity(Group, uint32(os.Getgid())) 6.59 +} 6.60 + 6.61 +// Domain returns the domain for a Version 2 UUID or false. 6.62 +func (uuid UUID) Domain() (Domain, bool) { 6.63 + if v, _ := uuid.Version(); v != 2 { 6.64 + return 0, false 6.65 + } 6.66 + return Domain(uuid[9]), true 6.67 +} 6.68 + 6.69 +// Id returns the id for a Version 2 UUID or false. 6.70 +func (uuid UUID) Id() (uint32, bool) { 6.71 + if v, _ := uuid.Version(); v != 2 { 6.72 + return 0, false 6.73 + } 6.74 + return binary.BigEndian.Uint32(uuid[0:4]), true 6.75 +} 6.76 + 6.77 +func (d Domain) String() string { 6.78 + switch d { 6.79 + case Person: 6.80 + return "Person" 6.81 + case Group: 6.82 + return "Group" 6.83 + case Org: 6.84 + return "Org" 6.85 + } 6.86 + return fmt.Sprintf("Domain%d", int(d)) 6.87 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/doc.go Thu Nov 12 20:33:26 2015 -0800 7.3 @@ -0,0 +1,8 @@ 7.4 +// Copyright 2011 Google Inc. All rights reserved. 7.5 +// Use of this source code is governed by a BSD-style 7.6 +// license that can be found in the LICENSE file. 7.7 + 7.8 +// The uuid package generates and inspects UUIDs. 7.9 +// 7.10 +// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services. 7.11 +package uuid
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/hash.go Thu Nov 12 20:33:26 2015 -0800 8.3 @@ -0,0 +1,53 @@ 8.4 +// Copyright 2011 Google Inc. All rights reserved. 8.5 +// Use of this source code is governed by a BSD-style 8.6 +// license that can be found in the LICENSE file. 8.7 + 8.8 +package uuid 8.9 + 8.10 +import ( 8.11 + "crypto/md5" 8.12 + "crypto/sha1" 8.13 + "hash" 8.14 +) 8.15 + 8.16 +// Well known Name Space IDs and UUIDs 8.17 +var ( 8.18 + NameSpace_DNS = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 8.19 + NameSpace_URL = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8") 8.20 + NameSpace_OID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8") 8.21 + NameSpace_X500 = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8") 8.22 + NIL = Parse("00000000-0000-0000-0000-000000000000") 8.23 +) 8.24 + 8.25 +// NewHash returns a new UUID dervied from the hash of space concatenated with 8.26 +// data generated by h. The hash should be at least 16 byte in length. The 8.27 +// first 16 bytes of the hash are used to form the UUID. The version of the 8.28 +// UUID will be the lower 4 bits of version. NewHash is used to implement 8.29 +// NewMD5 and NewSHA1. 8.30 +func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID { 8.31 + h.Reset() 8.32 + h.Write(space) 8.33 + h.Write([]byte(data)) 8.34 + s := h.Sum(nil) 8.35 + uuid := make([]byte, 16) 8.36 + copy(uuid, s) 8.37 + uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4) 8.38 + uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant 8.39 + return uuid 8.40 +} 8.41 + 8.42 +// NewMD5 returns a new MD5 (Version 3) UUID based on the 8.43 +// supplied name space and data. 8.44 +// 8.45 +// NewHash(md5.New(), space, data, 3) 8.46 +func NewMD5(space UUID, data []byte) UUID { 8.47 + return NewHash(md5.New(), space, data, 3) 8.48 +} 8.49 + 8.50 +// NewSHA1 returns a new SHA1 (Version 5) UUID based on the 8.51 +// supplied name space and data. 8.52 +// 8.53 +// NewHash(sha1.New(), space, data, 5) 8.54 +func NewSHA1(space UUID, data []byte) UUID { 8.55 + return NewHash(sha1.New(), space, data, 5) 8.56 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/json.go Thu Nov 12 20:33:26 2015 -0800 9.3 @@ -0,0 +1,30 @@ 9.4 +// Copyright 2014 Google Inc. All rights reserved. 9.5 +// Use of this source code is governed by a BSD-style 9.6 +// license that can be found in the LICENSE file. 9.7 + 9.8 +package uuid 9.9 + 9.10 +import "errors" 9.11 + 9.12 +func (u UUID) MarshalJSON() ([]byte, error) { 9.13 + if len(u) == 0 { 9.14 + return []byte(`""`), nil 9.15 + } 9.16 + return []byte(`"` + u.String() + `"`), nil 9.17 +} 9.18 + 9.19 +func (u *UUID) UnmarshalJSON(data []byte) error { 9.20 + if len(data) == 0 || string(data) == `""` { 9.21 + return nil 9.22 + } 9.23 + if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { 9.24 + return errors.New("invalid UUID format") 9.25 + } 9.26 + data = data[1 : len(data)-1] 9.27 + uu := Parse(string(data)) 9.28 + if uu == nil { 9.29 + return errors.New("invalid UUID format") 9.30 + } 9.31 + *u = uu 9.32 + return nil 9.33 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/node.go Thu Nov 12 20:33:26 2015 -0800 10.3 @@ -0,0 +1,101 @@ 10.4 +// Copyright 2011 Google Inc. All rights reserved. 10.5 +// Use of this source code is governed by a BSD-style 10.6 +// license that can be found in the LICENSE file. 10.7 + 10.8 +package uuid 10.9 + 10.10 +import "net" 10.11 + 10.12 +var ( 10.13 + interfaces []net.Interface // cached list of interfaces 10.14 + ifname string // name of interface being used 10.15 + nodeID []byte // hardware for version 1 UUIDs 10.16 +) 10.17 + 10.18 +// NodeInterface returns the name of the interface from which the NodeID was 10.19 +// derived. The interface "user" is returned if the NodeID was set by 10.20 +// SetNodeID. 10.21 +func NodeInterface() string { 10.22 + return ifname 10.23 +} 10.24 + 10.25 +// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs. 10.26 +// If name is "" then the first usable interface found will be used or a random 10.27 +// Node ID will be generated. If a named interface cannot be found then false 10.28 +// is returned. 10.29 +// 10.30 +// SetNodeInterface never fails when name is "". 10.31 +func SetNodeInterface(name string) bool { 10.32 + if interfaces == nil { 10.33 + var err error 10.34 + interfaces, err = net.Interfaces() 10.35 + if err != nil && name != "" { 10.36 + return false 10.37 + } 10.38 + } 10.39 + 10.40 + for _, ifs := range interfaces { 10.41 + if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) { 10.42 + if setNodeID(ifs.HardwareAddr) { 10.43 + ifname = ifs.Name 10.44 + return true 10.45 + } 10.46 + } 10.47 + } 10.48 + 10.49 + // We found no interfaces with a valid hardware address. If name 10.50 + // does not specify a specific interface generate a random Node ID 10.51 + // (section 4.1.6) 10.52 + if name == "" { 10.53 + if nodeID == nil { 10.54 + nodeID = make([]byte, 6) 10.55 + } 10.56 + randomBits(nodeID) 10.57 + return true 10.58 + } 10.59 + return false 10.60 +} 10.61 + 10.62 +// NodeID returns a slice of a copy of the current Node ID, setting the Node ID 10.63 +// if not already set. 10.64 +func NodeID() []byte { 10.65 + if nodeID == nil { 10.66 + SetNodeInterface("") 10.67 + } 10.68 + nid := make([]byte, 6) 10.69 + copy(nid, nodeID) 10.70 + return nid 10.71 +} 10.72 + 10.73 +// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes 10.74 +// of id are used. If id is less than 6 bytes then false is returned and the 10.75 +// Node ID is not set. 10.76 +func SetNodeID(id []byte) bool { 10.77 + if setNodeID(id) { 10.78 + ifname = "user" 10.79 + return true 10.80 + } 10.81 + return false 10.82 +} 10.83 + 10.84 +func setNodeID(id []byte) bool { 10.85 + if len(id) < 6 { 10.86 + return false 10.87 + } 10.88 + if nodeID == nil { 10.89 + nodeID = make([]byte, 6) 10.90 + } 10.91 + copy(nodeID, id) 10.92 + return true 10.93 +} 10.94 + 10.95 +// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is 10.96 +// not valid. The NodeID is only well defined for version 1 and 2 UUIDs. 10.97 +func (uuid UUID) NodeID() []byte { 10.98 + if len(uuid) != 16 { 10.99 + return nil 10.100 + } 10.101 + node := make([]byte, 6) 10.102 + copy(node, uuid[10:]) 10.103 + return node 10.104 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/time.go Thu Nov 12 20:33:26 2015 -0800 11.3 @@ -0,0 +1,132 @@ 11.4 +// Copyright 2014 Google Inc. All rights reserved. 11.5 +// Use of this source code is governed by a BSD-style 11.6 +// license that can be found in the LICENSE file. 11.7 + 11.8 +package uuid 11.9 + 11.10 +import ( 11.11 + "encoding/binary" 11.12 + "sync" 11.13 + "time" 11.14 +) 11.15 + 11.16 +// A Time represents a time as the number of 100's of nanoseconds since 15 Oct 11.17 +// 1582. 11.18 +type Time int64 11.19 + 11.20 +const ( 11.21 + lillian = 2299160 // Julian day of 15 Oct 1582 11.22 + unix = 2440587 // Julian day of 1 Jan 1970 11.23 + epoch = unix - lillian // Days between epochs 11.24 + g1582 = epoch * 86400 // seconds between epochs 11.25 + g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs 11.26 +) 11.27 + 11.28 +var ( 11.29 + mu sync.Mutex 11.30 + lasttime uint64 // last time we returned 11.31 + clock_seq uint16 // clock sequence for this run 11.32 + 11.33 + timeNow = time.Now // for testing 11.34 +) 11.35 + 11.36 +// UnixTime converts t the number of seconds and nanoseconds using the Unix 11.37 +// epoch of 1 Jan 1970. 11.38 +func (t Time) UnixTime() (sec, nsec int64) { 11.39 + sec = int64(t - g1582ns100) 11.40 + nsec = (sec % 10000000) * 100 11.41 + sec /= 10000000 11.42 + return sec, nsec 11.43 +} 11.44 + 11.45 +// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and 11.46 +// clock sequence as well as adjusting the clock sequence as needed. An error 11.47 +// is returned if the current time cannot be determined. 11.48 +func GetTime() (Time, uint16, error) { 11.49 + defer mu.Unlock() 11.50 + mu.Lock() 11.51 + return getTime() 11.52 +} 11.53 + 11.54 +func getTime() (Time, uint16, error) { 11.55 + t := timeNow() 11.56 + 11.57 + // If we don't have a clock sequence already, set one. 11.58 + if clock_seq == 0 { 11.59 + setClockSequence(-1) 11.60 + } 11.61 + now := uint64(t.UnixNano()/100) + g1582ns100 11.62 + 11.63 + // If time has gone backwards with this clock sequence then we 11.64 + // increment the clock sequence 11.65 + if now <= lasttime { 11.66 + clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000 11.67 + } 11.68 + lasttime = now 11.69 + return Time(now), clock_seq, nil 11.70 +} 11.71 + 11.72 +// ClockSequence returns the current clock sequence, generating one if not 11.73 +// already set. The clock sequence is only used for Version 1 UUIDs. 11.74 +// 11.75 +// The uuid package does not use global static storage for the clock sequence or 11.76 +// the last time a UUID was generated. Unless SetClockSequence a new random 11.77 +// clock sequence is generated the first time a clock sequence is requested by 11.78 +// ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated 11.79 +// for 11.80 +func ClockSequence() int { 11.81 + defer mu.Unlock() 11.82 + mu.Lock() 11.83 + return clockSequence() 11.84 +} 11.85 + 11.86 +func clockSequence() int { 11.87 + if clock_seq == 0 { 11.88 + setClockSequence(-1) 11.89 + } 11.90 + return int(clock_seq & 0x3fff) 11.91 +} 11.92 + 11.93 +// SetClockSeq sets the clock sequence to the lower 14 bits of seq. Setting to 11.94 +// -1 causes a new sequence to be generated. 11.95 +func SetClockSequence(seq int) { 11.96 + defer mu.Unlock() 11.97 + mu.Lock() 11.98 + setClockSequence(seq) 11.99 +} 11.100 + 11.101 +func setClockSequence(seq int) { 11.102 + if seq == -1 { 11.103 + var b [2]byte 11.104 + randomBits(b[:]) // clock sequence 11.105 + seq = int(b[0])<<8 | int(b[1]) 11.106 + } 11.107 + old_seq := clock_seq 11.108 + clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant 11.109 + if old_seq != clock_seq { 11.110 + lasttime = 0 11.111 + } 11.112 +} 11.113 + 11.114 +// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in 11.115 +// uuid. It returns false if uuid is not valid. The time is only well defined 11.116 +// for version 1 and 2 UUIDs. 11.117 +func (uuid UUID) Time() (Time, bool) { 11.118 + if len(uuid) != 16 { 11.119 + return 0, false 11.120 + } 11.121 + time := int64(binary.BigEndian.Uint32(uuid[0:4])) 11.122 + time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32 11.123 + time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48 11.124 + return Time(time), true 11.125 +} 11.126 + 11.127 +// ClockSequence returns the clock sequence encoded in uuid. It returns false 11.128 +// if uuid is not valid. The clock sequence is only well defined for version 1 11.129 +// and 2 UUIDs. 11.130 +func (uuid UUID) ClockSequence() (int, bool) { 11.131 + if len(uuid) != 16 { 11.132 + return 0, false 11.133 + } 11.134 + return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true 11.135 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/util.go Thu Nov 12 20:33:26 2015 -0800 12.3 @@ -0,0 +1,43 @@ 12.4 +// Copyright 2011 Google Inc. All rights reserved. 12.5 +// Use of this source code is governed by a BSD-style 12.6 +// license that can be found in the LICENSE file. 12.7 + 12.8 +package uuid 12.9 + 12.10 +import ( 12.11 + "io" 12.12 +) 12.13 + 12.14 +// randomBits completely fills slice b with random data. 12.15 +func randomBits(b []byte) { 12.16 + if _, err := io.ReadFull(rander, b); err != nil { 12.17 + panic(err.Error()) // rand should never fail 12.18 + } 12.19 +} 12.20 + 12.21 +// xvalues returns the value of a byte as a hexadecimal digit or 255. 12.22 +var xvalues = []byte{ 12.23 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.24 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.25 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.26 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 12.27 + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.28 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.29 + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.30 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.31 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.32 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.33 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.34 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.35 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.36 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.37 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.38 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 12.39 +} 12.40 + 12.41 +// xtob converts the the first two hex bytes of x into a byte. 12.42 +func xtob(x string) (byte, bool) { 12.43 + b1 := xvalues[x[0]] 12.44 + b2 := xvalues[x[1]] 12.45 + return (b1 << 4) | b2, b1 != 255 && b2 != 255 12.46 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/uuid.go Thu Nov 12 20:33:26 2015 -0800 13.3 @@ -0,0 +1,163 @@ 13.4 +// Copyright 2011 Google Inc. All rights reserved. 13.5 +// Use of this source code is governed by a BSD-style 13.6 +// license that can be found in the LICENSE file. 13.7 + 13.8 +package uuid 13.9 + 13.10 +import ( 13.11 + "bytes" 13.12 + "crypto/rand" 13.13 + "fmt" 13.14 + "io" 13.15 + "strings" 13.16 +) 13.17 + 13.18 +// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 13.19 +// 4122. 13.20 +type UUID []byte 13.21 + 13.22 +// A Version represents a UUIDs version. 13.23 +type Version byte 13.24 + 13.25 +// A Variant represents a UUIDs variant. 13.26 +type Variant byte 13.27 + 13.28 +// Constants returned by Variant. 13.29 +const ( 13.30 + Invalid = Variant(iota) // Invalid UUID 13.31 + RFC4122 // The variant specified in RFC4122 13.32 + Reserved // Reserved, NCS backward compatibility. 13.33 + Microsoft // Reserved, Microsoft Corporation backward compatibility. 13.34 + Future // Reserved for future definition. 13.35 +) 13.36 + 13.37 +var rander = rand.Reader // random function 13.38 + 13.39 +// New returns a new random (version 4) UUID as a string. It is a convenience 13.40 +// function for NewRandom().String(). 13.41 +func New() string { 13.42 + return NewRandom().String() 13.43 +} 13.44 + 13.45 +// Parse decodes s into a UUID or returns nil. Both the UUID form of 13.46 +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 13.47 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. 13.48 +func Parse(s string) UUID { 13.49 + if len(s) == 36+9 { 13.50 + if strings.ToLower(s[:9]) != "urn:uuid:" { 13.51 + return nil 13.52 + } 13.53 + s = s[9:] 13.54 + } else if len(s) != 36 { 13.55 + return nil 13.56 + } 13.57 + if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { 13.58 + return nil 13.59 + } 13.60 + uuid := make([]byte, 16) 13.61 + for i, x := range []int{ 13.62 + 0, 2, 4, 6, 13.63 + 9, 11, 13.64 + 14, 16, 13.65 + 19, 21, 13.66 + 24, 26, 28, 30, 32, 34} { 13.67 + if v, ok := xtob(s[x:]); !ok { 13.68 + return nil 13.69 + } else { 13.70 + uuid[i] = v 13.71 + } 13.72 + } 13.73 + return uuid 13.74 +} 13.75 + 13.76 +// Equal returns true if uuid1 and uuid2 are equal. 13.77 +func Equal(uuid1, uuid2 UUID) bool { 13.78 + return bytes.Equal(uuid1, uuid2) 13.79 +} 13.80 + 13.81 +// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 13.82 +// , or "" if uuid is invalid. 13.83 +func (uuid UUID) String() string { 13.84 + if uuid == nil || len(uuid) != 16 { 13.85 + return "" 13.86 + } 13.87 + b := []byte(uuid) 13.88 + return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", 13.89 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 13.90 +} 13.91 + 13.92 +// URN returns the RFC 2141 URN form of uuid, 13.93 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. 13.94 +func (uuid UUID) URN() string { 13.95 + if uuid == nil || len(uuid) != 16 { 13.96 + return "" 13.97 + } 13.98 + b := []byte(uuid) 13.99 + return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x", 13.100 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 13.101 +} 13.102 + 13.103 +// Variant returns the variant encoded in uuid. It returns Invalid if 13.104 +// uuid is invalid. 13.105 +func (uuid UUID) Variant() Variant { 13.106 + if len(uuid) != 16 { 13.107 + return Invalid 13.108 + } 13.109 + switch { 13.110 + case (uuid[8] & 0xc0) == 0x80: 13.111 + return RFC4122 13.112 + case (uuid[8] & 0xe0) == 0xc0: 13.113 + return Microsoft 13.114 + case (uuid[8] & 0xe0) == 0xe0: 13.115 + return Future 13.116 + default: 13.117 + return Reserved 13.118 + } 13.119 + panic("unreachable") 13.120 +} 13.121 + 13.122 +// Version returns the verison of uuid. It returns false if uuid is not 13.123 +// valid. 13.124 +func (uuid UUID) Version() (Version, bool) { 13.125 + if len(uuid) != 16 { 13.126 + return 0, false 13.127 + } 13.128 + return Version(uuid[6] >> 4), true 13.129 +} 13.130 + 13.131 +func (v Version) String() string { 13.132 + if v > 15 { 13.133 + return fmt.Sprintf("BAD_VERSION_%d", v) 13.134 + } 13.135 + return fmt.Sprintf("VERSION_%d", v) 13.136 +} 13.137 + 13.138 +func (v Variant) String() string { 13.139 + switch v { 13.140 + case RFC4122: 13.141 + return "RFC4122" 13.142 + case Reserved: 13.143 + return "Reserved" 13.144 + case Microsoft: 13.145 + return "Microsoft" 13.146 + case Future: 13.147 + return "Future" 13.148 + case Invalid: 13.149 + return "Invalid" 13.150 + } 13.151 + return fmt.Sprintf("BadVariant%d", int(v)) 13.152 +} 13.153 + 13.154 +// SetRand sets the random number generator to r, which implents io.Reader. 13.155 +// If r.Read returns an error when the package requests random data then 13.156 +// a panic will be issued. 13.157 +// 13.158 +// Calling SetRand with nil sets the random number generator to the default 13.159 +// generator. 13.160 +func SetRand(r io.Reader) { 13.161 + if r == nil { 13.162 + rander = rand.Reader 13.163 + return 13.164 + } 13.165 + rander = r 13.166 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/version1.go Thu Nov 12 20:33:26 2015 -0800 14.3 @@ -0,0 +1,41 @@ 14.4 +// Copyright 2011 Google Inc. All rights reserved. 14.5 +// Use of this source code is governed by a BSD-style 14.6 +// license that can be found in the LICENSE file. 14.7 + 14.8 +package uuid 14.9 + 14.10 +import ( 14.11 + "encoding/binary" 14.12 +) 14.13 + 14.14 +// NewUUID returns a Version 1 UUID based on the current NodeID and clock 14.15 +// sequence, and the current time. If the NodeID has not been set by SetNodeID 14.16 +// or SetNodeInterface then it will be set automatically. If the NodeID cannot 14.17 +// be set NewUUID returns nil. If clock sequence has not been set by 14.18 +// SetClockSequence then it will be set automatically. If GetTime fails to 14.19 +// return the current NewUUID returns nil. 14.20 +func NewUUID() UUID { 14.21 + if nodeID == nil { 14.22 + SetNodeInterface("") 14.23 + } 14.24 + 14.25 + now, seq, err := GetTime() 14.26 + if err != nil { 14.27 + return nil 14.28 + } 14.29 + 14.30 + uuid := make([]byte, 16) 14.31 + 14.32 + time_low := uint32(now & 0xffffffff) 14.33 + time_mid := uint16((now >> 32) & 0xffff) 14.34 + time_hi := uint16((now >> 48) & 0x0fff) 14.35 + time_hi |= 0x1000 // Version 1 14.36 + 14.37 + binary.BigEndian.PutUint32(uuid[0:], time_low) 14.38 + binary.BigEndian.PutUint16(uuid[4:], time_mid) 14.39 + binary.BigEndian.PutUint16(uuid[6:], time_hi) 14.40 + binary.BigEndian.PutUint16(uuid[8:], seq) 14.41 + copy(uuid[10:], nodeID) 14.42 + 14.43 + return uuid 14.44 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/vendor/code.google.com/p/go-uuid/uuid/version4.go Thu Nov 12 20:33:26 2015 -0800 15.3 @@ -0,0 +1,25 @@ 15.4 +// Copyright 2011 Google Inc. All rights reserved. 15.5 +// Use of this source code is governed by a BSD-style 15.6 +// license that can be found in the LICENSE file. 15.7 + 15.8 +package uuid 15.9 + 15.10 +// Random returns a Random (Version 4) UUID or panics. 15.11 +// 15.12 +// The strength of the UUIDs is based on the strength of the crypto/rand 15.13 +// package. 15.14 +// 15.15 +// A note about uniqueness derived from from the UUID Wikipedia entry: 15.16 +// 15.17 +// Randomly generated UUIDs have 122 random bits. One's annual risk of being 15.18 +// hit by a meteorite is estimated to be one chance in 17 billion, that 15.19 +// means the probability is about 0.00000000006 (6 × 10−11), 15.20 +// equivalent to the odds of creating a few tens of trillions of UUIDs in a 15.21 +// year and having one duplicate. 15.22 +func NewRandom() UUID { 15.23 + uuid := make([]byte, 16) 15.24 + randomBits([]byte(uuid)) 15.25 + uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4 15.26 + uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10 15.27 + return uuid 15.28 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/vendor/code.secondbit.org/uuid.hg/uuid.go Thu Nov 12 20:33:26 2015 -0800 16.3 @@ -0,0 +1,131 @@ 16.4 +package uuid 16.5 + 16.6 +import ( 16.7 + "database/sql/driver" 16.8 + "encoding/json" 16.9 + "encoding/xml" 16.10 + "errors" 16.11 + 16.12 + "code.google.com/p/go-uuid/uuid" 16.13 +) 16.14 + 16.15 +var InvalidIDError = errors.New("Invalid ID format.") 16.16 + 16.17 +type ID uuid.UUID 16.18 + 16.19 +func NewID() ID { 16.20 + return ID(uuid.NewRandom()) 16.21 +} 16.22 + 16.23 +func (id ID) String() string { 16.24 + return uuid.UUID(id).String() 16.25 +} 16.26 + 16.27 +func (id ID) IsZero() bool { 16.28 + if id == nil { 16.29 + return true 16.30 + } 16.31 + if len(id) == 0 { 16.32 + return true 16.33 + } 16.34 + return false 16.35 +} 16.36 + 16.37 +func (id ID) Copy() ID { 16.38 + resp, _ := Parse(id.String()) 16.39 + // ignore the error because they asked for a copy of the ID, they 16.40 + // never asked if it was valid or not. 16.41 + // This is, overall, not the most efficient way to do this (we're 16.42 + // essentially converting to a string and then back again) but the 16.43 + // computational complexity involved is pretty minor, and it allows 16.44 + // us to respect the boundaries between the packages, using only the 16.45 + // exported interfaces to perform a copy. And that seems pretty 16.46 + // valuable. 16.47 + return resp 16.48 +} 16.49 + 16.50 +func (id ID) MarshalJSON() ([]byte, error) { 16.51 + return json.Marshal(id.String()) 16.52 +} 16.53 + 16.54 +func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error { 16.55 + return e.EncodeElement(id.String(), start) 16.56 +} 16.57 + 16.58 +func (id ID) Value() (driver.Value, error) { 16.59 + return id.String(), nil 16.60 +} 16.61 + 16.62 +func (id *ID) Scan(src interface{}) error { 16.63 + if src == nil { 16.64 + id = nil 16.65 + return nil 16.66 + } 16.67 + switch src.(type) { 16.68 + case []byte: 16.69 + if len(src.([]byte)) == 0 { 16.70 + *id = (*id)[:0] 16.71 + return nil 16.72 + } 16.73 + newID, err := Parse(string(src.([]byte))) 16.74 + if err != nil { 16.75 + return err 16.76 + } 16.77 + *id = append((*id)[:0], newID...) 16.78 + return nil 16.79 + case string: 16.80 + if len(src.(string)) == 0 { 16.81 + *id = (*id)[:0] 16.82 + return nil 16.83 + } 16.84 + newID, err := Parse(src.(string)) 16.85 + if err != nil { 16.86 + return err 16.87 + } 16.88 + *id = append((*id)[:0], newID...) 16.89 + return nil 16.90 + default: 16.91 + return InvalidIDError 16.92 + } 16.93 + return nil 16.94 +} 16.95 + 16.96 +func (id *ID) UnmarshalJSON(in []byte) error { 16.97 + var tmp string 16.98 + err := json.Unmarshal(in, &tmp) 16.99 + if err != nil { 16.100 + return err 16.101 + } 16.102 + newID, err := Parse(tmp) 16.103 + if err != nil { 16.104 + return err 16.105 + } 16.106 + *id = append((*id)[:0], newID...) 16.107 + return nil 16.108 +} 16.109 + 16.110 +func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { 16.111 + var tmp string 16.112 + err := d.DecodeElement(&tmp, &start) 16.113 + if err != nil { 16.114 + return err 16.115 + } 16.116 + newID, err := Parse(tmp) 16.117 + if err != nil { 16.118 + return err 16.119 + } 16.120 + *id = append((*id)[:0], newID...) 16.121 + return nil 16.122 +} 16.123 + 16.124 +func Parse(in string) (ID, error) { 16.125 + id := ID(uuid.Parse(in)) 16.126 + if id == nil { 16.127 + return id, InvalidIDError 16.128 + } 16.129 + return id, nil 16.130 +} 16.131 + 16.132 +func (id ID) Equal(other ID) bool { 16.133 + return uuid.Equal(uuid.UUID(id), uuid.UUID(other)) 16.134 +}
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/vendor/golang.org/x/net/LICENSE Thu Nov 12 20:33:26 2015 -0800 17.3 @@ -0,0 +1,27 @@ 17.4 +Copyright (c) 2009 The Go Authors. All rights reserved. 17.5 + 17.6 +Redistribution and use in source and binary forms, with or without 17.7 +modification, are permitted provided that the following conditions are 17.8 +met: 17.9 + 17.10 + * Redistributions of source code must retain the above copyright 17.11 +notice, this list of conditions and the following disclaimer. 17.12 + * Redistributions in binary form must reproduce the above 17.13 +copyright notice, this list of conditions and the following disclaimer 17.14 +in the documentation and/or other materials provided with the 17.15 +distribution. 17.16 + * Neither the name of Google Inc. nor the names of its 17.17 +contributors may be used to endorse or promote products derived from 17.18 +this software without specific prior written permission. 17.19 + 17.20 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17.21 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17.22 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17.23 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 17.24 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 17.25 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 17.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 17.27 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 17.28 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 17.29 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 17.30 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/vendor/golang.org/x/net/PATENTS Thu Nov 12 20:33:26 2015 -0800 18.3 @@ -0,0 +1,22 @@ 18.4 +Additional IP Rights Grant (Patents) 18.5 + 18.6 +"This implementation" means the copyrightable works distributed by 18.7 +Google as part of the Go project. 18.8 + 18.9 +Google hereby grants to You a perpetual, worldwide, non-exclusive, 18.10 +no-charge, royalty-free, irrevocable (except as stated in this section) 18.11 +patent license to make, have made, use, offer to sell, sell, import, 18.12 +transfer and otherwise run, modify and propagate the contents of this 18.13 +implementation of Go, where such license applies only to those patent 18.14 +claims, both currently owned or controlled by Google and acquired in 18.15 +the future, licensable by Google that are necessarily infringed by this 18.16 +implementation of Go. This grant does not include claims that would be 18.17 +infringed only as a consequence of further modification of this 18.18 +implementation. If you or your agent or exclusive licensee institute or 18.19 +order or agree to the institution of patent litigation against any 18.20 +entity (including a cross-claim or counterclaim in a lawsuit) alleging 18.21 +that this implementation of Go or any code incorporated within this 18.22 +implementation of Go constitutes direct or contributory patent 18.23 +infringement, or inducement of patent infringement, then any patent 18.24 +rights granted to you under this License for this implementation of Go 18.25 +shall terminate as of the date such litigation is filed.
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/vendor/golang.org/x/net/context/context.go Thu Nov 12 20:33:26 2015 -0800 19.3 @@ -0,0 +1,447 @@ 19.4 +// Copyright 2014 The Go Authors. All rights reserved. 19.5 +// Use of this source code is governed by a BSD-style 19.6 +// license that can be found in the LICENSE file. 19.7 + 19.8 +// Package context defines the Context type, which carries deadlines, 19.9 +// cancelation signals, and other request-scoped values across API boundaries 19.10 +// and between processes. 19.11 +// 19.12 +// Incoming requests to a server should create a Context, and outgoing calls to 19.13 +// servers should accept a Context. The chain of function calls between must 19.14 +// propagate the Context, optionally replacing it with a modified copy created 19.15 +// using WithDeadline, WithTimeout, WithCancel, or WithValue. 19.16 +// 19.17 +// Programs that use Contexts should follow these rules to keep interfaces 19.18 +// consistent across packages and enable static analysis tools to check context 19.19 +// propagation: 19.20 +// 19.21 +// Do not store Contexts inside a struct type; instead, pass a Context 19.22 +// explicitly to each function that needs it. The Context should be the first 19.23 +// parameter, typically named ctx: 19.24 +// 19.25 +// func DoSomething(ctx context.Context, arg Arg) error { 19.26 +// // ... use ctx ... 19.27 +// } 19.28 +// 19.29 +// Do not pass a nil Context, even if a function permits it. Pass context.TODO 19.30 +// if you are unsure about which Context to use. 19.31 +// 19.32 +// Use context Values only for request-scoped data that transits processes and 19.33 +// APIs, not for passing optional parameters to functions. 19.34 +// 19.35 +// The same Context may be passed to functions running in different goroutines; 19.36 +// Contexts are safe for simultaneous use by multiple goroutines. 19.37 +// 19.38 +// See http://blog.golang.org/context for example code for a server that uses 19.39 +// Contexts. 19.40 +package context 19.41 + 19.42 +import ( 19.43 + "errors" 19.44 + "fmt" 19.45 + "sync" 19.46 + "time" 19.47 +) 19.48 + 19.49 +// A Context carries a deadline, a cancelation signal, and other values across 19.50 +// API boundaries. 19.51 +// 19.52 +// Context's methods may be called by multiple goroutines simultaneously. 19.53 +type Context interface { 19.54 + // Deadline returns the time when work done on behalf of this context 19.55 + // should be canceled. Deadline returns ok==false when no deadline is 19.56 + // set. Successive calls to Deadline return the same results. 19.57 + Deadline() (deadline time.Time, ok bool) 19.58 + 19.59 + // Done returns a channel that's closed when work done on behalf of this 19.60 + // context should be canceled. Done may return nil if this context can 19.61 + // never be canceled. Successive calls to Done return the same value. 19.62 + // 19.63 + // WithCancel arranges for Done to be closed when cancel is called; 19.64 + // WithDeadline arranges for Done to be closed when the deadline 19.65 + // expires; WithTimeout arranges for Done to be closed when the timeout 19.66 + // elapses. 19.67 + // 19.68 + // Done is provided for use in select statements: 19.69 + // 19.70 + // // Stream generates values with DoSomething and sends them to out 19.71 + // // until DoSomething returns an error or ctx.Done is closed. 19.72 + // func Stream(ctx context.Context, out <-chan Value) error { 19.73 + // for { 19.74 + // v, err := DoSomething(ctx) 19.75 + // if err != nil { 19.76 + // return err 19.77 + // } 19.78 + // select { 19.79 + // case <-ctx.Done(): 19.80 + // return ctx.Err() 19.81 + // case out <- v: 19.82 + // } 19.83 + // } 19.84 + // } 19.85 + // 19.86 + // See http://blog.golang.org/pipelines for more examples of how to use 19.87 + // a Done channel for cancelation. 19.88 + Done() <-chan struct{} 19.89 + 19.90 + // Err returns a non-nil error value after Done is closed. Err returns 19.91 + // Canceled if the context was canceled or DeadlineExceeded if the 19.92 + // context's deadline passed. No other values for Err are defined. 19.93 + // After Done is closed, successive calls to Err return the same value. 19.94 + Err() error 19.95 + 19.96 + // Value returns the value associated with this context for key, or nil 19.97 + // if no value is associated with key. Successive calls to Value with 19.98 + // the same key returns the same result. 19.99 + // 19.100 + // Use context values only for request-scoped data that transits 19.101 + // processes and API boundaries, not for passing optional parameters to 19.102 + // functions. 19.103 + // 19.104 + // A key identifies a specific value in a Context. Functions that wish 19.105 + // to store values in Context typically allocate a key in a global 19.106 + // variable then use that key as the argument to context.WithValue and 19.107 + // Context.Value. A key can be any type that supports equality; 19.108 + // packages should define keys as an unexported type to avoid 19.109 + // collisions. 19.110 + // 19.111 + // Packages that define a Context key should provide type-safe accessors 19.112 + // for the values stores using that key: 19.113 + // 19.114 + // // Package user defines a User type that's stored in Contexts. 19.115 + // package user 19.116 + // 19.117 + // import "golang.org/x/net/context" 19.118 + // 19.119 + // // User is the type of value stored in the Contexts. 19.120 + // type User struct {...} 19.121 + // 19.122 + // // key is an unexported type for keys defined in this package. 19.123 + // // This prevents collisions with keys defined in other packages. 19.124 + // type key int 19.125 + // 19.126 + // // userKey is the key for user.User values in Contexts. It is 19.127 + // // unexported; clients use user.NewContext and user.FromContext 19.128 + // // instead of using this key directly. 19.129 + // var userKey key = 0 19.130 + // 19.131 + // // NewContext returns a new Context that carries value u. 19.132 + // func NewContext(ctx context.Context, u *User) context.Context { 19.133 + // return context.WithValue(ctx, userKey, u) 19.134 + // } 19.135 + // 19.136 + // // FromContext returns the User value stored in ctx, if any. 19.137 + // func FromContext(ctx context.Context) (*User, bool) { 19.138 + // u, ok := ctx.Value(userKey).(*User) 19.139 + // return u, ok 19.140 + // } 19.141 + Value(key interface{}) interface{} 19.142 +} 19.143 + 19.144 +// Canceled is the error returned by Context.Err when the context is canceled. 19.145 +var Canceled = errors.New("context canceled") 19.146 + 19.147 +// DeadlineExceeded is the error returned by Context.Err when the context's 19.148 +// deadline passes. 19.149 +var DeadlineExceeded = errors.New("context deadline exceeded") 19.150 + 19.151 +// An emptyCtx is never canceled, has no values, and has no deadline. It is not 19.152 +// struct{}, since vars of this type must have distinct addresses. 19.153 +type emptyCtx int 19.154 + 19.155 +func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { 19.156 + return 19.157 +} 19.158 + 19.159 +func (*emptyCtx) Done() <-chan struct{} { 19.160 + return nil 19.161 +} 19.162 + 19.163 +func (*emptyCtx) Err() error { 19.164 + return nil 19.165 +} 19.166 + 19.167 +func (*emptyCtx) Value(key interface{}) interface{} { 19.168 + return nil 19.169 +} 19.170 + 19.171 +func (e *emptyCtx) String() string { 19.172 + switch e { 19.173 + case background: 19.174 + return "context.Background" 19.175 + case todo: 19.176 + return "context.TODO" 19.177 + } 19.178 + return "unknown empty Context" 19.179 +} 19.180 + 19.181 +var ( 19.182 + background = new(emptyCtx) 19.183 + todo = new(emptyCtx) 19.184 +) 19.185 + 19.186 +// Background returns a non-nil, empty Context. It is never canceled, has no 19.187 +// values, and has no deadline. It is typically used by the main function, 19.188 +// initialization, and tests, and as the top-level Context for incoming 19.189 +// requests. 19.190 +func Background() Context { 19.191 + return background 19.192 +} 19.193 + 19.194 +// TODO returns a non-nil, empty Context. Code should use context.TODO when 19.195 +// it's unclear which Context to use or it's is not yet available (because the 19.196 +// surrounding function has not yet been extended to accept a Context 19.197 +// parameter). TODO is recognized by static analysis tools that determine 19.198 +// whether Contexts are propagated correctly in a program. 19.199 +func TODO() Context { 19.200 + return todo 19.201 +} 19.202 + 19.203 +// A CancelFunc tells an operation to abandon its work. 19.204 +// A CancelFunc does not wait for the work to stop. 19.205 +// After the first call, subsequent calls to a CancelFunc do nothing. 19.206 +type CancelFunc func() 19.207 + 19.208 +// WithCancel returns a copy of parent with a new Done channel. The returned 19.209 +// context's Done channel is closed when the returned cancel function is called 19.210 +// or when the parent context's Done channel is closed, whichever happens first. 19.211 +// 19.212 +// Canceling this context releases resources associated with it, so code should 19.213 +// call cancel as soon as the operations running in this Context complete. 19.214 +func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { 19.215 + c := newCancelCtx(parent) 19.216 + propagateCancel(parent, &c) 19.217 + return &c, func() { c.cancel(true, Canceled) } 19.218 +} 19.219 + 19.220 +// newCancelCtx returns an initialized cancelCtx. 19.221 +func newCancelCtx(parent Context) cancelCtx { 19.222 + return cancelCtx{ 19.223 + Context: parent, 19.224 + done: make(chan struct{}), 19.225 + } 19.226 +} 19.227 + 19.228 +// propagateCancel arranges for child to be canceled when parent is. 19.229 +func propagateCancel(parent Context, child canceler) { 19.230 + if parent.Done() == nil { 19.231 + return // parent is never canceled 19.232 + } 19.233 + if p, ok := parentCancelCtx(parent); ok { 19.234 + p.mu.Lock() 19.235 + if p.err != nil { 19.236 + // parent has already been canceled 19.237 + child.cancel(false, p.err) 19.238 + } else { 19.239 + if p.children == nil { 19.240 + p.children = make(map[canceler]bool) 19.241 + } 19.242 + p.children[child] = true 19.243 + } 19.244 + p.mu.Unlock() 19.245 + } else { 19.246 + go func() { 19.247 + select { 19.248 + case <-parent.Done(): 19.249 + child.cancel(false, parent.Err()) 19.250 + case <-child.Done(): 19.251 + } 19.252 + }() 19.253 + } 19.254 +} 19.255 + 19.256 +// parentCancelCtx follows a chain of parent references until it finds a 19.257 +// *cancelCtx. This function understands how each of the concrete types in this 19.258 +// package represents its parent. 19.259 +func parentCancelCtx(parent Context) (*cancelCtx, bool) { 19.260 + for { 19.261 + switch c := parent.(type) { 19.262 + case *cancelCtx: 19.263 + return c, true 19.264 + case *timerCtx: 19.265 + return &c.cancelCtx, true 19.266 + case *valueCtx: 19.267 + parent = c.Context 19.268 + default: 19.269 + return nil, false 19.270 + } 19.271 + } 19.272 +} 19.273 + 19.274 +// removeChild removes a context from its parent. 19.275 +func removeChild(parent Context, child canceler) { 19.276 + p, ok := parentCancelCtx(parent) 19.277 + if !ok { 19.278 + return 19.279 + } 19.280 + p.mu.Lock() 19.281 + if p.children != nil { 19.282 + delete(p.children, child) 19.283 + } 19.284 + p.mu.Unlock() 19.285 +} 19.286 + 19.287 +// A canceler is a context type that can be canceled directly. The 19.288 +// implementations are *cancelCtx and *timerCtx. 19.289 +type canceler interface { 19.290 + cancel(removeFromParent bool, err error) 19.291 + Done() <-chan struct{} 19.292 +} 19.293 + 19.294 +// A cancelCtx can be canceled. When canceled, it also cancels any children 19.295 +// that implement canceler. 19.296 +type cancelCtx struct { 19.297 + Context 19.298 + 19.299 + done chan struct{} // closed by the first cancel call. 19.300 + 19.301 + mu sync.Mutex 19.302 + children map[canceler]bool // set to nil by the first cancel call 19.303 + err error // set to non-nil by the first cancel call 19.304 +} 19.305 + 19.306 +func (c *cancelCtx) Done() <-chan struct{} { 19.307 + return c.done 19.308 +} 19.309 + 19.310 +func (c *cancelCtx) Err() error { 19.311 + c.mu.Lock() 19.312 + defer c.mu.Unlock() 19.313 + return c.err 19.314 +} 19.315 + 19.316 +func (c *cancelCtx) String() string { 19.317 + return fmt.Sprintf("%v.WithCancel", c.Context) 19.318 +} 19.319 + 19.320 +// cancel closes c.done, cancels each of c's children, and, if 19.321 +// removeFromParent is true, removes c from its parent's children. 19.322 +func (c *cancelCtx) cancel(removeFromParent bool, err error) { 19.323 + if err == nil { 19.324 + panic("context: internal error: missing cancel error") 19.325 + } 19.326 + c.mu.Lock() 19.327 + if c.err != nil { 19.328 + c.mu.Unlock() 19.329 + return // already canceled 19.330 + } 19.331 + c.err = err 19.332 + close(c.done) 19.333 + for child := range c.children { 19.334 + // NOTE: acquiring the child's lock while holding parent's lock. 19.335 + child.cancel(false, err) 19.336 + } 19.337 + c.children = nil 19.338 + c.mu.Unlock() 19.339 + 19.340 + if removeFromParent { 19.341 + removeChild(c.Context, c) 19.342 + } 19.343 +} 19.344 + 19.345 +// WithDeadline returns a copy of the parent context with the deadline adjusted 19.346 +// to be no later than d. If the parent's deadline is already earlier than d, 19.347 +// WithDeadline(parent, d) is semantically equivalent to parent. The returned 19.348 +// context's Done channel is closed when the deadline expires, when the returned 19.349 +// cancel function is called, or when the parent context's Done channel is 19.350 +// closed, whichever happens first. 19.351 +// 19.352 +// Canceling this context releases resources associated with it, so code should 19.353 +// call cancel as soon as the operations running in this Context complete. 19.354 +func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { 19.355 + if cur, ok := parent.Deadline(); ok && cur.Before(deadline) { 19.356 + // The current deadline is already sooner than the new one. 19.357 + return WithCancel(parent) 19.358 + } 19.359 + c := &timerCtx{ 19.360 + cancelCtx: newCancelCtx(parent), 19.361 + deadline: deadline, 19.362 + } 19.363 + propagateCancel(parent, c) 19.364 + d := deadline.Sub(time.Now()) 19.365 + if d <= 0 { 19.366 + c.cancel(true, DeadlineExceeded) // deadline has already passed 19.367 + return c, func() { c.cancel(true, Canceled) } 19.368 + } 19.369 + c.mu.Lock() 19.370 + defer c.mu.Unlock() 19.371 + if c.err == nil { 19.372 + c.timer = time.AfterFunc(d, func() { 19.373 + c.cancel(true, DeadlineExceeded) 19.374 + }) 19.375 + } 19.376 + return c, func() { c.cancel(true, Canceled) } 19.377 +} 19.378 + 19.379 +// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to 19.380 +// implement Done and Err. It implements cancel by stopping its timer then 19.381 +// delegating to cancelCtx.cancel. 19.382 +type timerCtx struct { 19.383 + cancelCtx 19.384 + timer *time.Timer // Under cancelCtx.mu. 19.385 + 19.386 + deadline time.Time 19.387 +} 19.388 + 19.389 +func (c *timerCtx) Deadline() (deadline time.Time, ok bool) { 19.390 + return c.deadline, true 19.391 +} 19.392 + 19.393 +func (c *timerCtx) String() string { 19.394 + return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now())) 19.395 +} 19.396 + 19.397 +func (c *timerCtx) cancel(removeFromParent bool, err error) { 19.398 + c.cancelCtx.cancel(false, err) 19.399 + if removeFromParent { 19.400 + // Remove this timerCtx from its parent cancelCtx's children. 19.401 + removeChild(c.cancelCtx.Context, c) 19.402 + } 19.403 + c.mu.Lock() 19.404 + if c.timer != nil { 19.405 + c.timer.Stop() 19.406 + c.timer = nil 19.407 + } 19.408 + c.mu.Unlock() 19.409 +} 19.410 + 19.411 +// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). 19.412 +// 19.413 +// Canceling this context releases resources associated with it, so code should 19.414 +// call cancel as soon as the operations running in this Context complete: 19.415 +// 19.416 +// func slowOperationWithTimeout(ctx context.Context) (Result, error) { 19.417 +// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) 19.418 +// defer cancel() // releases resources if slowOperation completes before timeout elapses 19.419 +// return slowOperation(ctx) 19.420 +// } 19.421 +func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { 19.422 + return WithDeadline(parent, time.Now().Add(timeout)) 19.423 +} 19.424 + 19.425 +// WithValue returns a copy of parent in which the value associated with key is 19.426 +// val. 19.427 +// 19.428 +// Use context Values only for request-scoped data that transits processes and 19.429 +// APIs, not for passing optional parameters to functions. 19.430 +func WithValue(parent Context, key interface{}, val interface{}) Context { 19.431 + return &valueCtx{parent, key, val} 19.432 +} 19.433 + 19.434 +// A valueCtx carries a key-value pair. It implements Value for that key and 19.435 +// delegates all other calls to the embedded Context. 19.436 +type valueCtx struct { 19.437 + Context 19.438 + key, val interface{} 19.439 +} 19.440 + 19.441 +func (c *valueCtx) String() string { 19.442 + return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val) 19.443 +} 19.444 + 19.445 +func (c *valueCtx) Value(key interface{}) interface{} { 19.446 + if c.key == key { 19.447 + return c.val 19.448 + } 19.449 + return c.Context.Value(key) 19.450 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/vendor/golang.org/x/net/context/ctxhttp/cancelreq.go Thu Nov 12 20:33:26 2015 -0800 20.3 @@ -0,0 +1,18 @@ 20.4 +// Copyright 2015 The Go Authors. All rights reserved. 20.5 +// Use of this source code is governed by a BSD-style 20.6 +// license that can be found in the LICENSE file. 20.7 + 20.8 +// +build go1.5 20.9 + 20.10 +package ctxhttp 20.11 + 20.12 +import "net/http" 20.13 + 20.14 +func canceler(client *http.Client, req *http.Request) func() { 20.15 + ch := make(chan struct{}) 20.16 + req.Cancel = ch 20.17 + 20.18 + return func() { 20.19 + close(ch) 20.20 + } 20.21 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/vendor/golang.org/x/net/context/ctxhttp/cancelreq_go14.go Thu Nov 12 20:33:26 2015 -0800 21.3 @@ -0,0 +1,23 @@ 21.4 +// Copyright 2015 The Go Authors. All rights reserved. 21.5 +// Use of this source code is governed by a BSD-style 21.6 +// license that can be found in the LICENSE file. 21.7 + 21.8 +// +build !go1.5 21.9 + 21.10 +package ctxhttp 21.11 + 21.12 +import "net/http" 21.13 + 21.14 +type requestCanceler interface { 21.15 + CancelRequest(*http.Request) 21.16 +} 21.17 + 21.18 +func canceler(client *http.Client, req *http.Request) func() { 21.19 + rc, ok := client.Transport.(requestCanceler) 21.20 + if !ok { 21.21 + return func() {} 21.22 + } 21.23 + return func() { 21.24 + rc.CancelRequest(req) 21.25 + } 21.26 +}
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go Thu Nov 12 20:33:26 2015 -0800 22.3 @@ -0,0 +1,79 @@ 22.4 +// Copyright 2015 The Go Authors. All rights reserved. 22.5 +// Use of this source code is governed by a BSD-style 22.6 +// license that can be found in the LICENSE file. 22.7 + 22.8 +// Package ctxhttp provides helper functions for performing context-aware HTTP requests. 22.9 +package ctxhttp 22.10 + 22.11 +import ( 22.12 + "io" 22.13 + "net/http" 22.14 + "net/url" 22.15 + "strings" 22.16 + 22.17 + "golang.org/x/net/context" 22.18 +) 22.19 + 22.20 +// Do sends an HTTP request with the provided http.Client and returns an HTTP response. 22.21 +// If the client is nil, http.DefaultClient is used. 22.22 +// If the context is canceled or times out, ctx.Err() will be returned. 22.23 +func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { 22.24 + if client == nil { 22.25 + client = http.DefaultClient 22.26 + } 22.27 + 22.28 + // Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go. 22.29 + cancel := canceler(client, req) 22.30 + 22.31 + type responseAndError struct { 22.32 + resp *http.Response 22.33 + err error 22.34 + } 22.35 + result := make(chan responseAndError, 1) 22.36 + 22.37 + go func() { 22.38 + resp, err := client.Do(req) 22.39 + result <- responseAndError{resp, err} 22.40 + }() 22.41 + 22.42 + select { 22.43 + case <-ctx.Done(): 22.44 + cancel() 22.45 + return nil, ctx.Err() 22.46 + case r := <-result: 22.47 + return r.resp, r.err 22.48 + } 22.49 +} 22.50 + 22.51 +// Get issues a GET request via the Do function. 22.52 +func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) { 22.53 + req, err := http.NewRequest("GET", url, nil) 22.54 + if err != nil { 22.55 + return nil, err 22.56 + } 22.57 + return Do(ctx, client, req) 22.58 +} 22.59 + 22.60 +// Head issues a HEAD request via the Do function. 22.61 +func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) { 22.62 + req, err := http.NewRequest("HEAD", url, nil) 22.63 + if err != nil { 22.64 + return nil, err 22.65 + } 22.66 + return Do(ctx, client, req) 22.67 +} 22.68 + 22.69 +// Post issues a POST request via the Do function. 22.70 +func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) { 22.71 + req, err := http.NewRequest("POST", url, body) 22.72 + if err != nil { 22.73 + return nil, err 22.74 + } 22.75 + req.Header.Set("Content-Type", bodyType) 22.76 + return Do(ctx, client, req) 22.77 +} 22.78 + 22.79 +// PostForm issues a POST request via the Do function. 22.80 +func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) { 22.81 + return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) 22.82 +}