Skip to content

Commit

Permalink
ConfDecoderEx: allow extending collections
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 22, 2021
1 parent 8204327 commit e28748a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
25 changes: 25 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ for `ConfDecoderExT[A, A]`.
If a decoder for type `T` is defined, the package defines implicits to derive
decoders for `Option[T]`, `Seq[T]` and `Map[String, T]`.

There's also a special for _extending_ collections rather than redefining them
(works only for `ConfDecoderEx`, not the original `ConfDecoder`):
```
// sets list
a = [ ... ]
// sets map
a = {
b { ... }
c { ... }
}
// extends list
a = {
// must be the only key
"+" = [ ... ]
}
// extends map
a = {
// must be the only key
"+" = {
d { ... }
}
}
```

## ConfEncoder

To convert a class instance into `Conf` use `ConfEncoder[T]`. It's possible to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ object ConfDecoderExT {
fromPartial(
s"Map[String, ${classTag.runtimeClass.getName}]"
) {
case (stateOpt, Conf.Obj(List(("+", Conf.Obj(values))))) =>
val res =
buildFrom(none, values, ev, factory)(_._2, (x, y) => (x._1, y))
res.map { x =>
stateOpt.fold(x) { state =>
val builder = factory.newBuilder
builder ++= state
builder ++= x
builder.result()
}
}
case (_, Conf.Obj(values)) =>
buildFrom(none, values, ev, factory)(_._2, (x, y) => (x._1, y))
}
Expand All @@ -136,6 +147,15 @@ object ConfDecoderExT {
fromPartial(
s"List[${classTag.runtimeClass.getName}]"
) {
case (stateOpt, Conf.Obj(List(("+", Conf.Lst(values))))) =>
buildFrom(none, values, ev, factory)(identity, (_, x) => x).map { x =>
stateOpt.fold(x) { state =>
val builder = factory.newBuilder
builder ++= state
builder ++= x
builder.result()
}
}
case (_, Conf.Lst(values)) =>
buildFrom(none, values, ev, factory)(identity, (_, x) => x)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ class DeriveConfDecoderExJVMSuite extends munit.FunSuite {
| k3 { param = 3 }
| }
|}
|d {
| "+" = [{
| b { param = 40 }
| }]
|}
|""".stripMargin,
Nested(
a = 14,
c = Nested2(a = "n2", b = OneParam(4), c = Map("k3" -> OneParam(3))),
d = Seq(
Nested2("n1", OneParam(2), Map("k1" -> OneParam(1)))
Nested2("n1", OneParam(2), Map("k1" -> OneParam(1))),
Nested2(b = OneParam(40))
)
),
Nested(
Expand All @@ -54,6 +60,11 @@ class DeriveConfDecoderExJVMSuite extends munit.FunSuite {
| a = "xxx"
| b {
| b { param = 3 }
| c {
| "+" = {
| k3 { param = 33 }
| }
| }
| }
|}
|""".stripMargin,
Expand All @@ -63,7 +74,7 @@ class DeriveConfDecoderExJVMSuite extends munit.FunSuite {
b = Nested2(
a = "zzz",
b = OneParam(3),
c = Map("k2" -> OneParam(2))
c = Map("k2" -> OneParam(2), "k3" -> OneParam(33))
)
)
),
Expand Down

0 comments on commit e28748a

Please sign in to comment.