Skip to content

Commit

Permalink
feat: add int64 methods (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
robojones authored Jul 10, 2019
1 parent c63fa88 commit db8f530
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions iid.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func FromUint64(i uint64) (Iid) {
return Iid(b)
}

// FromInt imports an existing Iid from its int64 representation.
func FromInt(i int64) (Iid) {
return FromUint64(uint64(i))
}

// Iid represents time sortable ID which can be exported as a base64url encoded string or uint64.
type Iid []byte

Expand All @@ -72,3 +77,8 @@ func (i Iid) String() string {
func (i Iid) Uint64() uint64 {
return binary.BigEndian.Uint64(i)
}

// Int returns a int64 representing the Iid.
func (i Iid) Int() int64 {
return int64(i.Uint64())
}
26 changes: 26 additions & 0 deletions iid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ func ExampleFromUint64() {
// Output: 6711382541547442289
}

func TestFromInt(t *testing.T) {
ex := New()
i := int64(binary.BigEndian.Uint64(ex))

id := FromInt(i)

assert.Equal(t, ex, id)
}

func ExampleFromInt() {
var i int64 = 6711382541547442289
id := FromInt(i)

fmt.Println(id.Int())
// Output: 6711382541547442289
}

func TestIid_String(t *testing.T) {
ex := New()

Expand All @@ -102,3 +119,12 @@ func TestIid_Uint64(t *testing.T) {
id := FromUint64(i)
assert.Equal(t, ex, id)
}

func TestIid_Int(t *testing.T) {
ex := New()

i := ex.Int()

id := FromInt(i)
assert.Equal(t, ex, id)
}

0 comments on commit db8f530

Please sign in to comment.