0

尝试以消化函子形式解析内置类型没有问题,例如,我有一个引用国家 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 字段。 在此处输入图像描述

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

4

1 回答 1

1

After some help from dmwit Freenode #haskell, the following will solve the problem:

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

I think the confusion came from the fact that "Key Country" type (a newtype) cannot be "read" directly from an integer...

于 2015-04-09T03:45:05.753 回答