- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的 HOCON 配置:
[
{
name = 1
url = "http://example.com"
},
{
name = 2
url = "http://example2.com"
},
{
name = 3
url = {
A = "http://example3.com"
B = "http://example4.com"
}
}
]
我想用 pureconfig 解析它。我如何表示 URL 可以是字符串或多个 URL 的映射,每个 URL 都有一个键?
我试过这个:
import pureconfig.ConfigSource
import pureconfig.generic.auto.exportReader
case class Site(name: Int, url: Either[String, Map[String, String]])
case class Config(sites: List[Site])
ConfigSource.default.loadOrThrow[Config]
但结果是“预期类型 OBJECT。而是找到了 STRING。”
我知道 pureconfig 支持 Option
。我没找到支持Either
的,是不是可以换成别的东西?
最佳答案
如您所见,Either
不在 types supported out of the box 列表中.
但是 Either
属于 sealed family ,所以:
@ ConfigSource.string("""{ type: left, value: "test" }""").load[Either[String, String]]
res15: ConfigReader.Result[Either[String, String]] = Right(Left("test"))
@ ConfigSource.string("""{ type: right, value: "test" }""").load[Either[String, String]]
res16: ConfigReader.Result[Either[String, String]] = Right(Right("test"))
有效。如果你有一个密封的层次结构,pureconfig 将做的是需要一个具有字段 type
的对象 - 该字段将用于将解析分派(dispatch)到特定的子类型。所有其他字段将作为字段传递以解析为该子类型。
如果这对您不起作用,您可以尝试自己实现编解码器:
// just an example
implicit def eitherReader[A: ConfigReader, B: ConfigReader] =
new ConfigReader[Either[A, B]] {
def from(cur: ConfigCursor) =
// try left, if fail try right
ConfigReader[A].from(cur).map(Left(_)) orElse ConfigReader[B].from(cur).map(Right(_))
}
现在不需要区分值:
@ ConfigSource.string("""{ test: "test" }""").load[Map[String, Either[String, String]]]
res26: ConfigReader.Result[Map[String, Either[String, String]]] = Right(Map("test" -> Left("test")))
默认情况下不提供此功能,因为您必须自己回答一些问题:
Left
还是 Right
解码?Left
后备 Right
或 Right
后备 Left
有意义吗?Either[X, X]
怎么样?如果您知道预期的行为是什么,您可以实现自己的编解码器并在派生中使用它。
关于scala - 在 pureconfig 中表示 Either,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61058354/
有没有办法以更“单子(monad)”的方式编写这个函数,而不是在 Either 上使用模式匹配? {-# LANGUAGE LambdaCase #-} calculate :: (Monad m)
我正在使用 cats ,想知道怎么用它转一个数据。 从 val data = Either[Error, Option[Either[Error, Account]]] 到 val target: E
我是 Haskell 的初学者,我正在编写一些使用 Either 的 Haskell 代码用于错误处理。 Either 的左侧元素表示错误,而右侧元素表示成功的结果。代码经常使用 Either 的 A
我有一个 Either 列表,表示错误: type ErrorType = List[String] type FailFast[A] = Either[ErrorType, A] import ca
假设我在 processOne 中有一个 monadic 函数,定义如下: def processOne(输入:输入):Either[ErrorType, Output] = ... 给定“Input
db.findUser(id).then(R.pipe( R.ifElse(firstTestHere, Either.Right, () => Either.Left(err)), R.ma
函数的名称是否定义如下: f :: [Either a b] -> Either [a] [b] f x = let (y1, y2) = partitionEithers x in case y
我尝试通过自己的练习找到解决方案,并满足以下要求: 我们需要根据给定的序列移动对象。 序列由 Action 组成。 以下是可能的操作:F、L、R F:前进 L : 向左旋转 90° R : 向右旋转
给定一个序列 Seq[Either[String,A]],其中 Left 是错误消息。我想获得一个 Either[String,Seq[A]] ,其中我得到一个 Right (这将是一个 Seq[A]
假设我有两个功能: b2c :: B -> Either String C a2bs :: A -> [[B]] 如何使用 b2c 和 a2bs 创建以下 a2cs 函数,以便 [[ 中是否有任何 L
查看 Haskell 的 Either Monad,有一个 >>= 函数。 Prelude Map> let add100 = \x -> Right (x+100 :: Int) Prelude M
我正在使用 cats ,想知道如何用它来转数据: val data = NonEmptyList[Either[Error, User]] 到 val target: Either[Error, No
我正在尝试将 Codable 与协议(protocol)一起使用来处理 API 请求和响应。我正在查询的 API 以“结果”键下的一组项目作为响应: { results: ["id": "1", "i
我有一个 Future[Either[A, B]]以及提供 Future[C] 的函数来自 B . 我需要转换 Future[Either[A, B]]至 Future[Either[A, C]] .
首先,我运行了以下代码,运行良好: class Monster: def __init__(self): self._can_do = [] print("cr
var newRight; if(either.isRight()) { newRight = either.getOrElse(() => throw UnimplementedError())
说我有一些代码: def foo(s:String):Either[Bar, Baz] = // some code here ... 我想将其用作: val a = Array("a", "b",
我的表单有四个字段。我需要: - 如果至少填充一个字段,则验证成功 - 对所有字段使用相同的错误“请输入电话或电子邮件” 下面的代码不能在偶然的基础上工作 - 所有字段都是单独验证的,即使我使用该函数
forall m。 MonadFail m => m 可以替换为 Either String,方法是替换 pure = Right 和 fail = Left。但是,由于 Either String
我试过这个: type TestT = Either Int Float testM :: (a -> a) -> TestT -> TestT testM f (Left x) = Left (f
我是一名优秀的程序员,十分优秀!