segmentio / ksuid

K-Sortable Globally Unique IDs
https://segment.com/blog/a-brief-history-of-the-uuid/
MIT License
4.96k stars 177 forks source link

Added OrNil functions #67

Closed v1def closed 2 years ago

v1def commented 2 years ago

@achille-roussel Hello, sorry to disturb you. Maybe you didn't see this pull request?

v1def commented 2 years ago

Ease of use, there are cases where you are sure that your ksuid is correct. But you need to get from bytes or string, example below.

func getPosts(before, after []byte) {
    b, err := ksuid.FromBytes(before)
    if err != nil {
        panic(err)
    }

    a, err := ksuid.FromBytes(after)
    if err != nil {
        panic(err)
    }

    sortOptions := SortOptions{Before: b, After:  a}
}

Instead, you can do it like this:

func getPosts(before, after []byte) {
    sortOptions := SortOptions{
        Before: ksuid.FromBytesOrNil(before),
        After:  ksuid.FromBytesOrNil(after),
    }
}

KSUID already has something similar, ksuid.New(), only this function panics on error. OrNil functions are also used in many other packages (uuid, ulid).

achille-roussel commented 2 years ago

Thanks for sharing context!

Do you mind adding a section to the README with these examples to showcase how to use the new functions? Along with links to the other libraries you mentioned would be amazing as well!

v1def commented 2 years ago

Yes, thank you!