Skip to content

Commit

Permalink
Add support for setting variant bits other than RFC4122.
Browse files Browse the repository at this point in the history
  • Loading branch information
satori committed Jan 3, 2018
1 parent 0633591 commit f58768c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
10 changes: 5 additions & 5 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (g *generator) NewV1() UUID {
copy(u[10:], hardwareAddr)

u.SetVersion(V1)
u.SetVariant()
u.SetVariant(VariantRFC4122)

return u
}
Expand All @@ -133,7 +133,7 @@ func (g *generator) NewV2(domain byte) UUID {
copy(u[10:], hardwareAddr)

u.SetVersion(V2)
u.SetVariant()
u.SetVariant(VariantRFC4122)

return u
}
Expand All @@ -142,7 +142,7 @@ func (g *generator) NewV2(domain byte) UUID {
func (g *generator) NewV3(ns UUID, name string) UUID {
u := newFromHash(md5.New(), ns, name)
u.SetVersion(V3)
u.SetVariant()
u.SetVariant(VariantRFC4122)

return u
}
Expand All @@ -152,7 +152,7 @@ func (g *generator) NewV4() UUID {
u := UUID{}
g.safeRandom(u[:])
u.SetVersion(V4)
u.SetVariant()
u.SetVariant(VariantRFC4122)

return u
}
Expand All @@ -161,7 +161,7 @@ func (g *generator) NewV4() UUID {
func (g *generator) NewV5(ns UUID, name string) UUID {
u := newFromHash(sha1.New(), ns, name)
u.SetVersion(V5)
u.SetVariant()
u.SetVariant(VariantRFC4122)

return u
}
Expand Down
28 changes: 21 additions & 7 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,17 @@ func (u UUID) Version() byte {
// Variant returns UUID layout variant.
func (u UUID) Variant() byte {
switch {
case (u[8] & 0x80) == 0x00:
case (u[8] >> 7) == 0x00:
return VariantNCS
case (u[8]&0xc0)|0x80 == 0x80:
case (u[8] >> 6) == 0x02:
return VariantRFC4122
case (u[8]&0xe0)|0xc0 == 0xc0:
case (u[8] >> 5) == 0x06:
return VariantMicrosoft
case (u[8] >> 5) == 0x07:
fallthrough
default:
return VariantFuture
}
return VariantFuture
}

// Bytes returns bytes slice representation of UUID.
Expand Down Expand Up @@ -130,9 +133,20 @@ func (u *UUID) SetVersion(v byte) {
u[6] = (u[6] & 0x0f) | (v << 4)
}

// SetVariant sets variant bits as described in RFC 4122.
func (u *UUID) SetVariant() {
u[8] = (u[8] & 0xbf) | 0x80
// SetVariant sets variant bits.
func (u *UUID) SetVariant(v byte) {
switch v {
case VariantNCS:
u[8] = (u[8]&(0xff>>1) | (0x00 << 7))
case VariantRFC4122:
u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
case VariantMicrosoft:
u[8] = (u[8]&(0xff>>3) | (0x06 << 5))
case VariantFuture:
fallthrough
default:
u[8] = (u[8]&(0xff>>3) | (0x07 << 5))
}
}

// Must is a helper that wraps a call to a function returning (UUID, error)
Expand Down
8 changes: 7 additions & 1 deletion uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (s *testSuite) TestVariant(c *C) {

func (s *testSuite) TestSetVariant(c *C) {
u := UUID{}
u.SetVariant()
u.SetVariant(VariantNCS)
c.Assert(u.Variant(), Equals, VariantNCS)
u.SetVariant(VariantRFC4122)
c.Assert(u.Variant(), Equals, VariantRFC4122)
u.SetVariant(VariantMicrosoft)
c.Assert(u.Variant(), Equals, VariantMicrosoft)
u.SetVariant(VariantFuture)
c.Assert(u.Variant(), Equals, VariantFuture)
}

0 comments on commit f58768c

Please sign in to comment.