Skip to content

Commit

Permalink
Drop all redundant Either#right calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ruippeixotog committed Feb 28, 2021
1 parent 70047de commit e9f72d3
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 85 deletions.
Expand Up @@ -11,7 +11,7 @@ trait ProductReaders {
if (reader.isInstanceOf[ReadsMissingKeys])
reader.from(cursor.atKeyOrUndefined(key))
else
cursor.atKey(key).right.flatMap(reader.from))(_(_))
cursor.atKey(key).flatMap(reader.from))(_(_))

/**
* Builds a `ConfigReader` for an object created from the value of 1 key.
Expand All @@ -26,7 +26,7 @@ trait ProductReaders {
readerA0: ConfigReader[A0]
): ConfigReader[B] = new ConfigReader[B] {
def from(cur: ConfigCursor): ConfigReader.Result[B] =
cur.asObjectCursor.right.flatMap { objCur =>
cur.asObjectCursor.flatMap { objCur =>
val a0Result: ConfigReader.Result[A0 => B] = Right(f)
val a1Result = combineReaderResult(a0Result, readerA0, keyA0, objCur)
a1Result
Expand All @@ -45,7 +45,7 @@ trait ProductReaders {
[#readerA0: ConfigReader[A0]#]
): ConfigReader[B] = new ConfigReader[B] {
def from(cur: ConfigCursor): ConfigReader.Result[B] =
cur.asObjectCursor.right.flatMap { objCur =>
cur.asObjectCursor.flatMap { objCur =>
val a##0Result: ConfigReader.Result[[#A0# => ] => B] = Right(f.curried)
[#val a1Result = combineReaderResult(a0Result, readerA0, keyA0, objCur)#
]
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/scala/pureconfig/BasicReaders.scala
Expand Up @@ -40,7 +40,7 @@ trait PrimitiveReaders {
case v => v.toDouble
})

cur.asString.right.flatMap(s => cur.scopeFailure(asStringReader(s))).left.flatMap(_ => cur.asDouble)
cur.asString.flatMap(s => cur.scopeFailure(asStringReader(s))).left.flatMap(_ => cur.asDouble)
})

implicit val floatConfigReader: ConfigReader[Float] = ConfigReader.fromCursor({ cur =>
Expand All @@ -49,7 +49,7 @@ trait PrimitiveReaders {
case v => v.toFloat
})

cur.asString.right.flatMap(s => cur.scopeFailure(asStringReader(s))).left.flatMap(_ => cur.asFloat)
cur.asString.flatMap(s => cur.scopeFailure(asStringReader(s))).left.flatMap(_ => cur.asFloat)
})

implicit val intConfigReader: ConfigReader[Int] = ConfigReader.fromCursor(_.asInt)
Expand Down Expand Up @@ -125,7 +125,7 @@ trait DurationReaders {

implicit val finiteDurationConfigReader: ConfigReader[FiniteDuration] = {
val fromString: String => Either[FailureReason, FiniteDuration] = { string =>
DurationUtils.fromString(string).right.flatMap {
DurationUtils.fromString(string).flatMap {
case d: FiniteDuration => Right(d)
case _ =>
Left(
Expand Down Expand Up @@ -163,16 +163,16 @@ trait NumericReaders {
trait TypesafeConfigReaders {

implicit val configConfigReader: ConfigReader[Config] =
ConfigReader.fromCursor(_.asObjectCursor.right.map(_.objValue.toConfig))
ConfigReader.fromCursor(_.asObjectCursor.map(_.objValue.toConfig))

implicit val configObjectConfigReader: ConfigReader[ConfigObject] =
ConfigReader.fromCursor(_.asObjectCursor.right.map(_.objValue))
ConfigReader.fromCursor(_.asObjectCursor.map(_.objValue))

implicit val configValueConfigReader: ConfigReader[ConfigValue] =
ConfigReader.fromCursor(_.asConfigValue)

implicit val configListConfigReader: ConfigReader[ConfigList] =
ConfigReader.fromCursor(_.asListCursor.right.map(_.listValue))
ConfigReader.fromCursor(_.asListCursor.map(_.listValue))

// TODO: improve error handling by copying the parsing logic from Typesafe and making it use PureConfig Results
// (see PeriodUtils and DurationUtils)
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/pureconfig/CollectionReaders.scala
Expand Up @@ -19,7 +19,7 @@ trait CollectionReaders {
new ConfigReader[Option[A]] with ReadsMissingKeys {
override def from(cur: ConfigCursor): ConfigReader.Result[Option[A]] = {
if (cur.isUndefined || cur.isNull) Right(None)
else conv.value.from(cur).right.map(Some(_))
else conv.value.from(cur).map(Some(_))
}
}

Expand All @@ -30,7 +30,7 @@ trait CollectionReaders {
new ConfigReader[F[A]] {

override def from(cur: ConfigCursor): ConfigReader.Result[F[A]] = {
cur.fluent.mapList { valueCur => configConvert.value.from(valueCur) }.right.map { coll =>
cur.fluent.mapList { valueCur => configConvert.value.from(valueCur) }.map { coll =>
val builder = cbf.newBuilder()
(builder ++= coll).result()
}
Expand All @@ -47,7 +47,7 @@ trait CollectionReaders {
implicit def arrayReader[A: ClassTag](implicit reader: Derivation[ConfigReader[A]]): ConfigReader[Array[A]] =
new ConfigReader[Array[A]] {
override def from(cur: ConfigCursor): ConfigReader.Result[Array[A]] =
cur.fluent.mapList(reader.value.from).right.map(_.toArray)
cur.fluent.mapList(reader.value.from).map(_.toArray)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/pureconfig/ConfigConvert.scala
Expand Up @@ -70,7 +70,7 @@ object ConfigConvert extends ConvertHelpers {
def viaNonEmptyString[A](fromF: String => Either[FailureReason, A], toF: A => String)(implicit
ct: ClassTag[A]
): ConfigConvert[A] = {
viaString[A](string => ensureNonEmpty(ct)(string).right.flatMap(s => fromF(s)), toF)
viaString[A](string => ensureNonEmpty(ct)(string).flatMap(s => fromF(s)), toF)
}

def viaNonEmptyStringTry[A: ClassTag](fromF: String => Try[A], toF: A => String): ConfigConvert[A] = {
Expand Down
20 changes: 10 additions & 10 deletions core/src/main/scala/pureconfig/ConfigCursor.scala
Expand Up @@ -173,31 +173,31 @@ sealed trait ConfigCursor {
* otherwise.
*/
def asListCursor: ConfigReader.Result[ConfigListCursor] =
castOrFail(LIST, v => Right(v.asInstanceOf[ConfigList])).right.map(ConfigListCursor(_, pathElems))
castOrFail(LIST, v => Right(v.asInstanceOf[ConfigList])).map(ConfigListCursor(_, pathElems))

