Skip to content

Commit e88e2d7

Browse files
committed
SectionRenameDecoder: implement partial rename
That is, if the conversion function is non empty and doesn't match, we do not modify the section.
1 parent f94fbb7 commit e88e2d7

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

metaconfig-core/shared/src/main/scala/metaconfig/annotation/Annotations.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ object SectionRename {
100100
)(oldName: String, newName: String): SectionRename =
101101
new SectionRename(oldName, newName, conv)
102102

103+
/** - if conv doesn't match, or returns null, value is not moved at all */
104+
def partial(
105+
conv: PartialFunction[Conf, Conf],
106+
)(oldName: String, newName: String): SectionRename =
107+
apply(oldName, newName, toFunc(conv, (_: Conf) => null))
108+
103109
implicit def fromTuple(obj: (String, String)): SectionRename =
104110
apply(obj._1, obj._2)
105111
}

metaconfig-tests/jvm/src/test/scala/metaconfig/DeriveConfDecoderExJVMSuite.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class DeriveConfDecoderExJVMSuite extends munit.FunSuite {
234234
val nested = generic.deriveDecoderEx[Nested](Nested()).noTypos
235235
.withSectionRenames(
236236
SectionRename
237-
.apply { case Conf.Str("xxx") => Conf.Str("yyy") }("E.A", "c.a"),
237+
.partial { case Conf.Str("xxx") => Conf.Str("yyy") }("E.A", "c.a"),
238238
"E.A" -> "e.a",
239239
)
240240
checkOkStrEx(
@@ -255,9 +255,9 @@ class DeriveConfDecoderExJVMSuite extends munit.FunSuite {
255255
| A = "zzz"
256256
|}
257257
|""".stripMargin,
258-
out = Nested(c = Nested2(a = "zzz")),
258+
out = Nested(e = Nested3(a = "zzz")),
259259
in = Nested(),
260-
convConfStr = "c.a = zzz",
260+
convConfStr = "e.a = zzz",
261261
)
262262
}
263263

0 commit comments

Comments
 (0)