Skip to content

Commit

Permalink
Merge pull request #326 from nkgm/fix-collections
Browse files Browse the repository at this point in the history
Fixes hocon collections order
  • Loading branch information
zawodskoj authored Nov 15, 2021
2 parents 96cbec9 + cbcb3ec commit 9663c26
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private[hocon] trait ConfigValueDecoderCollectionInstances {
.asScala
.map(dec.decode(Some(SeqElementKeyType), _))
.toList
.reverse
).map(cbf.fromSpecific(_))
}

Expand All @@ -70,6 +71,7 @@ private[hocon] trait ConfigValueDecoderCollectionInstances {
.asScala
.map(entry => dec.decode(Some(MapEntryKeyType), entry.getValue).map(entry.getKey -> _))
.toList
.reverse
).map(_.toMap)
}

Expand Down
37 changes: 0 additions & 37 deletions modules/ciris/src/test/scala/derevo/ciris/CirisSpec.scala

This file was deleted.

76 changes: 76 additions & 0 deletions modules/ciris/src/test/scala/derevo/ciris/CirisSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package derevo.ciris

import derevo.derive
import com.typesafe.config.ConfigFactory
import cats.effect.IO
import ciris.hocon._
import ciris.hocon.instances._
import scala.concurrent.duration.Duration

import org.scalatest.funsuite.AnyFunSuite

import scala.concurrent.ExecutionContext

class CirisSuite extends AnyFunSuite {

implicit val contextShift = IO.contextShift(ExecutionContext.global)

test("hoconSource example") {

@derive(cirisDecoder)
case class Data(name: String, list: List[String], map: Map[String, Int], rate: Rate)

@derive(cirisDecoder)
case class Rate(elements: Int, duration: Option[Duration])

val cfg = ConfigFactory.parseString(
"""
|data {
| name = Demo
| list = [1, 2, 3]
| map.wtf = 1
| map.lol = 2
| map.wut = 3
| rate {
| elements = 2
| duration = 100 millis
| }
|}
""".stripMargin
)

val res = hoconSource[Data](cfg, "data").load[IO].unsafeRunSync()

assert(
res == Data(
"Demo",
List("1", "2", "3"),
Map("wtf" -> 1, "lol" -> 2, "wut" -> 3),
Rate(2, Option(Duration(100, "millis")))
)
)
}

test("README.md example") {

@derive(cirisDecoder)
case class DataConfig(name: String, addresses: List[String], mapping: Map[String, Int])

val cfg = ConfigFactory.parseString(
"""
|data {
| name = AAA
| addresses = [home, work, pub]
| mapping.until = 1
| mapping.from = 2
| mapping.to = 3
|}
""".stripMargin
)

val dataConfig = hoconSource[DataConfig](cfg, "data").as[DataConfig].load[IO].unsafeRunSync()

assert(dataConfig == DataConfig("AAA", List("home", "work", "pub"), Map("until" -> 1, "from" -> 2, "to" -> 3)))
}

}

0 comments on commit 9663c26

Please sign in to comment.