Inheriting a Transformer from a supertype
#776
-
|
I'm currently working on a project that's rather convoluted and involves quite deeply nested subtyping hierarchies. So for example, I might have to work with types None of the transformers can be fully derived - I may have, for example, to rename In my current implementation, I need to do the Is there a way of factorizing that? Of declaring that all subtypes of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I haven't tried tackling such case, but there is one thing that might potentially help handling it: val transformationInProcess = foo.into[Bar]
.withFieldRenamed(_.a1, _.a2)
transformationInProcess
.withFieldConst(_.a2, value)
.transform
transformationInProcess
.withFieldComputed(_.a2, foo => ...)
.transformThe chaining, at the same time:
so splitting the transformation into parts is possible, as long as the type-level config is preserved. I guess such a refactoring as what you think about might be more tricky, but I believe something like: // Example with Transformer here because you probably want to reuse
// but works with inlined transformations as well
// Let the type be inferred
def sharedFields[Str <: A1, Tgt <: A2] = Transformer.define[Src, Tgt]
.withFieldRenamed(_.a1, _.a2)
import io.scalaland.chimney.internal.runtime.{
TransformerFlags,
TransformerOverrides,
}
def specializedForB[Overrides <: TransformerOverrides, Flags <: TransformerFlags](
shared: TransformerDefinition[B1, B2, Overrides, Flags]
) = shared
.withFieldRenamed(_.b1, _.b2)
sharedFields[B1, B2]
.pipe(specializedForB)
// maybe some more overrides
.buildTransformershould do the trick. If DSL only needs to know type enough to see that there is |
Beta Was this translation helpful? Give feedback.
-
|
I'll play with it a little more, try to refine it, but the "in process" pattern was just confirmed to work just fine. It's not the direction I was looking into, but it's definitely a good answer, thank you! I'm not entirely sure what the sharedFields1[B1, B2]
.withFieldRenamed(_.b1, _.b2)
.buildTransformerAm I missing something subtle? |
Beta Was this translation helpful? Give feedback.
I haven't tried tackling such case, but there is one thing that might potentially help handling it:
The chaining, at the same time:
Vectorso splitting the transformation into parts is possible, as long as the type-level config is preserved.
I guess such a refactoring as what you think about might be more tricky, but I…