2018-09-14 08:06:03 +00:00
|
|
|
package egoscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-09-14 08:06:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UUID holds a UUID v4
|
|
|
|
type UUID struct {
|
|
|
|
uuid.UUID
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
// DeepCopy create a true copy of the receiver.
|
|
|
|
func (u *UUID) DeepCopy() *UUID {
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := [uuid.Size]byte{}
|
|
|
|
copy(out[:], u.Bytes())
|
|
|
|
|
|
|
|
return &UUID{
|
|
|
|
(uuid.UUID)(out),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepCopyInto copies the receiver into out.
|
|
|
|
//
|
|
|
|
// In must be non nil.
|
|
|
|
func (u *UUID) DeepCopyInto(out *UUID) {
|
|
|
|
o := [uuid.Size]byte{}
|
|
|
|
copy(o[:], u.Bytes())
|
|
|
|
|
|
|
|
out.UUID = (uuid.UUID)(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if itself is equal to other.
|
2018-09-14 08:06:03 +00:00
|
|
|
func (u UUID) Equal(other UUID) bool {
|
|
|
|
return uuid.Equal(u.UUID, other.UUID)
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
// UnmarshalJSON unmarshals the raw JSON into the UUID.
|
2018-09-14 08:06:03 +00:00
|
|
|
func (u *UUID) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
new, err := ParseUUID(s)
|
|
|
|
if err == nil {
|
|
|
|
u.UUID = new.UUID
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
// MarshalJSON converts the UUID to a string representation.
|
2018-09-14 08:06:03 +00:00
|
|
|
func (u UUID) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf("%q", u.String())), nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
// ParseUUID parses a string into a UUID.
|
2018-09-14 08:06:03 +00:00
|
|
|
func ParseUUID(s string) (*UUID, error) {
|
|
|
|
u, err := uuid.FromString(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &UUID{u}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:04:04 +00:00
|
|
|
// MustParseUUID acts like ParseUUID but panic in case of a failure.
|
2018-09-14 08:06:03 +00:00
|
|
|
func MustParseUUID(s string) *UUID {
|
|
|
|
u, e := ParseUUID(s)
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|