我经常有一个具有“也许没什么 someFunc”模式的代码:
instance FromJSON SaveSection where
parseJSON (Object o) =
SaveSection <$>
o .:? "eventId" <*>
(maybe Nothing parseUSDate <$> o .:? "eventDate") <*>
o .:? "eventRecId" <*>
o .:? "idxId" <*>
(maybe Nothing parseUSDate <$> o .:? "idxDate") <*>
o .:? "idxRecId"
这里parseUSDate
有类型Text -> Maybe Date
。
Aeson 解析显然返回Maybe Text
。
所以在我看来,我需要通过Maybe
这里的 2 层。而且我不知道如何以任何其他方式做到这一点,除了maybe Nothing someFunc
模式。
我是否缺少一些明显的“扁平化”或我可以在这里使用的任何功能?
编辑:感谢 Alexey 的回答。
这正是我想要的。这是最终结果:
instance FromJSON SaveSection where
parseJSON (Object o) =
SaveSection <$>
o .:? "eventId" <*>
((>>= parseUSDate) <$> o .:? "eventDate") <*>
o .:? "eventRecId" <*>
o .:? "idxId" <*>
((>>= parseUSDate) <$> o .:? "idxDate") <*>
o .:? "idxRecId"