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

Question: Transformer from > 1 case classes to another #208

Closed
alexklibisz opened this issue May 3, 2021 · 3 comments
Closed

Question: Transformer from > 1 case classes to another #208

alexklibisz opened this issue May 3, 2021 · 3 comments

Comments

@alexklibisz
Copy link

Hi - first, I've gotten a lot of value from this project. Your work is much appreciated.

My question: is it possible to have a Transformer that combines 2 (or more) case classes into a third case class? I've found this would be particularly useful when working with case classes representing normalized database rows. I often have a FooRow and a BarRow which I want to combine into a BazDto.

@rogern
Copy link

rogern commented May 4, 2022

it would be cool if the patching could be combined with transformation somehow, like:
person.into[Employee].withPatch(companyDetails).transform

@alexklibisz
Copy link
Author

One approach that seems to work is to just pack the source types into a tuple and build a transformer for the tuple:

import io.scalaland.chimney.Transformer
import io.scalaland.chimney.dsl._

final case class Foo(a: String, b: String)
final case class Bar(c: String)
final case class FooBar(a: String, b: String, c: String)

implicit val fooAndBarToFooBar: Transformer[(Foo, Bar), FooBar] = {
    case (foo, bar) =>
      foo
        .into[FooBar]
        .withFieldConst(_.c, bar.c)
        .transform
  }

val f  = Foo("a", "b")
val b  = Bar("c")
val fb = (f, b).transformInto[FooBar]
println(fb)

@MateuszKubuszok
Copy link
Member

It's a duplicate of #115, so all suggestions and ideas ca be posted there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants