Skip to content

Commit

Permalink
Merge pull request #25 from 88labs/feat/uild
Browse files Browse the repository at this point in the history
feat: add goroutine safe ulid library
  • Loading branch information
tomtwinkle authored Aug 25, 2022
2 parents 8db2b04 + a6dfd98 commit 57c1ad5
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ulid/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/88labs/go-utils/ulid

go 1.18

require github.com/oklog/ulid/v2 v2.1.0
3 changes: 3 additions & 0 deletions ulid/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
70 changes: 70 additions & 0 deletions ulid/ulid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ulid

import (
"crypto/rand"
"errors"
"io"
"sync"

oklogulid "github.com/oklog/ulid/v2"
)

type ULID oklogulid.ULID

var (
pool = sync.Pool{
New: func() interface{} { return oklogulid.Monotonic(rand.Reader, 0) },
}
zeroValueULID oklogulid.ULID
ErrULIDZero = errors.New("ulid is zero")
)

func New() (ULID, error) {
var entropy = rand.Reader
if e, ok := pool.Get().(io.Reader); ok {
entropy = e
defer pool.Put(e)
}
id, err := oklogulid.New(oklogulid.Now(), entropy)
if err != nil {
return ULID{}, err
}
return ULID(id), nil
}

func MustNew() ULID {
id, err := New()
if err != nil {
panic(err)
}
return id
}

func MustParse(s string) ULID {
u := ULID(oklogulid.MustParseStrict(s))
if u.IsZero() {
panic(ErrULIDZero)
}
return u
}

func Parse(s string) (ULID, error) {
oklogULID, err := oklogulid.ParseStrict(s)
u := ULID(oklogULID)
if err != nil {
return u, err
}
if u.IsZero() {
return u, ErrULIDZero
}

return u, nil
}

func (u ULID) String() string {
return oklogulid.ULID(u).String()
}

func (u ULID) IsZero() bool {
return oklogulid.ULID(u) == zeroValueULID
}
62 changes: 62 additions & 0 deletions ulid/ulid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ulid_test

import (
"fmt"
"strings"
"sync"
"testing"

"github.com/88labs/go-utils/ulid"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
input string
err string
}{
{"zero", "00000000000000000000000000", "ulid is zero"},
{"ok", "0000XSNJG0MQJHBF4QX1EFD6Y3", ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id, err := ulid.Parse(tt.input)
if len(tt.err) == 0 {
if err != nil {
t.Fatalf("want no err, but has err %v", err)
}
if id.IsZero() {
t.Fatal("ulid is zero")
}
}

if len(tt.err) > 0 {
if err == nil {
t.Fatalf("want %v, but %v", tt.err, err)
}
if !strings.Contains(err.Error(), tt.err) {
t.Fatalf("want %v, but %v", tt.err, err)
}
}
})
}
}

func TestMustNew(t *testing.T) {
t.Run("ulidnew", func(t *testing.T) {
var wg sync.WaitGroup
num := 100000
wg.Add(num)
for i := 0; i < num; i++ {
go func() {
defer wg.Done()
id := ulid.MustNew()
if id.IsZero() {
panic(fmt.Sprintf("zero: %v", id))
}
}()
}
wg.Wait()
})
}

0 comments on commit 57c1ad5

Please sign in to comment.