Reputation: 2489
In my application, my data model has several different instances of using an Integer or a String for some identifier. For safety, I've gone ahead and wrapped those identifiers into newtype declarations like so:
newtype DocId = DocId Integer
newtype GroupName = GroupName String
newtype UserName = UserName String
When I'm setting up my Yesod paths, I'm discovering that I have to create at least three instances for each of these, and the instances are almost always identical
instance Read DocId where
readsPrec prec val = case reads val of
(i, ""):_ -> [(DocId i, "")]
[] -> []
instance B.ToMarkup DocId where
toMarkup (DocId val) = B.toMarkup val
instance PathPiece DocId where
toPathPiece (DocId i) = T.pack $ show i
fromPathPiece s =
case reads $ T.unpack s of
(i, ""):_ -> Just i
[] -> Nothing
This text, over and over again.
What do I really need to set up in order to both render my data type in URLs (like @{ViewDocument docId}) and be able to parse those URLs?
Upvotes: 2
Views: 277
Reputation: 31315
If you turn on GeneralizedNewtypeDeriving
, then you can just add deriving PathPiece
under each new datatype, or deriving instance PathPiece DocId
if you can't derive directly on the datatype.
You will need Read, Show, and PathPiece instances for every datatype that is to be part of a route.
Upvotes: 4
Reputation: 2489
Based on some trial and error (i.e., removing features and seeing what breaks) it looks like in order for a data type to be part of a Yesod path, it needs three typeclasses to be defined:
My ToMarkup declaration above applies to how I display my data types in the Html (Hamlet, Blaze, whatever, but in my case Hamlet). It is therefore not necessary if I'm only going to put the data type into a URL.
Upvotes: 0