Search code examples
haskellyesod

What typeclasses need to be defined for a Yesod path?


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?


Solution

  • 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.