nikita-volkov / hasql-implicits

Implicit definitions for Hasql, such as default codecs for standard types
http://hackage.haskell.org/package/hasql-implicits
MIT License
2 stars 2 forks source link

Decoders? #7

Open mikeplus64 opened 3 days ago

mikeplus64 commented 3 days ago

Would be nice to have some implicit decoders as well.

Something like

class DefaultValueDecoder a where
  defaultValue :: Value a

Rows, if they are to be added, are probably calling for something like product-profunctors where the idea is to promote a constructor [or anything like sqlType -> hsType] to a parser for that constructor. Or using Generic is quite doable though inevitably annoying when a new GHC version breaks it. Or rolling your own minimal version of profunctors isn't so bad:

class DefaultRowDecoder a where
  defaultRow :: D.Row a

-- Maybe ==> nullable
-- Non-maybe ==> nonNullable

liftDefaultRow :: (LiftDefaultRowDecoder a f) => f -> D.Row a
liftDefaultRow f = defaultRowP (pure f)

class LiftDefaultRowDecoder a f where
  defaultRowP :: D.Row f -> D.Row a -- dunno what's a good name here

instance (DefaultRowDecoder x, LiftDefaultRowDecoder r' r) => LiftDefaultRowDecoder r' (x -> r) where
  defaultRowP r = defaultRowP (r <*> defaultRow)

instance LiftDefaultRowDecoder r r where
  defaultRowP r = r

data MyNiceRowResult = MyNiceRowResult { a, b, c, d :: Text }

someRowDecoder :: Row MyNiceRowResult -- Requires annotation or type application to solve instances
someRowDecoder = liftDefaultRow MyNiceRowResult
nikita-volkov commented 2 days ago

This all looks interesting and may be a good basis for your own library. You clearly have a vision there. If your lib will become the norm I'll deprecate this one and replace it with yours in hasql-dynamic-statements.

nikita-volkov commented 2 days ago

Also please beware that there also exists at least one other library in this category. There may be more.

nikita-volkov commented 2 days ago

There's also a lib for generics.