Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add automatic field renaming according to custom function or popular conventions #89

Closed
kmikulski opened this issue Sep 22, 2018 · 2 comments · Fixed by #478
Closed
Labels
enhancement relatively easy but not small Task with requires some knowlege and writing more code, but not the intimate knoledge about macros

Comments

@kmikulski
Copy link
Contributor

It might be useful to consider adding a method that automatically renames all fields of a class according to provided f: String => String, something along these lines:

case class MyDTO(my_string_field: String, my_int_field: Int)
case class MyEntity(myStringField: String, myIntField: Int)

def rename(s: String) = {
  val words = s.split("_").toList
  (words.head :: words.tail.map(_.capitalize)).mkString
}

MyDTO("abc", 1).into[MyEntity].withFieldsAutorenamed(rename).transform

If running a user-provided function in compile time turns out to be impossible or we deem it too dangerous, perhaps we could support transitions between the common case styles?

@krzemin
Copy link
Member

krzemin commented Nov 21, 2018

The problem with function provided by the user is that either (1) we sacrifice compile-time type safety and run the function in runtime allowing for runtime exceptions, or (2) we must be somehow able to run this function in compile time.

From my perspective (1) is no-go zone as it would diminish strong guarantees provided by the library. When it comes to (2), it doesn't either seem to be technically possible as it would require taking a program that is being compiled and run it during the compilation.

What we could do instead is to provide builtin support for camelCase and snake_case naming styles - something like:

MyDTO("abc", 1)
  .into[MyEntity]
  .withNamingConvention(Naming.SnakeCase, Naming.CamelCase)
  .transform

I'm keen to accept PRs that provides support for such conventions.

enhan added a commit to enhan/chimney that referenced this issue Apr 13, 2020
ghostbuster91 pushed a commit to ghostbuster91/chimney that referenced this issue Oct 25, 2020
@MateuszKubuszok MateuszKubuszok added dragons ahead Task which requires handwriting compiletime reflection for Scala2&3 and/or updating the architecture relatively easy but not small Task with requires some knowlege and writing more code, but not the intimate knoledge about macros and removed dragons ahead Task which requires handwriting compiletime reflection for Scala2&3 and/or updating the architecture labels Oct 20, 2023
@MateuszKubuszok MateuszKubuszok linked a pull request Mar 13, 2024 that will close this issue
17 tasks
@MateuszKubuszok
Copy link
Member

Added in 1.0.0-M3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement relatively easy but not small Task with requires some knowlege and writing more code, but not the intimate knoledge about macros
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants