What typeclasses need to be defined for a Yesod path?

左心房为你撑大大i 提交于 2019-12-06 11:16:02

问题


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?


回答1:


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.




回答2:


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:

  • Read
  • Show
  • PathPiece

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.



来源:https://stackoverflow.com/questions/20751415/what-typeclasses-need-to-be-defined-for-a-yesod-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!