google / uuid

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
BSD 3-Clause "New" or "Revised" License
5.26k stars 362 forks source link

Requesting some clarification on a sentence in README #19

Closed shawnps closed 7 years ago

shawnps commented 7 years ago

Hi, could someone confirm if my understanding of this sentence is correct?

"One loss due to this change is the ability to represent an invalid UUID (vs a NIL UUID)."

This is saying that it's not possible to represent an invalid UUID with the google/uuid library because it is a strict 16-byte array rather than a byte slice? When/why would I want to be able to represent invalid UUIDs, for testing?

Thank you!

pborman commented 7 years ago

Yes, that is the correct understanding.

The original uuid package (github.com/pborman/uuid) was based on slices has the following function:

func Parse(s string) UUID

This package had to change this to:

func Parse(s string) (UUID, error)

If you have a type that contains an optional UUID nothing special needs to be added for a byte slice representation:

type S struct {
        ID uuid.UUID
}

if s.ID != nil {
        fmt.Printf("ID is %v\n", s.ID)
}

With an array it becomes be one of

type S1 struct {
        ID     uuid.UUID
        HaveID bool
}
type S2 struct {
        ID *uuid.UUID
}

if s1.HaveID {
        fmt.Printf("ID is %v\n", s1.ID)
}
if s2.ID != nil {
        fmt.Printf("ID is %v\n", *s2.ID)
}

This is the only reason that there is both a github/pborman/uuid package and a github/google/uuid package (the pborman version is the original, the google version addresses the desire of making UUIDs comparable objects by switch from a slice to an array. It changes the code from:

if uuid.Compare(uuid1, uuid2) {

to

if uuid1 == uuid2 {

and also allows UUIDs to be used as keys in a map.

shawnps commented 7 years ago

@pborman I see, thank you for the clarification! I'll close this issue out.