Modelling sum types #174
-
Hey @ocharles. We're experimenting with Rel8 at Artificial and enjoying it! I'd like to handle tables like this: data Task f = Pending | Complete (CompletedTask f)
data CompletedTask f = CompletedTask {
date :: f UTCTime, ...
} create table task (serial id primary key, tag varchar(32) not null, date timestamp, ...) Then have a Rel8able instance and tableSchema that inspects the How would you do this with Rel8? I feel like Rel8.Table.Either is related, but I'm not sure how. That seems to use a special field |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Related is modelling things like |
Beta Was this translation helpful? Give feedback.
-
Hey @chrisdone-artificial, cool to hear you're having a play with Rel8! This is an interesting question, and I think we've got support for this, or we're at least pretty close. For data Task f = Pending | Complete (CompletedTask f)
deriving stock Generic
deriving anyclass Rel8able
data CompletedTask f = CompletedTask
{ date :: Column f UTCTime, ...
}
deriving stock Generic
deriving anyclass Rel8able You would then use Have a play with this and maybe try seeing what the type of |
Beta Was this translation helpful? Give feedback.
-
Here's an example of how you could embed that into a {-# language DeriveAnyClass #-}
{-# language DeriveGeneric #-}
{-# language DisambiguateRecordFields #-}
{-# language OverloadedStrings #-}
{-# language TypeApplications #-}
-- base
import Data.Int ( Int64 )
import GHC.Generics ( Generic )
-- rel8
import Rel8
-- time
import Data.Time ( UTCTime )
data TaskTable f = TaskTable
{ id :: Column f Int64
, task :: HADT f Task
}
deriving (Generic, Rel8able)
data Task f = Pending | Complete (CompletedTask f)
deriving (Generic)
data CompletedTask f = CompletedTask
{ date :: Column f UTCTime
}
deriving (Generic, Rel8able)
main :: IO ()
main = putStrLn $ showQuery $ each TableSchema
{ schema = Nothing
, name = "task"
, columns = TaskTable
{ id = "id"
, task =
nameADT @Task
"tag"
CompletedTask
{ date = "date"
}
}
} |
Beta Was this translation helpful? Give feedback.
Here's an example of how you could embed that into a
Rel8able
, which is probably what you want to do: