我有以下代码片段,用于读取由镜头 (
lens-aeson
) 表达式指定的值:
import Control.Lens ((^?))
import Data.Aeson (Value)
import Data.Aeson.Key (fromString)
import Data.Aeson.Lens (key)
import Data.Text (pack)
-- >>> readValue
-- Just (String "value")
readValue :: Maybe Value
readValue = json ^? path
where
path = key (fromString "key") . key (fromString "of") . key (fromString "interest")
json = pack "{\"key\": {\"of\": {\"interest\": \"value\"}}}"
现在我想通过将列表
path
传递到 ["key","of","interest"]
来从配置动态构建 readValue
表达式。
如何从列表中动态创建
path
?
我猜是这样的:
readValue = json ^? _Value.path
where
path = foldr (.) id (key . fromString <$> ["key", "of", "interest"])
json = pack "{\"key\": {\"of\": {\"interest\": \"value\"}}}"