8000 ulid: add MakeFromTime helper function by craigpastro · Pull Request #87 · oklog/ulid · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ulid: add MakeFromTime helper function #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func MustNew(ms uint64, entropy io.Reader) ULID {
return id
}

// MustNewDefault is a convenience function equivalent to MustNew with
// DefaultEntropy as the entropy. It may panic if the given time.Time is too
// large or too small.
func MustNewDefault(t time.Time) ULID {
return MustNew(Timestamp(t), DefaultEntropy())
}

var (
entropy io.Reader
entropyOnce sync.Once
Expand Down
24 changes: 24 additions & 0 deletions ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ func TestMustNew(t *testing.T) {
})
}

func TestMustNewDefault(t *testing.T) {
t.Parallel()

t.Run("ULID", func(t *testing.T) {
id := ulid.MustNewDefault(time.Now())
rt, err := ulid.Parse(id.String())
if err != nil {
t.Fatalf("parse %q: %v", id.String(), err)
}
if id != rt {
t.Fatalf("%q != %q", id.String(), rt.String())
}
})

t.Run("Panic", func(t *testing.T) {
defer func() {
if got, want := recover(), ulid.ErrBigTime; got != want {
t.Errorf("got panic %v, want %v", got, want)
}
}()
_ = ulid.MustNewDefault(time.Time{})
})
}

func TestMustParse(t *testing.T) {
t.Parallel()

Expand Down
0