Skip to content

Commit 398ce3a

Browse files
Yann MoisanMateuszKubuszok
andcommitted
Add TransformedNamesComparison.IgnorePrefix name comparison
Adds a base `IgnorePrefix(prefix)` comparison that matches a source name to a target name after stripping a fixed prefix from the source side. This is common for code-generated types such as Protobuf enums whose variants repeat the enum name as a prefix. `IgnorePrefix` is intentionally not `sealed` so downstream code can extend it (`case object Foo extends TransformedNamesComparison.IgnorePrefix("Foo")`), which is the documented usage; a test object defined in another file guards this. Ships with unit tests and a user-facing entry in supported-transformations.md. Co-authored-by: Mateusz Kubuszok <mateusz.kubuszok@gmail.com>
1 parent ca63484 commit 398ce3a

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

chimney/src/main/scala/io/scalaland/chimney/dsl/TransformedNamesComparison.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ object TransformedNamesComparison {
7676
}
7777
}
7878

79+
/** Matches names after stripping a fixed prefix from the source name. */
80+
abstract class IgnorePrefix(prefix: String) extends TransformedNamesComparison { this: Singleton =>
81+
82+
def namesMatch(fromName: String, toName: String): Boolean = fromName == prefix + toName
83+
}
84+
7985
type FieldDefault = BeanAware.type
8086
val FieldDefault: FieldDefault = BeanAware
8187

chimney/src/test/scala/io/scalaland/chimney/TransformedNamesComparisonSpec.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,28 @@ class TransformedNamesComparisonSpec extends ChimneySpec {
110110
}
111111

112112
}
113+
114+
group("TransformedNamesComparison.IgnorePrefix") {
115+
116+
test("should match when the source name equals the prefix followed by the target name") {
117+
IgnoreFooPrefix.namesMatch("FooBar", "Bar") ==> true
118+
IgnoreFooPrefix.namesMatch("FooBaz", "Baz") ==> true
119+
}
120+
121+
test("should match identical names only when the prefix is empty") {
122+
IgnoreFooPrefix.namesMatch("Bar", "Bar") ==> false
123+
IgnoreFooPrefix.namesMatch("Foo", "Foo") ==> false
124+
}
125+
126+
test("should not match when the prefix is missing or the names differ") {
127+
IgnoreFooPrefix.namesMatch("Bar", "FooBar") ==> false
128+
IgnoreFooPrefix.namesMatch("BarFoo", "Bar") ==> false
129+
IgnoreFooPrefix.namesMatch("FooBar", "Baz") ==> false
130+
}
131+
}
113132
}
133+
134+
/** Extended outside of `TransformedNamesComparison` to make sure `IgnorePrefix` is not `sealed` and can be reused by
135+
* downstream code, as documented.
136+
*/
137+
case object IgnoreFooPrefix extends TransformedNamesComparison.IgnorePrefix("Foo")

docs/docs/supported-transformations.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6227,8 +6227,13 @@ Arguments taken by both `.enableCustomFieldNameComparison` and `.enableCustomSub
62276227
`true`
62286228
- `TransformedNamesComparison.CamelSnakeCaseEquality` - 2 names are considered equal if they are identical `String`s OR if they are
62296229
identical after you convert them from snake to camel case (e.g. `snake_case` -> `snakeCase`, `camelCase` -> `camelCase`)
6230+
- `TransformedNamesComparison.IgnorePrefix` - a base class (rather than a ready-to-use value) that you extend with a fixed
6231+
prefix, e.g. `case object FooNamesComparison extends TransformedNamesComparison.IgnorePrefix("Foo")`. The source name
6232+
matches the target name if it equals the prefix followed by the target name (e.g. `FooBar` matches `Bar`). This is handy for
6233+
code-generated types such as Protobuf enums whose variants
6234+
[repeat the enum name as a prefix](https://buf.build/blog/totw-3-enum-names-need-prefixes)
62306235

6231-
However, these 4 do not exhaust all possible comparisons, and you might need to provide one yourself.
6236+
However, these 5 do not exhaust all possible comparisons, and you might need to provide one yourself.
62326237

62336238
!!! warning
62346239

0 commit comments

Comments
 (0)