From 0ab45922c2c91a6c5c6ba5efbc1a7f6f67df29b1 Mon Sep 17 00:00:00 2001 From: Sebastian Haracz Date: Sat, 28 Sep 2024 14:55:25 +0200 Subject: [PATCH] Add roundtrip test for custom Hocon codecs --- .../avsystem/commons/hocon/HoconOutput.scala | 13 +++++++++++-- .../hocon/HoconGenCodecRoundtripTest.scala | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala b/hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala index 511a2e8fe..d35a3ca64 100644 --- a/hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala +++ b/hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala @@ -2,7 +2,9 @@ package com.avsystem.commons package hocon import com.avsystem.commons.annotation.explicitGenerics -import com.avsystem.commons.serialization._ +import com.avsystem.commons.serialization.* +import com.avsystem.commons.serialization.cbor.RawCbor +import com.avsystem.commons.serialization.json.RawJson import com.typesafe.config.{ConfigValue, ConfigValueFactory} object HoconOutput { @@ -31,7 +33,14 @@ class HoconOutput(consumer: ConfigValue => Unit) extends OutputAndSimpleOutput { def writeList(): HoconListOutput = new HoconListOutput(consumer) def writeObject(): HoconObjectOutput = new HoconObjectOutput(consumer) - //TODO: writeCustom + // TODO: handle other markers in writeCustom? Unfortunately typesafe config does not provide a methods to write + // duration to nice string representation etc. + override def writeCustom[T](typeMarker: TypeMarker[T], value: T): Boolean = + typeMarker match { + case ConfigValueMarker => consumer(value); true + case PeriodMarker => anyRef(value.toString.toLowerCase.stripPrefix("p")); true + case _ => false + } } class HoconListOutput(consumer: ConfigValue => Unit) extends ListOutput { diff --git a/hocon/src/test/scala/com/avsystem/commons/hocon/HoconGenCodecRoundtripTest.scala b/hocon/src/test/scala/com/avsystem/commons/hocon/HoconGenCodecRoundtripTest.scala index 9d015347c..f79ad3b5b 100644 --- a/hocon/src/test/scala/com/avsystem/commons/hocon/HoconGenCodecRoundtripTest.scala +++ b/hocon/src/test/scala/com/avsystem/commons/hocon/HoconGenCodecRoundtripTest.scala @@ -1,8 +1,12 @@ package com.avsystem.commons package hocon +import com.avsystem.commons.hocon.HoconInputTest.CustomCodecsClass import com.avsystem.commons.serialization.{GenCodecRoundtripTest, Input, Output} -import com.typesafe.config.ConfigValue +import com.typesafe.config.{ConfigFactory, ConfigValue} + +import java.time.{Duration, Period} +import scala.concurrent.duration.* class HoconGenCodecRoundtripTest extends GenCodecRoundtripTest { type Raw = ConfigValue @@ -15,4 +19,17 @@ class HoconGenCodecRoundtripTest extends GenCodecRoundtripTest { def createInput(raw: ConfigValue): Input = new HoconInput(raw) + + test("custom codes class") { + val value = CustomCodecsClass( + duration = 1.minute, + jDuration = Duration.ofMinutes(5), + fileSize = SizeInBytes.`1KiB`, + embeddedConfig = ConfigFactory.parseMap(JMap("something" -> "abc")), + period = Period.ofWeeks(2), + clazz = classOf[HoconGenCodecRoundtripTest], + clazzMap = Map(classOf[HoconGenCodecRoundtripTest] -> "abc"), + ) + testRoundtrip(value) + } }