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

Configured: add implicit fold/foreach extensions #127

Merged
merged 1 commit into from
May 22, 2021
Merged
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 @@ -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
35 changes: 28 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,20 @@ 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 getOrRecover(fa: ConfError => A): A =
fold(fa)(identity)

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)

}

}
19 changes: 7 additions & 12 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) =>
error.all.foreach { message => app.error(message) }
1
}
val configured: Configured[command.Value] = Conf
.parseCliArgs[command.Value](tail)(command.settings)
.andThen(_.as[command.Value](command.decoder))
configured.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