Skip to content

Commit

Permalink
Configured: add implicit fold/foreach extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 22, 2021
1 parent 05b8f32 commit 95f87b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ object ConfDecoderExT {
val errorB = List.newBuilder[ConfError]
successB.sizeHint(values.length)
values.foreach { value =>
ev.read(state, a2conf(value)) match {
case Configured.NotOk(e) => errorB += e
case Configured.Ok(decoded) => successB += ab2c(value, decoded)
}
val res = ev.read(state, a2conf(value))
res.foreach(errorB += _)(successB += ab2c(value, _))
}
Configured(successB.result(), errorB.result(): _*)
}
Expand Down
32 changes: 25 additions & 7 deletions metaconfig-core/shared/src/main/scala/metaconfig/Configured.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ sealed abstract class Configured[+A] extends Product with Serializable {
def isOk: Boolean = this match { case Ok(_) => true; case _ => false }
def isNotOk: Boolean = !isOk
}
object Configured {

trait ConfiguredLowPriorityImplicits {

implicit def toOption[A](value: Configured[_ <: A]): Option[A] =
value match {
case Configured.Ok(v) => Some(v)
case _ => None
}

}

object Configured extends ConfiguredLowPriorityImplicits {
def apply[T](value: => T, error: Option[ConfError]): Configured[T] =
error.fold(ok(value))(notOk)
def apply[T](value: => T, errors: ConfError*): Configured[T] =
apply(value, ConfError(errors))
def fold[T](value: Option[T], error: => ConfError): Configured[T] =
def opt[T](value: Option[T])(error: => ConfError): Configured[T] =
value.fold(notOk[T](error))(ok)

@deprecated("No longer supported", "0.8.1")
Expand Down Expand Up @@ -90,10 +101,17 @@ object Configured {
def combine(other: NotOk): NotOk = combine(other.error)
}

implicit def toOption[A](value: Configured[_ <: A]): Option[A] =
value match {
case Ok(v) => Some(v)
case _ => None
}
implicit class ConfiguredImplicit[A](value: Configured[A]) {

def fold[B](fa: ConfError => B)(fb: A => B): B =
value match {
case Configured.Ok(value) => fb(value)
case Configured.NotOk(error) => fa(error)
}

def foreach(fa: ConfError => Unit)(fb: A => Unit): Unit =
fold(fa)(fb)

}

}
15 changes: 5 additions & 10 deletions metaconfig-core/shared/src/main/scala/metaconfig/cli/CliApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import fansi.Str
import fansi.Color
import java.nio.file.Paths
import metaconfig.Conf
import metaconfig.Configured.Ok
import metaconfig.Configured.NotOk
import metaconfig.Configured

case class CliApp(
Expand Down Expand Up @@ -40,16 +38,13 @@ case class CliApp(
case subcommand :: tail =>
commands.find(_.matchesName(subcommand)) match {
case Some(command) =>
val conf = Conf.parseCliArgs[command.Value](tail)(command.settings)
val configured: Configured[command.Value] =
conf.andThen(_.as[command.Value](command.decoder))
configured match {
case Ok(value) =>
command.run(value, app)
case NotOk(error) =>
Conf
.parseCliArgs[command.Value](tail)(command.settings)
.andThen(_.as[command.Value](command.decoder))
.fold { error =>
error.all.foreach { message => app.error(message) }
1
}
}(command.run(_, app))
case None =>
HelpCommand.notRecognized(subcommand, app)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import metaconfig.Conf
import metaconfig.ConfDecoder
import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Configured.NotOk
import metaconfig.Configured.Ok

object CanBuildFromDecoder {

Expand Down Expand Up @@ -44,10 +42,7 @@ object CanBuildFromDecoder {
val errorB = List.newBuilder[ConfError]
successB.sizeHint(values.length)
values.foreach { value =>
ev.read(a2conf(value)) match {
case NotOk(e) => errorB += e
case Ok(decoded) => successB += ab2c(value, decoded)
}
ev.read(a2conf(value)).foreach(errorB += _)(successB += ab2c(value, _))
}
Configured(successB.result(), errorB.result(): _*)
}
Expand Down

0 comments on commit 95f87b9

Please sign in to comment.