Skip to content

Commit

Permalink
refactor: fix comments, export constants (#15) (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
robojones committed Aug 26, 2019
1 parent ef30a91 commit b91731b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions iid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ import (
)

const (
iidLen = 8
strLen = 11
// IidLen is the length of the byte slice used for the Iid type.
IidLen = 8
// StrLen is the length of iid string representations in bytes.
StrLen = 11
// randIndex is the index of the first byte which is initialized with random values during the creation of iids.
// All following bytes are also initialized with random values.
randIndex = 4
// encoder is the base64 URL encoding used for the string serialization of iids.
encoder = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
// postfix is the character which needs to be appended to iid string
// representations so they can be parsed by the encoding in Enc.
postfix = "="
)

var enc = base64.NewEncoding(encoder)
// The encoding used for string serialization and deserialization of iids.
var Enc = base64.NewEncoding(encoder)

// RandReader is used to fill the 31 least significant bits of new iids with random values.
var RandReader = rand.Reader

// Timestamp is used to get the current UNIX time in seconds when generating new iids.
var Timestamp = func() uint32 {
return uint32(time.Now().Unix())
}
Expand All @@ -30,24 +40,23 @@ var Timestamp = func() uint32 {
// 32 bit current timestamp,
// 31 bit cryptographically secure random bits.
func New() Iid {
b := make([]byte, iidLen)
b := make([]byte, IidLen)

// Set the last four bytes to random values.
if _, err := RandReader.Read(b[randIndex:]); err != nil {
panic(errors.Wrap(err, "generate new iid"))
}

// Write current UNIX time in ms to the first four bytes.
writeTime(b, Timestamp())

return Iid(b)
}

// FromString imports an existing Iid from its base64url encoded string representation.
func FromString(s string) (Iid, error) {
b, err := enc.DecodeString(s + postfix)
b, err := Enc.DecodeString(s + postfix)

if err != nil || len(b) != iidLen {
if err != nil || len(b) != IidLen {
return nil, errors.Wrapf(ErrInvalid, "parse id %s", s)
}

Expand All @@ -56,7 +65,7 @@ func FromString(s string) (Iid, error) {

// FromUint64 imports an existing Iid from its uint64 representation.
func FromUint64(i uint64) (Iid) {
b := make([]byte, iidLen)
b := make([]byte, IidLen)

binary.BigEndian.PutUint64(b, i)

Expand All @@ -73,7 +82,7 @@ type Iid []byte

// String returns an 11 byte long, base64url encoded string representing the Iid.
func (i Iid) String() string {
return enc.EncodeToString(i)[:strLen]
return Enc.EncodeToString(i)[:StrLen]
}

// Uint64 returns a uint64 representing the Iid.
Expand Down
2 changes: 1 addition & 1 deletion iid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExampleNew() {

func TestFromString(t *testing.T) {
ex := New()
s := enc.EncodeToString(ex)[:strLen]
s := Enc.EncodeToString(ex)[:StrLen]

id, err := FromString(s)

Expand Down

0 comments on commit b91731b

Please sign in to comment.