storj / dbx

A neat codegen-based database wrapper written in Go
Apache License 2.0
24 stars 3 forks source link

support custom field types #7

Open egonelbre opened 1 year ago

egonelbre commented 1 year ago

Currently there's significant amount of code that's being written to convert from []byte to uuid.UUID, because dbx doesn't seem to allow specifying custom types.

zeebo commented 1 year ago

this would be great. an easy but janky way to do it is to require that the type not have any .s in it (because the parsing is harder) and allow any type for the field and use aliases. for example, just allow field foo uuid and add a type uuid = uuidpkg.UUID into the package somewhere.

i think the code gen needs to know how to create a zero value, but actually it could always have done *new(T) for any T like we do with generics now.

egonelbre commented 1 year ago

Maybe:

model baz (
    field id bytea ( default 50, type "storj.io/common/uuid.UUID" )
)

But, this should work as well, most of the time

model baz (
    field id bytea ( default 50, type storj.io/common/uuid.UUID )
)
egonelbre commented 1 year ago

One option would be to allow specifying custom types,e.g.

type StorjUUID (
    sqlite   text
    postgres bytea
    go       storj.io/common/uuid.UUID
)

model baz (
    field id StorjUUID ( default 50 )
)
zeebo commented 1 year ago

i think the quoted version would be easiest to support with the current parser. also, this works nicely as long as conversions are applicable, but uuid is a special case where it isn't.. more info or a convention is required for handling non-convertable types.

and your go directive in the type means this is a bit more complicated than i thought. maybe it's time to just drop hypothetical other languages as a feature because it hasn't materialized in 7 years lol.