Skip to content

Commit b9965fd

Browse files
authored
Merge pull request AVSystem#104 from AVSystem/commons-hocon
commons-hocon module with HoconInput for GenCodec
2 parents 9d9e646 + e9acc01 commit b9965fd

File tree

17 files changed

+1322
-730
lines changed

17 files changed

+1322
-730
lines changed

build.sbt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ lazy val `commons-jvm` = project.in(file(".jvm"))
177177
`commons-rest`,
178178
`commons-jetty`,
179179
`commons-mongo`,
180+
`commons-hocon`,
180181
`commons-spring`,
181182
`commons-redis`,
182183
`commons-akka`,
@@ -370,16 +371,24 @@ lazy val `commons-redis` = project
370371
parallelExecution in Test := false,
371372
)
372373

373-
lazy val `commons-spring` = project
374+
lazy val `commons-hocon` = project
374375
.dependsOn(`commons-core` % CompileAndTest)
375376
.settings(
376377
jvmCommonSettings,
377378
libraryDependencies ++= Seq(
378-
"org.springframework" % "spring-context" % springVersion,
379379
"com.typesafe" % "config" % typesafeConfigVersion,
380380
),
381381
)
382382

383+
lazy val `commons-spring` = project
384+
.dependsOn(`commons-hocon` % CompileAndTest)
385+
.settings(
386+
jvmCommonSettings,
387+
libraryDependencies ++= Seq(
388+
"org.springframework" % "spring-context" % springVersion,
389+
),
390+
)
391+
383392
lazy val `commons-akka` = project
384393
.dependsOn(`commons-core` % CompileAndTest)
385394
.settings(
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.avsystem.commons
22
package serialization
33

4-
trait JCodecTestBase extends CodecTestBase {
4+
import CodecTestData._
5+
6+
trait JCodecTestBase extends AbstractCodecTest {
57
val jTreeMap = stringMap(new JTreeMap[String, Int])
68
}

commons-core/jvm/src/test/scala/com/avsystem/commons/serialization/JGenCodecTest.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import java.lang.annotation.RetentionPolicy
55

66
import scala.collection.immutable.ListMap
77

8-
class JGenCodecTest extends JCodecTestBase {
8+
class JGenCodecTest extends JCodecTestBase with SimpleIOCodecTest {
99
test("java collection test (TreeMap)") {
10-
testWriteRead[JSortedMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
11-
testWriteRead[JNavigableMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
12-
testWriteRead[JTreeMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
10+
testWrite[JSortedMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
11+
testWrite[JNavigableMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
12+
testWrite[JTreeMap[String, Int]](jTreeMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
1313
}
1414

1515
test("java enum test") {
16-
testWriteRead(RetentionPolicy.RUNTIME, "RUNTIME")
17-
testWriteRead(RetentionPolicy.SOURCE, "SOURCE")
16+
testWrite(RetentionPolicy.RUNTIME, "RUNTIME")
17+
testWrite(RetentionPolicy.SOURCE, "SOURCE")
1818
}
1919
}

commons-core/src/main/scala/com/avsystem/commons/serialization/InputOutput.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,20 @@ trait ObjectInput extends Any with SequentialInput { self =>
306306
* present in the object, regardless of field order assumed by [[nextField]].
307307
* `Opt.Empty` is returned when field is absent or always when this `ObjectInput` does not support random field
308308
* access (in which case it must preserve field order instead).
309-
* NOTE: calling [[peekField]] and using [[FieldInput]] returned by it MUST NOT change state of this `ObjectInput`.
309+
* NOTE: calling [[peekField]] and using [[com.avsystem.commons.serialization.FieldInput FieldInput]] returned by
310+
* it MUST NOT change state of this `ObjectInput`.
310311
* Therefore, it cannot in any way influence results returned by [[nextField]] and [[hasNext]].
311-
* For example, if a [[FieldInput]] for particular field has already been
312+
* For example, if a [[com.avsystem.commons.serialization.FieldInput FieldInput]] for particular field has already been
312313
* accessed using [[peekField]] but has not yet been returned by [[nextField]] then it MUST be returned at some
313314
* point in the future by [[nextField]].
314315
*/
315316
def peekField(name: String): Opt[FieldInput] = Opt.Empty
316317

317318
/**
318-
* Tries to obtain [[FieldInput]] for field with specified name, either by using [[peekField]] (assuming format with
319-
* random field access) or [[nextField]] (assuming format that preserves field order). A codec that uses this method
320-
* must ensure that it reads fields in the same order as they were written using `writeField` on [[ObjectOutput]].
319+
* Tries to obtain [[com.avsystem.commons.serialization.FieldInput FieldInput]] for field with specified name,
320+
* either by using [[peekField]] (assuming format with random field access) or [[nextField]] (assuming format that
321+
* preserves field order). A codec that uses this method must ensure that it reads fields in the same order as they
322+
* were written using `writeField` on [[ObjectOutput]].
321323
*/
322324
def getNextNamedField(name: String): FieldInput =
323325
peekField(name).getOrElse {

commons-core/src/test/scala-2.12/com/avsystem/commons/serialization/OverloadedApplyCodecTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.avsystem.commons
22
package serialization
33

4-
class OverloadedApplyCodecTest extends CodecTestBase {
4+
class OverloadedApplyCodecTest extends SimpleIOCodecTest {
55
case class OverloadedApply(private val fields: Map[String, Int])
66
object OverloadedApply extends HasGenCodec[OverloadedApply] {
77
def apply(fields: Seq[Int]): OverloadedApply = OverloadedApply(fields.toMapBy(_.toString))
88
def unapply(oa: OverloadedApply): Option[Seq[Int]] = Some(oa.fields.values.toSeq)
99
}
1010

1111
test("overloaded apply/unapply") {
12-
testWriteRead(OverloadedApply(List(1, 2, 3)),
12+
testWrite(OverloadedApply(List(1, 2, 3)),
1313
Map("fields" -> List(1, 2, 3))
1414
)
1515
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.avsystem.commons
2+
package serialization
3+
4+
import org.scalactic.source.Position
5+
import org.scalatest.FunSuite
6+
7+
trait AbstractCodecTest extends FunSuite {
8+
def assertSameTypeValue[T](v1: T, v2: T)(implicit pos: Position): Unit = {
9+
assert(v1 == v2)
10+
assert(v1 == null || v1.getClass == v2.getClass)
11+
}
12+
13+
type Raw
14+
15+
def writeToOutput(write: Output => Unit): Raw
16+
def createInput(raw: Raw): Input
17+
18+
def testWrite[T: GenCodec](value: T, expectedRepr: Raw)(implicit pos: Position): Unit = {
19+
val repr = writeToOutput(GenCodec.write[T](_, value))
20+
assert(repr == expectedRepr)
21+
}
22+
23+
def testRead[T: GenCodec](repr: Raw, expected: T)(implicit pos: Position): Unit = {
24+
assert(expected == GenCodec.read[T](createInput(repr)))
25+
}
26+
27+
def testRoundtrip[T: GenCodec](value: T)(implicit pos: Position): Unit = {
28+
val written: Raw = writeToOutput(GenCodec.write[T](_, value))
29+
val readBack = GenCodec.read[T](createInput(written))
30+
assertSameTypeValue(value, readBack)
31+
}
32+
}

commons-core/src/test/scala/com/avsystem/commons/serialization/CodecTestBase.scala

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)