Skip to content

Commit

Permalink
Merge pull request #48 from pborman/google
Browse files Browse the repository at this point in the history
Leverage github.com/google/uuid where possible
  • Loading branch information
pborman authored Sep 6, 2018
2 parents 4c1ecd6 + de0d1a4 commit adf5a74
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 441 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: go

go:
- 1.9
- "1.9"
- "1.10"
- "1.11"
- tip

script:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ This project was automatically exported from code.google.com/p/go-uuid
# uuid ![build status](https://travis-ci.org/pborman/uuid.svg?branch=master)
The uuid package generates and inspects UUIDs based on [RFC 4122](http://tools.ietf.org/html/rfc4122) and DCE 1.1: Authentication and Security Services.

This package now leverages the github.com/google/uuid package (which is based off an earlier version of this package).

###### Install
`go get github.com/pborman/uuid`

Expand Down
7 changes: 6 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

// The uuid package generates and inspects UUIDs.
//
// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services.
// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security
// Services.
//
// This package is a partial wrapper around the github.com/google/uuid package.
// This package represents a UUID as []byte while github.com/google/uuid
// represents a UUID as [16]byte.
package uuid
10 changes: 6 additions & 4 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package uuid
import (
"errors"
"fmt"

guuid "github.com/google/uuid"
)

// MarshalText implements encoding.TextMarshaler.
Expand Down Expand Up @@ -60,11 +62,11 @@ func (u Array) MarshalText() ([]byte, error) {

// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Array) UnmarshalText(data []byte) error {
id := Parse(string(data))
if id == nil {
return errors.New("invalid UUID")
id, err := guuid.ParseBytes(data)
if err != nil {
return err
}
*u = id.Array()
*u = Array(id)
return nil
}

Expand Down
68 changes: 5 additions & 63 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
package uuid

import (
"sync"
)

var (
nodeMu sync.Mutex
ifname string // name of interface being used
nodeID []byte // hardware for version 1 UUIDs
guuid "github.com/google/uuid"
)

// NodeInterface returns the name of the interface from which the NodeID was
// derived. The interface "user" is returned if the NodeID was set by
// SetNodeID.
func NodeInterface() string {
defer nodeMu.Unlock()
nodeMu.Lock()
return ifname
return guuid.NodeInterface()
}

// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
Expand All @@ -30,70 +22,20 @@ func NodeInterface() string {
//
// SetNodeInterface never fails when name is "".
func SetNodeInterface(name string) bool {
defer nodeMu.Unlock()
nodeMu.Lock()
if nodeID != nil {
return true
}
return setNodeInterface(name)
}

func setNodeInterface(name string) bool {

iname, addr := getHardwareInterface(name) // null implementation for js
if iname != "" && setNodeID(addr) {
ifname = iname
return true
}

// We found no interfaces with a valid hardware address. If name
// does not specify a specific interface generate a random Node ID
// (section 4.1.6)
if name == "" {
if nodeID == nil {
nodeID = make([]byte, 6)
}
randomBits(nodeID)
return true
}
return false
return guuid.SetNodeInterface(name)
}

// NodeID returns a slice of a copy of the current Node ID, setting the Node ID
// if not already set.
func NodeID() []byte {
defer nodeMu.Unlock()
nodeMu.Lock()
if nodeID == nil {
setNodeInterface("")
}
nid := make([]byte, 6)
copy(nid, nodeID)
return nid
return guuid.NodeID()
}

// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
// of id are used. If id is less than 6 bytes then false is returned and the
// Node ID is not set.
func SetNodeID(id []byte) bool {
defer nodeMu.Unlock()
nodeMu.Lock()
if setNodeID(id) {
ifname = "user"
return true
}
return false
}

func setNodeID(id []byte) bool {
if len(id) < 6 {
return false
}
if nodeID == nil {
nodeID = make([]byte, 6)
}
copy(nodeID, id)
return true
return guuid.SetNodeID(id)
}

// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
Expand Down
12 changes: 0 additions & 12 deletions node_js.go

This file was deleted.

36 changes: 0 additions & 36 deletions node_net.go

This file was deleted.

87 changes: 6 additions & 81 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,18 @@ package uuid

import (
"encoding/binary"
"sync"
"time"

guuid "github.com/google/uuid"
)

// A Time represents a time as the number of 100's of nanoseconds since 15 Oct
// 1582.
type Time int64

const (
lillian = 2299160 // Julian day of 15 Oct 1582
unix = 2440587 // Julian day of 1 Jan 1970
epoch = unix - lillian // Days between epochs
g1582 = epoch * 86400 // seconds between epochs
g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs
)

var (
timeMu sync.Mutex
lasttime uint64 // last time we returned
clock_seq uint16 // clock sequence for this run

timeNow = time.Now // for testing
)

// UnixTime converts t the number of seconds and nanoseconds using the Unix
// epoch of 1 Jan 1970.
func (t Time) UnixTime() (sec, nsec int64) {
sec = int64(t - g1582ns100)
nsec = (sec % 10000000) * 100
sec /= 10000000
return sec, nsec
}
type Time = guuid.Time

// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and
// clock sequence as well as adjusting the clock sequence as needed. An error
// is returned if the current time cannot be determined.
func GetTime() (Time, uint16, error) {
defer timeMu.Unlock()
timeMu.Lock()
return getTime()
}

func getTime() (Time, uint16, error) {
t := timeNow()

// If we don't have a clock sequence already, set one.
if clock_seq == 0 {
setClockSequence(-1)
}
now := uint64(t.UnixNano()/100) + g1582ns100

// If time has gone backwards with this clock sequence then we
// increment the clock sequence
if now <= lasttime {
clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000
}
lasttime = now
return Time(now), clock_seq, nil
}
func GetTime() (Time, uint16, error) { return guuid.GetTime() }

// ClockSequence returns the current clock sequence, generating one if not
// already set. The clock sequence is only used for Version 1 UUIDs.
Expand All @@ -74,39 +27,11 @@ func getTime() (Time, uint16, error) {
// clock sequence is generated the first time a clock sequence is requested by
// ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated
// for
func ClockSequence() int {
defer timeMu.Unlock()
timeMu.Lock()
return clockSequence()
}

func clockSequence() int {
if clock_seq == 0 {
setClockSequence(-1)
}
return int(clock_seq & 0x3fff)
}
func ClockSequence() int { return guuid.ClockSequence() }

// SetClockSeq sets the clock sequence to the lower 14 bits of seq. Setting to
// -1 causes a new sequence to be generated.
func SetClockSequence(seq int) {
defer timeMu.Unlock()
timeMu.Lock()
setClockSequence(seq)
}

func setClockSequence(seq int) {
if seq == -1 {
var b [2]byte
randomBits(b[:]) // clock sequence
seq = int(b[0])<<8 | int(b[1])
}
old_seq := clock_seq
clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant
if old_seq != clock_seq {
lasttime = 0
}
}
func SetClockSequence(seq int) { guuid.SetClockSequence(seq) }

// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in
// uuid. It returns false if uuid is not valid. The time is only well defined
Expand Down
11 changes: 0 additions & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@

package uuid

import (
"io"
)

// randomBits completely fills slice b with random data.
func randomBits(b []byte) {
if _, err := io.ReadFull(rander, b); err != nil {
panic(err.Error()) // rand should never fail
}
}

// xvalues returns the value of a byte as a hexadecimal digit or 255.
var xvalues = [256]byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
Expand Down
Loading

0 comments on commit adf5a74

Please sign in to comment.