Transform wrapped class #441
-
I have a class domain class Let //> using dep io.scalaland::chimney::0.8.3
import io.scalaland.chimney.dsl.*
case class A(a: String)
case class WithB[X](b: String, x: X)
case class C(a: String, b: String)
WithB("b", A("a")).transformInto[C] I understand that Chimney by itself can't (and shouldn't) resolve Changing case class WithB[X](b: String, x: X) to case class WithB[X](b: String, x: X):
export x.* while technically exposing all fields, isn't accepted either. I am relatively new to Scala, please let me know if the above |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Currently the only way to combine values from 2 levels of nesting is:
//> using dep io.scalaland::chimney::0.8.3
import io.scalaland.chimney.dsl.*
case class A(a: String)
case class WithB[X](b: String, x: X)
case class C(a: String, b: String) // I modified this for this example, see below
// WithB[A] as the "main" object
WithB("b", A("a")).into[C].withFieldComputed(_.a, _.x.a).transform
// or A as the "main" object
val withB = WithB("b", A("a"))
withB.x.into[C].withFieldConst(_.b, withB.b).transform Handling it magically OOTB is not in the scope ever (since the compilation time would explode if the macro would try to randomly traverse the whole tree of values until some matching field would be found), but pointing Chimney which 2 values could be (recursively) merged was already requested in #115 and in #208. Then it would be possible to write something like val withB = WithB("b", A("a"))
// From what I understand .x seem to have majority of the fields necessary
withB.x.into[C].withFallbackTo(withB).transform but this functionality is rather complex (since it would have to recursively merge 2 values, not just the first layer) and is unlikely to be implemented anytime soon. Another issue with your code is that //> using dep io.scalaland::chimney::0.8.3
import io.scalaland.chimney.dsl.*
case class A(a: String)
case class WithB[X](b: String, x: X)
case class C(a: String, b: Int)
// PartialTransformer handles None, Exceptions, etc
WithB("b", A("a"))
.intoPartial[C]
.withFieldComputed(_.a, _.x.a)
.withFieldComputed(_.b, _.b.toInt)
.transform
// or
val withB = WithB("b", A("a"))
withB.x.intoPartial[C]
.withFieldComputed(_.b, _ => withB.b.toInt)
.transform I am assuming that this is just the demo, showing the issue, but if there would be real use case with such a ratio of generated code to the required customization I might have converted the code by hand. Ideally, in the future (after #115), if // catches Exceptions from .toInt
implicit val stringToInt: PartialTransformer[String, Int] =
PartialTransformer.fromFunction(str => str.toInt)
val withB = WithB("b", A("a"))
withB.x.intoPartial[C].withFallbackTo(withB).transform but with the current functionalities Chimney requires providing at least |
Beta Was this translation helpful? Give feedback.
Currently the only way to combine values from 2 levels of nesting is:
Handling it magically OOTB is not in the scope ever (since the compilation time …