【发布时间】:2015-08-08 22:36:15
【问题描述】:
我有一个使用 Persistent 进行存储的 Snap 应用程序,我正在尝试为 Persistent 中定义的数据类型生成类型安全的路由。我正在使用snap-web-routes package:。
我有以下创建Group 和GroupId 数据类型的模板Haskell 函数:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Group
name T.Text
deriving Show
|]
在我的Application.hs 我有:
data AppUrl = AddLink GroupId deriving (Eq, Show, Read, Generic)
文档建议:
instance PathInfo AppUrl
考虑到上面的Generic 推导,我需要做的只是这件事
No instance for (PathInfo (KeyBackend SqlBackend Group))
arising from a use of ‘Web.Routes.PathInfo.$gdmtoPathSegments’
我的假设是这个错误表明 Haskell 不知道如何使用 Persistent 的数据类型自动创建实例定义。
我的下一个尝试是手动定义实例:
instance PathInfo AppUrl where
toPathSegments (AddLink groupId) = "add-link" : toPathPiece groupId : []
fromPathSegments (x:y:[]) = ????
我似乎无法弄清楚如何构造 GroupId 数据类型。
从Yesod的优秀Persistent tutorial我知道数据类型定义为:
type GroupId = Key Group
newtype Key Group = GroupKey (BackendKey SqlBackend)
但后来我遇到了一个问题,因为BackendKey 没有公开,所以我无法导入它并创建自己的实例。我似乎找不到在 Persistent 中创建这种数据类型的公共 API。
【问题讨论】:
标签: haskell persistent haskell-snap-framework