Skip to content

Commit 0ab4592

Browse files
committed
Add roundtrip test for custom Hocon codecs
1 parent 65c5f75 commit 0ab4592

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.avsystem.commons
22
package hocon
33

44
import com.avsystem.commons.annotation.explicitGenerics
5-
import com.avsystem.commons.serialization._
5+
import com.avsystem.commons.serialization.*
6+
import com.avsystem.commons.serialization.cbor.RawCbor
7+
import com.avsystem.commons.serialization.json.RawJson
68
import com.typesafe.config.{ConfigValue, ConfigValueFactory}
79

810
object HoconOutput {
@@ -31,7 +33,14 @@ class HoconOutput(consumer: ConfigValue => Unit) extends OutputAndSimpleOutput {
3133
def writeList(): HoconListOutput = new HoconListOutput(consumer)
3234
def writeObject(): HoconObjectOutput = new HoconObjectOutput(consumer)
3335

34-
//TODO: writeCustom
36+
// TODO: handle other markers in writeCustom? Unfortunately typesafe config does not provide a methods to write
37+
// duration to nice string representation etc.
38+
override def writeCustom[T](typeMarker: TypeMarker[T], value: T): Boolean =
39+
typeMarker match {
40+
case ConfigValueMarker => consumer(value); true
41+
case PeriodMarker => anyRef(value.toString.toLowerCase.stripPrefix("p")); true
42+
case _ => false
43+
}
3544
}
3645

3746
class HoconListOutput(consumer: ConfigValue => Unit) extends ListOutput {

hocon/src/test/scala/com/avsystem/commons/hocon/HoconGenCodecRoundtripTest.scala

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.avsystem.commons
22
package hocon
33

4+
import com.avsystem.commons.hocon.HoconInputTest.CustomCodecsClass
45
import com.avsystem.commons.serialization.{GenCodecRoundtripTest, Input, Output}
5-
import com.typesafe.config.ConfigValue
6+
import com.typesafe.config.{ConfigFactory, ConfigValue}
7+
8+
import java.time.{Duration, Period}
9+
import scala.concurrent.duration.*
610

711
class HoconGenCodecRoundtripTest extends GenCodecRoundtripTest {
812
type Raw = ConfigValue
@@ -15,4 +19,17 @@ class HoconGenCodecRoundtripTest extends GenCodecRoundtripTest {
1519

1620
def createInput(raw: ConfigValue): Input =
1721
new HoconInput(raw)
22+
23+
test("custom codes class") {
24+
val value = CustomCodecsClass(
25+
duration = 1.minute,
26+
jDuration = Duration.ofMinutes(5),
27+
fileSize = SizeInBytes.`1KiB`,
28+
embeddedConfig = ConfigFactory.parseMap(JMap("something" -> "abc")),
29+
period = Period.ofWeeks(2),
30+
clazz = classOf[HoconGenCodecRoundtripTest],
31+
clazzMap = Map(classOf[HoconGenCodecRoundtripTest] -> "abc"),
32+
)
33+
testRoundtrip(value)
34+
}
1835
}

0 commit comments

Comments
 (0)