uuid

Paddy 2015-01-09 Parent:a8d80021a45f Child:1961288312a6

2:f095a179ce00 Go to Latest

uuid/uuid.go

Revert to UUID format. We used to hex encode the bytes from the UUID when encoding/printing it. Let's stop doing that, and use proper UUID.

History
     1.1 --- a/uuid.go	Wed Dec 17 22:28:44 2014 -0500
     1.2 +++ b/uuid.go	Fri Jan 09 02:42:22 2015 -0500
     1.3 @@ -2,7 +2,6 @@
     1.4  
     1.5  import (
     1.6  	"database/sql/driver"
     1.7 -	"encoding/hex"
     1.8  	"encoding/json"
     1.9  	"encoding/xml"
    1.10  	"errors"
    1.11 @@ -18,6 +17,10 @@
    1.12  	return ID(uuid.NewRandom())
    1.13  }
    1.14  
    1.15 +func (id ID) String() string {
    1.16 +	return uuid.UUID(id).String()
    1.17 +}
    1.18 +
    1.19  func (id ID) MarshalJSON() ([]byte, error) {
    1.20  	return json.Marshal(id.String())
    1.21  }
    1.22 @@ -26,10 +29,6 @@
    1.23  	return e.EncodeElement(id.String(), start)
    1.24  }
    1.25  
    1.26 -func (id ID) String() string {
    1.27 -	return hex.EncodeToString([]byte(id))
    1.28 -}
    1.29 -
    1.30  func (id ID) Value() (driver.Value, error) {
    1.31  	return id.String(), nil
    1.32  }
    1.33 @@ -42,12 +41,18 @@
    1.34  	switch src.(type) {
    1.35  	case []byte:
    1.36  		newID, err := Parse(string(src.([]byte)))
    1.37 +		if err != nil {
    1.38 +			return err
    1.39 +		}
    1.40  		*id = append((*id)[:0], newID...)
    1.41 -		return err
    1.42 +		return nil
    1.43  	case string:
    1.44  		newID, err := Parse(src.(string))
    1.45 +		if err != nil {
    1.46 +			return err
    1.47 +		}
    1.48  		*id = append((*id)[:0], newID...)
    1.49 -		return err
    1.50 +		return nil
    1.51  	default:
    1.52  		return InvalidIDError
    1.53  	}
    1.54 @@ -83,11 +88,11 @@
    1.55  }
    1.56  
    1.57  func Parse(in string) (ID, error) {
    1.58 -	b, err := hex.DecodeString(in)
    1.59 -	if err != nil {
    1.60 -		return nil, err
    1.61 +	id := ID(uuid.Parse(in))
    1.62 +	if id == nil {
    1.63 +		return id, InvalidIDError
    1.64  	}
    1.65 -	return ID(b), nil
    1.66 +	return id, nil
    1.67  }
    1.68  
    1.69  func (id ID) Equal(other ID) bool {