[英]How can I parse custom type in a digestive-functors form?
我可以嘗試以消化功能形式解析內置類型沒有問題,例如,我有一個客戶端類型(由持久性庫生成),該客戶端類型引用了國家(主要國家/地區)類型的ID:
Client
:: String
-> String
-> String
-> Database.Persist.Class.Key Country
-> Maybe Int
-> Client
然后定義一個clientForm值:
clientForm :: Monad m => Form String m Client
clientForm = Client <$> "firstName" .: string Nothing
<*> "lastName" .: string Nothing
<*> "address" .: string Nothing
<*> "country" .: stringRead "Cannot parse country" Nothing
<*> "age" .: optionalStringRead "Cannot parse age" Nothing
奇怪的是,clientForm在提交(POST)時無法解析國家/地區ID字段。
使用“ stringRead”解析“ Key Country”類型(可以從“ toSqlKey int64”獲得)是錯誤的嗎?
在dmwit Freenode #haskell尋求幫助之后,以下將解決該問題:
clientForm :: Monad m => Form String m Client
clientForm = Client <$> "firstName" .: string Nothing
<*> "lastName" .: string Nothing
<*> "address" .: string Nothing
<*> (toSqlKey <$> "country" .: stringRead "Cannot parse country id." Nothing)
<*> "age" .: optionalStringRead "Cannot parse age" Nothing
我認為造成這種混淆的原因是不能直接從整數“讀取”“ Key Country”類型(新類型)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.