eventhorizon icon indicating copy to clipboard operation
eventhorizon copied to clipboard

[api] Interface for ID instead of type

Open rodcorsi opened this issue 7 years ago • 5 comments

Hi ! Today we use snowflake as UUID, stored as uint64 and transmitted as string in base64. Do you think is possible to change UUID to an interface? This way we'll be able to implement our custom UUID. Please let me know what do you think, if you want I can help with this change

rodcorsi avatar Oct 25 '16 14:10 rodcorsi

That would be awesome! I have been thinking about getting rid of the UUID type as it is not core to the package. Do you have any idea of a good implementation that can handle marshaling and unmarshaling into the current and future DBs and messaging systems?

maxekman avatar Oct 25 '16 14:10 maxekman

If I understand you right, I think we need to implement all marshaller interfaces:

// UUID is a unique identifier, based on the UUID spec.
type UUID interface {
    String() string

    // JSON conversion
    MarshalJSON() ([]byte, error)
    UnmarshalJSON(data []byte) error

    // Mongo BSON conversion
    GetBSON() (interface{}, error)
    SetBSON(raw bson.Raw) error

    // SQL conversion
    Value() (driver.Value, error)
    Scan(value interface{}) error
}

rodcorsi avatar Oct 25 '16 16:10 rodcorsi

It would be best to avoid an interface that big and diverse if possible. I have been thinking a bit about this and I can imagine a really simple interface for the ID (maybe with only a Equals(ID) or something) together with a factory to create different type of IDs. In that case you would set the type of ID used by the factory in each project in an init function. Each implementing ID type should be clear about what type of marshaling/unmarshaling it can handle. What do you think about an approach like that?

maxekman avatar Oct 25 '16 18:10 maxekman

I think this is the better approach! I'll make some tests and after that, I'll submit a PR for your analysis 👍

rodcorsi avatar Oct 25 '16 19:10 rodcorsi

Sounds good!

maxekman avatar Oct 25 '16 19:10 maxekman