Skip to content

Commit 9e951e1

Browse files
committed
Change a UUID from []byte to [16]byte along with other API changes.
1 parent 82d3e37 commit 9e951e1

File tree

18 files changed

+395
-353
lines changed

18 files changed

+395
-353
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# How to contribute
2+
3+
We definitely welcome patches and contribution to this project!
4+
5+
### Legal requirements
6+
7+
In order to protect both you and ourselves, you will need to sign the
8+
[Contributor License Agreement](https://cla.developers.google.com/clas).
9+
10+
You may have already signed it for other Google projects.

CONTRIBUTORS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
Paul Borman <borman@google.com>
2+
bmatsuo
3+
shawnps
4+
theory
5+
jboverfelt
6+
dsymonds
7+
cd1
8+
wallclockbuilder
9+
dansouza

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ This project was automatically exported from code.google.com/p/go-uuid
33
# uuid ![build status](https://travis-ci.org/google/uuid.svg?branch=master)
44
The uuid package generates and inspects UUIDs based on [RFC 412](http://tools.ietf.org/html/rfc4122) and DCE 1.1: Authentication and Security Services.
55

6+
This package is based on the github.com/pborman/uuid package (previously named
7+
code.google.com/p/go-uuid). It differs from these earlier packages in that
8+
a UUID is a 16 byte array rather than a byte slice. One loss due to this
9+
change is the ability to represent an invalid UUID (vs a NIL UUID).
10+
611
###### Install
712
`go get github.com/google/uuid`
813

dce.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2011 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -29,46 +29,42 @@ const (
2929
//
3030
// For a given domain/id pair the same token may be returned for up to
3131
// 7 minutes and 10 seconds.
32-
func NewDCESecurity(domain Domain, id uint32) UUID {
33-
uuid := NewUUID()
34-
if uuid != nil {
32+
func NewDCESecurity(domain Domain, id uint32) (UUID, error) {
33+
uuid, err := NewUUID()
34+
if err == nil {
3535
uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2
3636
uuid[9] = byte(domain)
3737
binary.BigEndian.PutUint32(uuid[0:], id)
3838
}
39-
return uuid
39+
return uuid, err
4040
}
4141

4242
// NewDCEPerson returns a DCE Security (Version 2) UUID in the person
4343
// domain with the id returned by os.Getuid.
4444
//
4545
// NewDCEPerson(Person, uint32(os.Getuid()))
46-
func NewDCEPerson() UUID {
46+
func NewDCEPerson() (UUID, error) {
4747
return NewDCESecurity(Person, uint32(os.Getuid()))
4848
}
4949

5050
// NewDCEGroup returns a DCE Security (Version 2) UUID in the group
5151
// domain with the id returned by os.Getgid.
5252
//
5353
// NewDCEGroup(Group, uint32(os.Getgid()))
54-
func NewDCEGroup() UUID {
54+
func NewDCEGroup() (UUID, error) {
5555
return NewDCESecurity(Group, uint32(os.Getgid()))
5656
}
5757

58-
// Domain returns the domain for a Version 2 UUID or false.
59-
func (uuid UUID) Domain() (Domain, bool) {
60-
if v, _ := uuid.Version(); v != 2 {
61-
return 0, false
62-
}
63-
return Domain(uuid[9]), true
58+
// Domain returns the domain for a Version 2 UUID. Domains are only defined
59+
// for Version 2 UUIDs.
60+
func (uuid UUID) Domain() Domain {
61+
return Domain(uuid[9])
6462
}
6563

66-
// Id returns the id for a Version 2 UUID or false.
67-
func (uuid UUID) Id() (uint32, bool) {
68-
if v, _ := uuid.Version(); v != 2 {
69-
return 0, false
70-
}
71-
return binary.BigEndian.Uint32(uuid[0:4]), true
64+
// Id returns the id for a Version 2 UUID. Ids are only defined for Vrsion 2
65+
// UUIDs.
66+
func (uuid UUID) Id() uint32 {
67+
return binary.BigEndian.Uint32(uuid[0:4])
7268
}
7369

7470
func (d Domain) String() string {

doc.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
// Copyright 2011 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// The uuid package generates and inspects UUIDs.
5+
// Package uuid generates and inspects UUIDs.
66
//
7-
// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services.
7+
// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security
8+
// Services.
9+
//
10+
// A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to
11+
// maps or compared directly.
812
package uuid

hash.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2011 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -10,13 +10,13 @@ import (
1010
"hash"
1111
)
1212

13-
// Well known Name Space IDs and UUIDs
13+
// Well known namespace IDs and UUIDs
1414
var (
15-
NameSpace_DNS = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
16-
NameSpace_URL = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
17-
NameSpace_OID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
18-
NameSpace_X500 = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
19-
NIL = Parse("00000000-0000-0000-0000-000000000000")
15+
NameSpace_DNS = MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
16+
NameSpace_URL = MustParse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
17+
NameSpace_OID = MustParse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
18+
NameSpace_X500 = MustParse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
19+
NIL UUID // empty UUID, all zeros
2020
)
2121

2222
// NewHash returns a new UUID derived from the hash of space concatenated with
@@ -26,26 +26,26 @@ var (
2626
// NewMD5 and NewSHA1.
2727
func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
2828
h.Reset()
29-
h.Write(space)
29+
h.Write(space[:])
3030
h.Write([]byte(data))
3131
s := h.Sum(nil)
32-
uuid := make([]byte, 16)
33-
copy(uuid, s)
32+
var uuid UUID
33+
copy(uuid[:], s)
3434
uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
3535
uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant
3636
return uuid
3737
}
3838

3939
// NewMD5 returns a new MD5 (Version 3) UUID based on the
40-
// supplied name space and data.
40+
// supplied name space and data. It is the same as calling:
4141
//
4242
// NewHash(md5.New(), space, data, 3)
4343
func NewMD5(space UUID, data []byte) UUID {
4444
return NewHash(md5.New(), space, data, 3)
4545
}
4646

4747
// NewSHA1 returns a new SHA1 (Version 5) UUID based on the
48-
// supplied name space and data.
48+
// supplied name space and data. It is the same as calling:
4949
//
5050
// NewHash(sha1.New(), space, data, 5)
5151
func NewSHA1(space UUID, data []byte) UUID {

json_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
)
1212

13-
var testUUID = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
13+
var testUUID = MustParse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
1414

1515
func TestJSON(t *testing.T) {
1616
type S struct {
@@ -35,9 +35,10 @@ func BenchmarkUUID_MarshalJSON(b *testing.B) {
3535
x := &struct {
3636
UUID UUID `json:"uuid"`
3737
}{}
38-
x.UUID = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
39-
if x.UUID == nil {
40-
b.Fatal("invalid uuid")
38+
var err error
39+
x.UUID, err = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
40+
if err != nil {
41+
b.Fatal(err)
4142
}
4243
for i := 0; i < b.N; i++ {
4344
js, err := json.Marshal(x)

marshal.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import (
8+
"fmt"
9+
"unsafe"
10+
)
11+
12+
// MarshalText implements encoding.TextMarshaler.
13+
func (u UUID) MarshalText() ([]byte, error) {
14+
var js [36]byte
15+
encodeHex(js[:], u)
16+
return js[:], nil
17+
}
18+
19+
// UnmarshalText implements encoding.TextUnmarshaler.
20+
func (u *UUID) UnmarshalText(data []byte) error {
21+
// See comment in ParseBytes why we do this.
22+
// id, err := ParseBytes(data)
23+
id, err := Parse(*(*string)(unsafe.Pointer(&data)))
24+
if err == nil {
25+
*u = id
26+
}
27+
return err
28+
}
29+
30+
// MarshalBinary implements encoding.BinaryMarshaler.
31+
func (u UUID) MarshalBinary() ([]byte, error) {
32+
return u[:], nil
33+
}
34+
35+
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
36+
func (u *UUID) UnmarshalBinary(data []byte) error {
37+
if len(data) != 16 {
38+
return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
39+
}
40+
copy(u[:], data)
41+
return nil
42+
}

node.go

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2011 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -13,7 +13,8 @@ var (
1313
nodeMu sync.Mutex
1414
interfaces []net.Interface // cached list of interfaces
1515
ifname string // name of interface being used
16-
nodeID []byte // hardware for version 1 UUIDs
16+
nodeID [6]byte // hardware for version 1 UUIDs
17+
zeroID [6]byte // nodeID with only 0's
1718
)
1819

1920
// NodeInterface returns the name of the interface from which the NodeID was
@@ -48,21 +49,17 @@ func setNodeInterface(name string) bool {
4849

4950
for _, ifs := range interfaces {
5051
if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
51-
if setNodeID(ifs.HardwareAddr) {
52-
ifname = ifs.Name
53-
return true
54-
}
52+
copy(nodeID[:], ifs.HardwareAddr)
53+
ifname = ifs.Name
54+
return true
5555
}
5656
}
5757

5858
// We found no interfaces with a valid hardware address. If name
5959
// does not specify a specific interface generate a random Node ID
6060
// (section 4.1.6)
6161
if name == "" {
62-
if nodeID == nil {
63-
nodeID = make([]byte, 6)
64-
}
65-
randomBits(nodeID)
62+
randomBits(nodeID[:])
6663
return true
6764
}
6865
return false
@@ -73,35 +70,24 @@ func setNodeInterface(name string) bool {
7370
func NodeID() []byte {
7471
defer nodeMu.Unlock()
7572
nodeMu.Lock()
76-
if nodeID == nil {
73+
if nodeID == zeroID {
7774
setNodeInterface("")
7875
}
79-
nid := make([]byte, 6)
80-
copy(nid, nodeID)
81-
return nid
76+
nid := nodeID
77+
return nid[:]
8278
}
8379

8480
// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
8581
// of id are used. If id is less than 6 bytes then false is returned and the
8682
// Node ID is not set.
8783
func SetNodeID(id []byte) bool {
88-
defer nodeMu.Unlock()
89-
nodeMu.Lock()
90-
if setNodeID(id) {
91-
ifname = "user"
92-
return true
93-
}
94-
return false
95-
}
96-
97-
func setNodeID(id []byte) bool {
9884
if len(id) < 6 {
9985
return false
10086
}
101-
if nodeID == nil {
102-
nodeID = make([]byte, 6)
103-
}
104-
copy(nodeID, id)
87+
defer nodeMu.Unlock()
88+
nodeMu.Lock()
89+
copy(nodeID[:], id)
90+
ifname = "user"
10591
return true
10692
}
10793

@@ -111,7 +97,7 @@ func (uuid UUID) NodeID() []byte {
11197
if len(uuid) != 16 {
11298
return nil
11399
}
114-
node := make([]byte, 6)
115-
copy(node, uuid[10:])
116-
return node
100+
var node [6]byte
101+
copy(node[:], uuid[10:])
102+
return node[:]
117103
}

seq_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 Google Inc. All rights reserved.
1+
// Copyright 2016 Google Inc. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -42,7 +42,7 @@ func TestClockSeqRace(t *testing.T) {
4242
select {
4343
case <-done:
4444
return
45-
case ch <- NewUUID():
45+
case ch <- MustNewUUID():
4646
}
4747
}
4848
}()

0 commit comments

Comments
 (0)