looplab/eventhorizon

Use ksuid instead of google uuid?

MartinForReal opened this issue · 10 comments

As described in https://segment.com/blog/a-brief-history-of-the-uuid/ id generated by ksuid is sortable by timestamp.

the github repo is https://github.com/segmentio/ksuid

Do you think it is feasible to use ksuid instead?

This is definitely interesting but as we just switched to Googles UUID lib we have to wait a bit. Will leave this open for comments in the meantime.

Switching between UUID libs / ULID (https://github.com/oklog/ulid) / KSUID (https://github.com/segmentio/ksuid) is not a best options. Because EH still locking us with a specific lib / technology. We should find a way that allow to use any lib we want or just raw string as Entity's ID.

Yes! That would be awesome. There has been some experiments with a ID interface in the past. Any ideas for how to approach this is welcome!

Hard things are:

  • How to compare two ID (same type or not)?
  • How to marshal/unmarshal to json/database?
type ID interface {
	Equal(other ID) bool
	IsNil() bool
	String() string
}

I have done some more experimenting with a ID interface. It went really far, but unfortunately the encoding/json package has no way of decoding into an interface properly. For BSON it worked out OK, similarly to how the current encoder/decoder is registered, but for an interface instead.

I spent a lot of work on this just to realize that it's impossible to solve using the built-in JSON encoding, which most people use.

Here is the WIP if you are interested: https://github.com/maxekman/eventhorizon/tree/ISSUE-28/id-interface

One more. I also burnt myself on this one.
Perhaps it is possible to do it with an object instance and delegation (strategy pattern) rather than a interface. Not like I am telling anyone go ahead with it.

Tricky one! If you can provide an example I might be able to give it a go.

The best solution would be to have generics for this!

My idea is a ID structure of the type byte[16] containing the actual id, that have the necessary methods for supporting the native json marshal/unmarshal. The ID delegate marshal/unmarshal to a strategy class that make use of the actual id implementation (oklog/ulid or google). It is not as universal s an interface, but I think it can work.
I had a start at this. I will try to finish.

Will close this for now. I have added back the local UUID package, but this time as an alias for Googles UUID type. That means that anyone who wants to use another ID type can fork this project and keep a commit at the tip where they switch out the ID type alias.