/** Casts this cursor to a list of cursors.
*
* @return a `Right` with the list pointed to by this cursor if the cast can be done, `Left` with a list of failures
* otherwise.
*/
def asList: ConfigReader.Result[List[ConfigCursor]] =
asListCursor.right.map(_.list)
asListCursor.map(_.list)

/** Casts this cursor to a `ConfigObjectCursor`.
*
* @return a `Right` with this cursor as an object cursor if it points to an object, `Left` with a list of failures
* otherwise.
*/
def asObjectCursor: ConfigReader.Result[ConfigObjectCursor] =
castOrFail(OBJECT, v => Right(v.asInstanceOf[ConfigObject])).right.map(ConfigObjectCursor(_, pathElems))
castOrFail(OBJECT, v => Right(v.asInstanceOf[ConfigObject])).map(ConfigObjectCursor(_, pathElems))

/** Casts this cursor to a map from config keys to cursors.
*
* @return a `Right` with the map pointed to by this cursor if the cast can be done, `Left` with a list of failures
* otherwise.
*/
def asMap: ConfigReader.Result[Map[String, ConfigCursor]] =
asObjectCursor.right.map(_.map)
asObjectCursor.map(_.map)

/** Returns a cursor to the config at the path composed of given path segments.
*
Expand All @@ -215,15 +215,15 @@ sealed trait ConfigCursor {
*/
@deprecated("Use `asListCursor` and/or `asObjectCursor` instead", "0.10.1")
def asCollectionCursor: ConfigReader.Result[Either[ConfigListCursor, ConfigObjectCursor]] = {
asConfigValue.right.flatMap { value =>
val listAtLeft = asListCursor.right.map(Left.apply)
lazy val mapAtRight = asObjectCursor.right.map(Right.apply)
asConfigValue.flatMap { value =>
val listAtLeft = asListCursor.map(Left.apply)
lazy val mapAtRight = asObjectCursor.map(Right.apply)
listAtLeft.left.flatMap(_ => mapAtRight).left.flatMap(_ => failed(WrongType(value.valueType, Set(LIST, OBJECT))))
}
}

def fluent: FluentConfigCursor =
FluentConfigCursor(asConfigValue.right.map(_ => this))
FluentConfigCursor(asConfigValue.map(_ => this))

/** Returns a failed `ConfigReader` result resulting from scoping a `FailureReason` into the context of this cursor.
*
Expand Down Expand Up @@ -265,8 +265,8 @@ sealed trait ConfigCursor {
cast: ConfigValue => Either[FailureReason, A]
): ConfigReader.Result[A] = {

asConfigValue.right.flatMap { value =>
scopeFailure(ConfigCursor.transform(value, expectedType).right.flatMap(cast))
asConfigValue.flatMap { value =>
scopeFailure(ConfigCursor.transform(value, expectedType).flatMap(cast))
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/scala/pureconfig/ConfigReader.scala
Expand Up @@ -38,7 +38,7 @@ trait ConfigReader[A] {
* @return a `ConfigReader` returning the results of this reader mapped by `f`.
*/
def map[B](f: A => B): ConfigReader[B] =
fromCursor[B] { cur => from(cur).right.flatMap { v => cur.scopeFailure(toResult(f)(v)) } }
fromCursor[B] { cur => from(cur).flatMap { v => cur.scopeFailure(toResult(f)(v)) } }

/** Maps a function that can possibly fail over the results of this reader.
*
Expand All @@ -48,7 +48,7 @@ trait ConfigReader[A] {
* as a success or failure.
*/
def emap[B](f: A => Either[FailureReason, B]): ConfigReader[B] =
fromCursor[B] { cur => from(cur).right.flatMap { v => cur.scopeFailure(f(v)) } }
fromCursor[B] { cur => from(cur).flatMap { v => cur.scopeFailure(f(v)) } }

/** Monadically bind a function over the results of this reader.
*
Expand All @@ -57,7 +57,7 @@ trait ConfigReader[A] {
* @return a `ConfigReader` returning the results of this reader bound by `f`.
*/
def flatMap[B](f: A => ConfigReader[B]): ConfigReader[B] =
fromCursor[B] { cur => from(cur).right.flatMap(f(_).from(cur)) }
fromCursor[B] { cur => from(cur).flatMap(f(_).from(cur)) }

/** Combines this reader with another, returning both results as a pair.
*
Expand Down Expand Up @@ -131,8 +131,7 @@ object ConfigReader extends BasicReaders with CollectionReaders with ProductRead
case (Left(err), Right(_)) => Left(err)
case (Right(_), Left(err)) => Left(err)
case (Left(errs), Left(err)) => Left(errs ++ err)
}.right
.map(_.result())
}.map(_.result())
}

/** Merges two `Result`s using a given function.
Expand Down Expand Up @@ -172,7 +171,7 @@ object ConfigReader extends BasicReaders with CollectionReaders with ProductRead
* @return a `ConfigReader` for reading objects of type `A` using `fromF`.
*/
def fromFunction[A](fromF: ConfigValue => ConfigReader.Result[A]) =
fromCursor(_.asConfigValue.right.flatMap(fromF))
fromCursor(_.asConfigValue.flatMap(fromF))

def fromString[A](fromF: String => Either[FailureReason, A]): ConfigReader[A] =
ConfigReader.fromCursor(_.asString).emap(fromF)
Expand All @@ -186,7 +185,7 @@ object ConfigReader extends BasicReaders with CollectionReaders with ProductRead
}

