Skip to content

Commit

Permalink
Add roundtrip test for custom Hocon codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaciv committed Sep 28, 2024
1 parent 65c5f75 commit 0ab4592
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 11 additions & 2 deletions hocon/src/main/scala/com/avsystem/commons/hocon/HoconOutput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}

0 comments on commit 0ab4592

Please sign in to comment.