Skip to content

Commit

Permalink
Properties accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
eshu committed Nov 11, 2021
1 parent 77ffc56 commit d58495f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions core/src/main/scala/borscht/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ trait CfgNode extends Node with Iterable[(String, Node)] :

final def map[T: NodeParser](ref: String*): Map[String, T] = get[Map[String, T]](ref: _*) getOrElse Map.empty

final def properties(ref: String*): Map[String, String] =
node(ref: _*) map (properties(ref.mkString("."), _, Map.empty)) getOrElse Map.empty

private def properties(prefix: String, node: Node, result: Map[String, String]): Map[String, String] = node match
case scalar: ScalarNode => result + (prefix -> scalar.asString)
case cfg: CfgNode =>
val p = prefix + "."
(cfg foldLeft result) { case (r, (k, n)) => properties(p + k, n, r) }
case seq: SeqNode =>
val p = prefix + "."
(seq.iterator.zipWithIndex foldLeft result) { case (r, (n, i)) => properties(p + i, n, r) }

final def node(ref: String*): Option[Node] = if (ref.isEmpty) Some(this) else
val it = ref.iterator

Expand Down
21 changes: 20 additions & 1 deletion test/src/test/scala/borscht/CfgNodeTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package borscht

import borscht.parsers.NodeParserString
import borscht.test.cfg
import borscht.test.*
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand Down Expand Up @@ -33,3 +33,22 @@ class CfgNodeTest extends AnyFlatSpec with Matchers:
it should "throw a NodeParserException if there are multiple keys from the map" in {
a[NodeParserException] should be thrownBy cfg("key1" -> "value", "key2" -> "value").oneOf(OneOfMap)
}

"properties" should "return empty map for unexistent node" in {
cfg().properties("unexistent") shouldBe empty
}

it should "return a correct map" in {
cfg("key1" ->
cfg("key2" ->
cfg("cfg" -> cfg(
"seq" -> seq(1, 2, 3),
"scalar1" -> "value1",
"scalar2" -> "value2",
"empty" -> cfg())))).properties("key1", "key2") shouldEqual Map(
"key1.key2.cfg.seq.0" -> "1",
"key1.key2.cfg.seq.1" -> "2",
"key1.key2.cfg.seq.2" -> "3",
"key1.key2.cfg.scalar1" -> "value1",
"key1.key2.cfg.scalar2" -> "value2")
}
5 changes: 3 additions & 2 deletions test/src/test/scala/borscht/typed/types/TimeTypesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ class TimeTypesTest extends AnyFlatSpec with Matchers:

it should "be parsed with adjuster and time zone" in {
val zone = "Europe/London"
val before = ZonedDateTime.now.minusMonths(1)
val zoneId = ZoneId.of(zone)
val before = ZonedDateTime.now(zoneId).minusMonths(1)
val now = RefTypeNow(zone).parser(Nil) map (_(scalar("-0000-01-00")))
now match
case Right(ref: Ref[?]) =>
val value = ref.cast[ZonedDateTime].value
val after = ZonedDateTime.now.minusMonths(1)
val after = ZonedDateTime.now(zoneId).minusMonths(1)
value.getZone shouldBe ZoneId.of(zone)
value should (be >= before and be <= after)
case _ => fail("Unexpected result $now")
Expand Down

0 comments on commit d58495f

Please sign in to comment.