-
-
Notifications
You must be signed in to change notification settings - Fork 110
Description
Checklist
- I read the documentation at https://chimney.readthedocs.io/ and checked that the functionality doesn't exist
- I checked the https://github.com/scalalandio/chimney/issues and haven't found the feature already requested reported
- I confirmed that the request is not related to functionality that was deprecated: lifted transformers (
TransformerFs) orunsafeOptionflags - I believe that this is actual feature request, rather than a question how to use existing features
- I am aware of the maintainer's announcement and I understand, that this might only be implemented
if someone from the community decide to own the feature and see it through
Describe the desired behavior
I came across a case where I wanted to transform case class objects with camelCase member names into case class objects with snake case member names.
After researching a bit I came across this discussion and the related PR: #89
The related PR pointed me to the docs: https://chimney.readthedocs.io/en/stable/supported-transformations/#defining-custom-name-matching-predicate
With that, I implemented a mapper which works well enough:
case object CamelSnakeCaseEquality extends TransformedNamesComparison {
def namesMatch(fromName: String, toName: String): Boolean = {
val snakeCase = fromName
.flatMap { c =>
if (c.isUpper) Seq('_', c.toLower)
else Seq(c)
}
.mkString
snakeCase == toName
}
}Unless any of the existing extensions of TransformedNamesComparison already provide this functionality (which I could not find), I imagine this to be a common enough case to come with chimney out of the box.
Any opinions on this?
PS: I'd be happy to raise a PR for this.
Use case example
case class UserDAO(userId, createdAt)
case class UserDTO(user_id, created_at)
UserDAO(123, "2024-12-03T12:00:00")
.into[UserDTO]
.enableCustomFieldNameComparison(
CamelSnakeCaseEquality
)
.transform
How it relates to existing features
Yes, as stated above, this can be (self) implemented using existing functionality.
Additional context
Add any other context about the problem here.