dylex / postgresql-typed

Haskell PostgreSQL library with compile-time type inference
http://hackage.haskell.org/package/postgresql-typed
Other
83 stars 12 forks source link

Support macaddr type #25

Closed kamek-pf closed 3 years ago

kamek-pf commented 3 years ago

Hello,

Would it be hard to add support for Postgres' macaddr type ? We use it in a bunch of places, our current work around is to cast to text directly in the queries.

And thank you for this library, I like this approach to type-safe SQL a lot !

dylex commented 3 years ago

Shouldn't be too hard, though I guess the question is what Haskell type should it map to? For inet I ended up making a custom PGInet type since there was no obvious standard corresponding type, but it felt a bit messy.

This is also something you can add yourself, by the way, in your own code, if you have some custom type you want to use, just declare PGType "macaddr", PGParameter "macaddr" MacAddrType, and PGColumn "macaddr" MacAddrType instances. The only trick is that you have to write parsers for it (but you probably already have them). See https://github.com/dylex/postgresql-typed/blob/master/Database/PostgreSQL/Typed/Inet.hs for a complete example. You can also add the binary encoders/decoders if you know the internal postgresql representation and care about speed.

kamek-pf commented 3 years ago

Thanks for the pointers, I ended up implementing those instances like you suggested. I agree that there's a bunch of ways to do this, I'm personally just using a newtype wrapper around a bytestring, but I can think of several alternatives.

It's probably better to leave this to the users. Thanks for the quick response !