如何在消化函数形式中解析自定义类型?

时间:2015-04-08 15:54:30

标签: haskell digestive-functors

我尝试在消化函数形式中解析内置类型没有问题,例如我有一个客户类型(由持久库生成)引用国家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)时,无法解析country id字段。 enter image description here

使用“stringRead”解析“Key Country”类型(可以从“toSqlKey int64”获取)是错误的吗?

1 个答案:

答案 0 :(得分:1)

在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

我认为混淆来自&#34; Key Country&#34;类型(新类型)不能&#34;读&#34;直接从整数...