Skip to content

Commit

Permalink
Deprecate asCollectionCursor and try to read tuples as lists first
Browse files Browse the repository at this point in the history
  • Loading branch information
jcazevedo committed Sep 29, 2018
1 parent 2e29cd4 commit 570b926
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/pureconfig/ConfigCursor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ sealed trait ConfigCursor {
* @return a `Right` with this cursor as a list or object cursor if the cast can be done, `Left` with a list of
* failures otherwise.
*/
@deprecated("Use `asListCursor` and/or `asCollectionCursor` instead", "0.10.0")
def asCollectionCursor: Either[ConfigReaderFailures, Either[ConfigListCursor, ConfigObjectCursor]] = {
if (isUndefined) {
failed(KeyNotFound.forKeys(path, Set()))
Expand Down Expand Up @@ -255,7 +256,7 @@ object ConfigCursor {
* describes. This code mimics the behavior of the package-private `com.typesafe.config.impl.DefaultTransformer` class
* in Typesafe Config.
*/
private def transform(configValue: ConfigValue, requested: ConfigValueType): Either[WrongType, ConfigValue] = {
private[pureconfig] def transform(configValue: ConfigValue, requested: ConfigValueType): Either[WrongType, ConfigValue] = {
(configValue.valueType(), requested) match {
case (valueType, requestedType) if valueType == requestedType =>
Right(configValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ object DerivedConfigReader extends DerivedConfigReader1 {
pr: MapShapedReader.WithDefaults[F, LRepr, DefaultRepr]): DerivedConfigReader[F] = new DerivedConfigReader[F] {

def from(cur: ConfigCursor) = {
// Try to read first as the product representation (i.e.
// ConfigObject with '_1', '_2', etc. keys) and afterwards as the Generic
// representation (i.e. ConfigList).
cur.asCollectionCursor.right.flatMap {
case Right(objCur) => tupleAsObjectReader(objCur)
case Left(_) => tupleAsListReader(cur)
// Try to read first as the list representation and afterwards as the product representation (i.e. ConfigObject
// with '_1', '_2', etc. keys).
val cc = cur.asListCursor.right.map(Right.apply).left.flatMap(failure =>
cur.asObjectCursor.right.map(Left.apply).left.map(_ => failure))

cc.right.flatMap {
case Right(listCur) => tupleAsListReader(listCur)
case Left(objCur) => tupleAsObjectReader(objCur)
}
}
}

private[pureconfig] def tupleAsListReader[F: IsTuple, Repr <: HList](cur: ConfigCursor)(
private[pureconfig] def tupleAsListReader[F: IsTuple, Repr <: HList](cur: ConfigListCursor)(
implicit
gen: Generic.Aux[F, Repr],
cr: SeqShapedReader[Repr]): Either[ConfigReaderFailures, F] =
Expand Down
12 changes: 0 additions & 12 deletions tests/src/test/scala/pureconfig/ConfigCursorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ class ConfigCursorSuite extends BaseSuite {
Right(Map("a" -> cursor("1", "a" :: defaultPath), "b" -> cursor("2", "b" :: defaultPath)))
}

it should "allow being casted to a collection cursor in a safe way" in {
cursor("abc").asCollectionCursor should failWith(
WrongType(ConfigValueType.STRING, Set(ConfigValueType.LIST, ConfigValueType.OBJECT)), defaultPathStr)

cursor("[1, 2]").asCollectionCursor shouldBe
Right(Left(ConfigListCursor(conf("[1, 2]").asInstanceOf[ConfigList], defaultPath)))

cursor("{ a: 1, b: 2 }").asCollectionCursor shouldBe
Right(Right(ConfigObjectCursor(conf("{ a: 1, b: 2 }").asInstanceOf[ConfigObject], defaultPath)))
}

it should "allow access to a given path" in {
cursor("{ a: 2 }").atPath() shouldBe Right(cursor("{ a: 2 }", defaultPath))
cursor("{ a: 2 }").atPath("a") shouldBe Right(cursor("2", "a" :: defaultPath))
Expand All @@ -131,7 +120,6 @@ class ConfigCursorSuite extends BaseSuite {
cur.asList should failWithType[KeyNotFound]
cur.asObjectCursor should failWithType[KeyNotFound]
cur.asMap should failWithType[KeyNotFound]
cur.asCollectionCursor should failWithType[KeyNotFound]
}

behavior of "ConfigListCursor"
Expand Down
3 changes: 2 additions & 1 deletion tests/src/test/scala/pureconfig/TupleConvertersSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TupleConvertersSuite extends BaseSuite {

// Check readers from objects and lists
checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("_1" -> "one", "_2" -> 2).asJava) -> (("one", 2)))
checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("0" -> "one", "1" -> 2).asJava) -> (("one", 2)))
checkRead[(String, Int)](ConfigValueFactory.fromIterable(List("one", 2).asJava) -> (("one", 2)))

// Check writers
Expand All @@ -48,5 +49,5 @@ class TupleConvertersSuite extends BaseSuite {

checkFailures[(String, Int)](
ConfigValueFactory.fromAnyRef("str") -> ConfigReaderFailures(
ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.LIST, ConfigValueType.OBJECT)), None, "")))
ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.LIST)), None, "")))
}

0 comments on commit 570b926

Please sign in to comment.