Skip to content

Commit

Permalink
Merge d85aeae into c175340
Browse files Browse the repository at this point in the history
  • Loading branch information
ruippeixotog committed Jun 8, 2017
2 parents c175340 + d85aeae commit e4c428c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
18 changes: 9 additions & 9 deletions core/src/main/scala/pureconfig/DerivedReaders.scala
Expand Up @@ -71,22 +71,22 @@ trait DerivedReaders1 {
implicit final def hConsConfigReader[Wrapped, K <: Symbol, V, T <: HList, U <: HList](
implicit
key: Witness.Aux[K],
vFieldConvert: Lazy[ConfigReader[V]],
vFieldReader: Lazy[ConfigReader[V]],
tConfigReader: Lazy[WrappedDefaultValue[Wrapped, T, U]],
hint: ProductHint[Wrapped]): WrappedDefaultValue[Wrapped, FieldType[K, V] :: T, Option[V] :: U] = new WrappedDefaultValue[Wrapped, FieldType[K, V] :: T, Option[V] :: U] {

override def fromConfigObject(co: ConfigObject, default: Option[V] :: U): Either[ConfigReaderFailures, FieldType[K, V] :: T] = {
val fieldName = key.value.name
val keyStr = hint.configKey(fieldName)
val headResult = improveFailures[V](
(co.get(keyStr), vFieldConvert.value) match {
case (null, converter: AllowMissingKey) =>
converter.from(co.get(keyStr))
case (null, _) =>
val defaultValue = if (hint.useDefaultArgs) default.head else None
defaultValue.fold(fail[V](CannotConvertNull(fieldName, co.keySet.asScala)))(Right[Nothing, V](_))
case (value, converter) =>
converter.from(value)
(co.get(keyStr), vFieldReader.value) match {
case (null, reader) =>
default.head match {
case Some(defaultValue) if hint.useDefaultArgs => Right[Nothing, V](defaultValue)
case _ if reader.isInstanceOf[AllowMissingKey] => reader.from(null)
case _ => fail[V](CannotConvertNull(fieldName, co.keySet.asScala))
}
case (value, reader) => reader.from(value)
},
keyStr,
ConfigValueLocation(co))
Expand Down
11 changes: 7 additions & 4 deletions core/src/test/scala/pureconfig/ProductConvertersSuite.scala
Expand Up @@ -88,13 +88,13 @@ class ProductConvertersSuite extends BaseSuite {

it should "consider default arguments by default" in {
case class InnerConf(e: Int, g: Int)
case class Conf(a: Int, b: String = "default", c: Int = 42, d: InnerConf = InnerConf(43, 44))
case class Conf(a: Int, b: String = "default", c: Int = 42, d: InnerConf = InnerConf(43, 44), e: Option[Int] = Some(45))

val conf1 = ConfigFactory.parseMap(Map("a" -> 2).asJava).root()
ConfigConvert[Conf].from(conf1).right.value shouldBe Conf(2, "default", 42, InnerConf(43, 44))
ConfigConvert[Conf].from(conf1).right.value shouldBe Conf(2, "default", 42, InnerConf(43, 44), Some(45))

val conf2 = ConfigFactory.parseMap(Map("a" -> 2, "c" -> 50).asJava).root()
ConfigConvert[Conf].from(conf2).right.value shouldBe Conf(2, "default", 50, InnerConf(43, 44))
ConfigConvert[Conf].from(conf2).right.value shouldBe Conf(2, "default", 50, InnerConf(43, 44), Some(45))

val conf3 = ConfigFactory.parseMap(Map("c" -> 50).asJava).root()
ConfigConvert[Conf].from(conf3).left.value.toList should contain theSameElementsAs Seq(KeyNotFound("a", None))
Expand All @@ -103,11 +103,14 @@ class ProductConvertersSuite extends BaseSuite {
ConfigConvert[Conf].from(conf4).left.value.toList should contain theSameElementsAs Seq(KeyNotFound("d.g", None))

val conf5 = ConfigFactory.parseMap(Map("a" -> 2, "d.e" -> 5, "d.g" -> 6).asJava).root()
ConfigConvert[Conf].from(conf5).right.value shouldBe Conf(2, "default", 42, InnerConf(5, 6))
ConfigConvert[Conf].from(conf5).right.value shouldBe Conf(2, "default", 42, InnerConf(5, 6), Some(45))

val conf6 = ConfigFactory.parseMap(Map("a" -> 2, "d" -> "notAnInnerConf").asJava).root()
val failures = ConfigConvert[Conf].from(conf6).left.value.toList
failures should have size 1
failures.head shouldBe a[WrongType]

val conf7 = ConfigFactory.parseMap(Map("a" -> 2, "c" -> 50, "e" -> 1).asJava).root()
ConfigConvert[Conf].from(conf7).right.value shouldBe Conf(2, "default", 50, InnerConf(43, 44), Some(1))
}
}

0 comments on commit e4c428c

Please sign in to comment.