def fromNonEmptyString[A](fromF: String => Either[FailureReason, A])(implicit ct: ClassTag[A]): ConfigReader[A] = {
fromString(string => ensureNonEmpty(ct)(string).right.flatMap(s => fromF(s)))
fromString(string => ensureNonEmpty(ct)(string).flatMap(s => fromF(s)))
}

def fromNonEmptyStringTry[A](fromF: String => Try[A])(implicit ct: ClassTag[A]): ConfigReader[A] = {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/scala/pureconfig/ConfigSource.scala
Expand Up @@ -34,7 +34,7 @@ trait ConfigSource {
* @return a cursor for a `ConfigValue` retrieved from this source.
*/
def cursor(): Result[ConfigCursor] =
value().right.map(ConfigCursor(_, Nil))
value().map(ConfigCursor(_, Nil))

/** Returns a fluent cursor for a `ConfigValue` retrieved from this source.
*
Expand All @@ -58,7 +58,7 @@ trait ConfigSource {
* `A` from this source, a `Failure` with details on why it isn't possible otherwise
*/
final def load[A](implicit reader: Derivation[ConfigReader[A]]): Result[A] =
cursor().right.flatMap(reader.value.from)
cursor().flatMap(reader.value.from)

/** Loads a configuration of type `A` from this source. If it is not possible to create an
* instance of `A`, this method throws a `ConfigReaderException`.
Expand All @@ -83,11 +83,11 @@ trait ConfigSource {
final class ConfigObjectSource private (getConf: () => Result[Config]) extends ConfigSource {

def value(): Result[ConfigObject] =
config().right.flatMap(_.resolveSafe()).right.map(_.root)
config().flatMap(_.resolveSafe()).map(_.root)

// Avoids unnecessary cast on `ConfigCursor#asObjectCursor`.
override def cursor(): Result[ConfigCursor] =
value().right.map(ConfigObjectCursor(_, Nil))
value().map(ConfigObjectCursor(_, Nil))

/** Reads a `Config` from this config source. The returned config is usually unresolved, unless
* the source forces it otherwise.
Expand Down Expand Up @@ -179,7 +179,7 @@ object ConfigSource {
* custom application config source.
*/
def default(appSource: ConfigObjectSource): ConfigObjectSource =
ConfigObjectSource(appSource.config().right.flatMap(ConfigFactoryWrapper.load))
ConfigObjectSource(appSource.config().flatMap(ConfigFactoryWrapper.load))

/** A config source that always provides empty configs.
*/
Expand Down Expand Up @@ -301,7 +301,7 @@ object ConfigSource {
*/
private[pureconfig] def fromCursor(cur: FluentConfigCursor): ConfigSource =
new ConfigSource {
def value(): Result[ConfigValue] = cur.cursor.right.flatMap(_.asConfigValue)
def value(): Result[ConfigValue] = cur.cursor.flatMap(_.asConfigValue)
override def cursor() = cur.cursor
override def fluentCursor() = cur
}
Expand Down
30 changes: 15 additions & 15 deletions core/src/main/scala/pureconfig/FluentConfigCursor.scala
Expand Up @@ -16,63 +16,63 @@ case class FluentConfigCursor(cursor: ConfigReader.Result[ConfigCursor]) {
* @return a `Right` with the string value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asString: ConfigReader.Result[String] = cursor.right.flatMap(_.asString)
def asString: ConfigReader.Result[String] = cursor.flatMap(_.asString)

/** Casts this cursor to a boolean.
*
* @return a `Right` with the boolean value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asBoolean: ConfigReader.Result[Boolean] = cursor.right.flatMap(_.asBoolean)
def asBoolean: ConfigReader.Result[Boolean] = cursor.flatMap(_.asBoolean)

/** Casts this cursor to a long.
*
* @return a `Right` with the long value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asLong: ConfigReader.Result[Long] = cursor.right.flatMap(_.asLong)
def asLong: ConfigReader.Result[Long] = cursor.flatMap(_.asLong)

/** Casts this cursor to an int.
*
* @return a `Right` with the int value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asInt: ConfigReader.Result[Int] = cursor.right.flatMap(_.asInt)
def asInt: ConfigReader.Result[Int] = cursor.flatMap(_.asInt)

/** Casts this cursor to a short.
*
* @return a `Right` with the short value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asShort: ConfigReader.Result[Short] = cursor.right.flatMap(_.asShort)
def asShort: ConfigReader.Result[Short] = cursor.flatMap(_.asShort)

/** Casts this cursor to a double.
*
* @return a `Right` with the double value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asDouble: ConfigReader.Result[Double] = cursor.right.flatMap(_.asDouble)
def asDouble: ConfigReader.Result[Double] = cursor.flatMap(_.asDouble)

/** Casts this cursor to a float.
*
* @return a `Right` with the float value pointed to by this cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
def asFloat: ConfigReader.Result[Float] = cursor.right.flatMap(_.asFloat)
def asFloat: ConfigReader.Result[Float] = cursor.flatMap(_.asFloat)

/** Casts this cursor to a `ConfigListCursor`.
*
* @return a `Right` with this cursor as a list cursor if the cast can be done, `Left` with a list of failures
* otherwise.
*/
def asListCursor: ConfigReader.Result[ConfigListCursor] = cursor.right.flatMap(_.asListCursor)
def asListCursor: ConfigReader.Result[ConfigListCursor] = cursor.flatMap(_.asListCursor)

/** Casts this cursor to a `ConfigObjectCursor`.
*
* @return a `Right` with this cursor as an object cursor if it points to an object, `Left` with a list of failures
* otherwise.
*/
def asObjectCursor: ConfigReader.Result[ConfigObjectCursor] = cursor.right.flatMap(_.asObjectCursor)
def asObjectCursor: ConfigReader.Result[ConfigObjectCursor] = cursor.flatMap(_.asObjectCursor)

/** Returns a cursor to the config at a given path.
*
Expand All @@ -82,8 +82,8 @@ case class FluentConfigCursor(cursor: ConfigReader.Result[ConfigCursor]) {
def at(segments: PathSegment*): FluentConfigCursor =
FluentConfigCursor {
segments.foldLeft(this.cursor) {
case (Right(cur), PathSegment.Key(k)) => cur.asObjectCursor.right.flatMap(_.atKey(k))
case (Right(cur), PathSegment.Index(i)) => cur.asListCursor.right.flatMap(_.atIndex(i))
case (Right(cur), PathSegment.Key(k)) => cur.asObjectCursor.flatMap(_.atKey(k))
case (Right(cur), PathSegment.Index(i)) => cur.asListCursor.flatMap(_.atIndex(i))
case (Left(err), _) => Left(err)
}
}
Expand All @@ -97,7 +97,7 @@ case class FluentConfigCursor(cursor: ConfigReader.Result[ConfigCursor]) {
* casts and mappings can be done, `Left` with a list of failures otherwise.
*/
def mapList[A](f: ConfigCursor => ConfigReader.Result[A]): ConfigReader.Result[List[A]] =
asListCursor.right.flatMap { listCur => ConfigReader.Result.sequence(listCur.list.map(f)) }
asListCursor.flatMap { listCur => ConfigReader.Result.sequence(listCur.list.map(f)) }

/** Casts this cursor to a `ConfigObjectCursor` and maps each value to a result. This method tries to map all
* elements, combining failures from all of them if more than one exists.
Expand All @@ -108,8 +108,8 @@ case class FluentConfigCursor(cursor: ConfigReader.Result[ConfigCursor]) {
* casts and mappings can be done, `Left` with a list of failures otherwise.
*/
def mapObject[A](f: ConfigCursor => ConfigReader.Result[A]): ConfigReader.Result[Map[String, A]] =
asObjectCursor.right.flatMap { objCur =>
val kvResults = objCur.map.map { case (key, cur) => f(cur).right.map((key, _)) }
ConfigReader.Result.sequence[(String, A), Iterable](kvResults).right.map(_.toMap)
asObjectCursor.flatMap { objCur =>
val kvResults = objCur.map.map { case (key, cur) => f(cur).map((key, _)) }
ConfigReader.Result.sequence[(String, A), Iterable](kvResults).map(_.toMap)
}
}
Expand Up @@ -74,5 +74,5 @@ object ConfigFactoryWrapper {

/** Utility methods that parse a file and then calls `ConfigFactory.load` */
def loadFile(path: Path): ConfigReader.Result[Config] =
parseFile(path.toFile).right.flatMap(rawConfig => unsafeToReaderResult(ConfigFactory.load(rawConfig)))
parseFile(path.toFile).flatMap(rawConfig => unsafeToReaderResult(ConfigFactory.load(rawConfig)))
}

0 comments on commit e9f72d3

Please sign in to comment.