uuid

Paddy 2015-11-12 Parent:1961288312a6 Child:6b0a9efd1318

4:cda03b52c8c9 Browse Files

Add Copy() and IsZero() methods as helpers. Use id.Copy() to return a copy of the ID that shares nothing with the source ID. id.IsZero() returns true if the ID is equivalent to the zero value for the type.

uuid.go

     1.1 --- a/uuid.go	Tue Jul 14 00:07:25 2015 -0400
     1.2 +++ b/uuid.go	Thu Nov 12 20:16:23 2015 -0800
     1.3 @@ -21,6 +21,29 @@
     1.4  	return uuid.UUID(id).String()
     1.5  }
     1.6  
     1.7 +func (id ID) IsZero() bool {
     1.8 +	if id == nil {
     1.9 +		return true
    1.10 +	}
    1.11 +	if len(id) == 0 {
    1.12 +		return true
    1.13 +	}
    1.14 +	return false
    1.15 +}
    1.16 +
    1.17 +func (id ID) Copy() ID {
    1.18 +	resp, _ := Parse(id.String())
    1.19 +	// ignore the error because they asked for a copy of the ID, they
    1.20 +	// never asked if it was valid or not.
    1.21 +	// This is, overall, not the most efficient way to do this (we're
    1.22 +	// essentially converting to a string and then back again) but the
    1.23 +	// computational complexity involved is pretty minor, and it allows
    1.24 +	// us to respect the boundaries between the packages, using only the
    1.25 +	// exported interfaces to perform a copy. And that seems pretty
    1.26 +	// valuable.
    1.27 +	return resp
    1.28 +}
    1.29 +
    1.30  func (id ID) MarshalJSON() ([]byte, error) {
    1.31  	return json.Marshal(id.String())
    1.32  }