[英]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.