Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accumulate errors when reading Eithers #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ object Argument extends PlatformArguments {
override def combineK[A](x: Argument[A], y: Argument[A]): Argument[A] =
from[A](s"${x.defaultMetavar} | ${y.defaultMetavar}") { string =>
val ax = x.read(string)
if (ax.isValid) ax
else y.read(string)
ax match {
case Validated.Valid(a) => ax
case Validated.Invalid(e) => y.read(string).leftMap(e ++ _.toList)
}
}

/*
Expand Down
14 changes: 14 additions & 0 deletions core/shared/src/test/scala/com/monovore/decline/ArgumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.monovore.decline
import com.monovore.decline.discipline.ArgumentSuite
import cats.{Defer, SemigroupK, Show}
import cats.data.Validated
import cats.data.NonEmptyList
import org.scalacheck.{Arbitrary, Gen}

import java.util.UUID
Expand Down Expand Up @@ -154,6 +155,19 @@ class ArgumentSpec extends ArgumentSuite {
)
}

test("Argument[Either[_, _]] accumulates errors") {
val arg = Argument[Either[Int, Char]]
val result = arg.read("abc")
assert(
result == Validated.Invalid(
NonEmptyList.of(
"Invalid character: abc",
"Invalid integer: abc"
)
)
)
}

test("test Argument.fromMap") {
val a0 = Argument.fromMap("foo", Map.empty)
val r0 = a0.read("bar").toEither.left.toOption
Expand Down