arainko / ducktape

Automatic and customizable compile time transformations between similar case classes and sealed traits/enums, essentially a thing that glues your code. Scala 3 only. Or is it duct 🤔
https://arainko.github.io/ducktape/
Other
405 stars 7 forks source link

Make configurations more lens-like #179

Closed arainko closed 2 months ago

arainko commented 3 months ago

Right now if we try to do this:

case class Person(age: Int, name: String)

Person(1, "Joe").into[Person].transform(Field.const(_.age, 23))

we'd be greeted with a pretty cryptic error due to the fact that underneath the covers the plan derived for a Person to Person transformation is actually just an identity (i.e. ducktape doesn't create a new Person from the ground up since that'd be wasteful).

We could actually support that use case if we were to derive a lazy 'alternative' plan when encountering an identity transformation which could then be used when traversing the plan in PlanConfigurer which would basically replace the identity transformation with the fallback plan.

arainko commented 2 months ago

More examples that should work:

case class Level1(level2: Level2)

case class Level1Dest(level2: Level2)

case class Level2(level3: Level3)

case class Level3(int: Int)

        level
          .into[Level1Dest]
          .transform(
            Field.const(_.level2.level3.int, 1)
          )