I have a data type
data Reading = Reading
{ title :: String
, authors :: Vector String
, startDate :: Day
, endDate :: Maybe Day
}
deriving (Eq, Show, Generic, FromRow, ...)
and I want to be able to create at a POST endpoint for a form
:<|> "reading" :> ReqBody '[FormUrlEncoded] Reading :> Post '[HTML] Reading
To do this, I need to derive a FromForm
instance for Reading
. The issue is that there is no instance FromHttpApiData (Vector String)
. There does seem to be an instance for [String]
. I chose Vector String
so that I could also derive FromRow
, which allows me to naturally pull records using postgresql-simple.
So, is there a type that I can use for the authors
field to derive both FromRow
and FromForm
? I did spend a fair amount of time in the docs and experimenting to no avail. Or maybe I should be looking a defining a custom instance, or parameterizing the type of authors
based on the use case?
It does indeed look like there isn't machinery for deriving the FromForm
instance for Vector
.
I would define the instance by hand:
instance FromForm Reading where
fromForm f =
Reading
<$> parseUnique "title" f
<*> (fromList <$> parseAll "authors" f)
<*> parseUnique "startDate" f
<*> parseMaybe "endDate" f