oklog/ulid

Don't have MarshalText() return error

Closed this issue · 5 comments

Right now, you can use String() to get the string representation of the ID. However, if you don't want the overhead of casting the byte slice into a string, you have to use MarshalText(), which returns an error, that – as far as I can see – will always be nil, since the buffer size cannot be changed by the user and will always be set correctly.

For a better user experience, I would propose either making this change:

- func (id ULID) MarshalText() ([]byte, error)
+ func (id ULID) MarshalText() []byte

or to add a new Bytes() method, equal to String().

The MarshalText method is designed to implement the encoding.TextMarshaler interface, which dictates that method signature.

Ah yes, of course. What about adding Bytes() to the API? Similar to KSUID.

No objection there. @tsenart?

As per the KSUID, Bytes() would not return the bytes corresponding to the text representation of the ULID, it would return the in-memory byte representation. That's not what you want and making it return the text representation is counter intuitive IMO.

So I suggest we do nothing further here. Your ulid is just one bs, _ := id.MarshalText() away.