ducky/devices

Paddy 2015-11-12 Child:a700ede02f91

0:b6494e1a499e Go to Latest

ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go

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.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vendor/code.secondbit.org/uuid.hg/uuid.go	Thu Nov 12 20:33:26 2015 -0800
     1.3 @@ -0,0 +1,131 @@
     1.4 +package uuid
     1.5 +
     1.6 +import (
     1.7 +	"database/sql/driver"
     1.8 +	"encoding/json"
     1.9 +	"encoding/xml"
    1.10 +	"errors"
    1.11 +
    1.12 +	"code.google.com/p/go-uuid/uuid"
    1.13 +)
    1.14 +
    1.15 +var InvalidIDError = errors.New("Invalid ID format.")
    1.16 +
    1.17 +type ID uuid.UUID
    1.18 +
    1.19 +func NewID() ID {
    1.20 +	return ID(uuid.NewRandom())
    1.21 +}
    1.22 +
    1.23 +func (id ID) String() string {
    1.24 +	return uuid.UUID(id).String()
    1.25 +}
    1.26 +
    1.27 +func (id ID) IsZero() bool {
    1.28 +	if id == nil {
    1.29 +		return true
    1.30 +	}
    1.31 +	if len(id) == 0 {
    1.32 +		return true
    1.33 +	}
    1.34 +	return false
    1.35 +}
    1.36 +
    1.37 +func (id ID) Copy() ID {
    1.38 +	resp, _ := Parse(id.String())
    1.39 +	// ignore the error because they asked for a copy of the ID, they
    1.40 +	// never asked if it was valid or not.
    1.41 +	// This is, overall, not the most efficient way to do this (we're
    1.42 +	// essentially converting to a string and then back again) but the
    1.43 +	// computational complexity involved is pretty minor, and it allows
    1.44 +	// us to respect the boundaries between the packages, using only the
    1.45 +	// exported interfaces to perform a copy. And that seems pretty
    1.46 +	// valuable.
    1.47 +	return resp
    1.48 +}
    1.49 +
    1.50 +func (id ID) MarshalJSON() ([]byte, error) {
    1.51 +	return json.Marshal(id.String())
    1.52 +}
    1.53 +
    1.54 +func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    1.55 +	return e.EncodeElement(id.String(), start)
    1.56 +}
    1.57 +
    1.58 +func (id ID) Value() (driver.Value, error) {
    1.59 +	return id.String(), nil
    1.60 +}
    1.61 +
    1.62 +func (id *ID) Scan(src interface{}) error {
    1.63 +	if src == nil {
    1.64 +		id = nil
    1.65 +		return nil
    1.66 +	}
    1.67 +	switch src.(type) {
    1.68 +	case []byte:
    1.69 +		if len(src.([]byte)) == 0 {
    1.70 +			*id = (*id)[:0]
    1.71 +			return nil
    1.72 +		}
    1.73 +		newID, err := Parse(string(src.([]byte)))
    1.74 +		if err != nil {
    1.75 +			return err
    1.76 +		}
    1.77 +		*id = append((*id)[:0], newID...)
    1.78 +		return nil
    1.79 +	case string:
    1.80 +		if len(src.(string)) == 0 {
    1.81 +			*id = (*id)[:0]
    1.82 +			return nil
    1.83 +		}
    1.84 +		newID, err := Parse(src.(string))
    1.85 +		if err != nil {
    1.86 +			return err
    1.87 +		}
    1.88 +		*id = append((*id)[:0], newID...)
    1.89 +		return nil
    1.90 +	default:
    1.91 +		return InvalidIDError
    1.92 +	}
    1.93 +	return nil
    1.94 +}
    1.95 +
    1.96 +func (id *ID) UnmarshalJSON(in []byte) error {
    1.97 +	var tmp string
    1.98 +	err := json.Unmarshal(in, &tmp)
    1.99 +	if err != nil {
   1.100 +		return err
   1.101 +	}
   1.102 +	newID, err := Parse(tmp)
   1.103 +	if err != nil {
   1.104 +		return err
   1.105 +	}
   1.106 +	*id = append((*id)[:0], newID...)
   1.107 +	return nil
   1.108 +}
   1.109 +
   1.110 +func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
   1.111 +	var tmp string
   1.112 +	err := d.DecodeElement(&tmp, &start)
   1.113 +	if err != nil {
   1.114 +		return err
   1.115 +	}
   1.116 +	newID, err := Parse(tmp)
   1.117 +	if err != nil {
   1.118 +		return err
   1.119 +	}
   1.120 +	*id = append((*id)[:0], newID...)
   1.121 +	return nil
   1.122 +}
   1.123 +
   1.124 +func Parse(in string) (ID, error) {
   1.125 +	id := ID(uuid.Parse(in))
   1.126 +	if id == nil {
   1.127 +		return id, InvalidIDError
   1.128 +	}
   1.129 +	return id, nil
   1.130 +}
   1.131 +
   1.132 +func (id ID) Equal(other ID) bool {
   1.133 +	return uuid.Equal(uuid.UUID(id), uuid.UUID(other))
   1.134 +}