如何动态构建镜头表情?

问题描述 投票:0回答:1

我有以下代码片段,用于读取由镜头 (

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

haskell haskell-lens aeson
1个回答
0
投票

我猜是这样的:

readValue = json ^? _Value.path
  where
    path = foldr (.) id (key . fromString <$> ["key", "of", "interest"])
    json = pack "{\"key\": {\"of\": {\"interest\": \"value\"}}}"
© www.soinside.com 2019 - 2024. All rights reserved.