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

[Issue 199] Add `Field.computedDeep` and `Field.fallibleComputedDeep` #220

Closed arainko closed 1 week ago

arainko commented 1 week ago

Closes #199

This adds two new config options that allow to computing deeply nested fields (with the value that corresponds to the source value under the config path). Example:

 case class SourceToplevel1(level1: Option[SourceLevel1])
    case class SourceLevel1(level2: Option[SourceLevel2])
    case class SourceLevel2(level3: SourceLevel3)
    case class SourceLevel3(int: Int)

    case class DestToplevel1(level1: Option[DestLevel1])
    case class DestLevel1(level2: Option[DestLevel2])
    case class DestLevel2(level3: Option[DestLevel3])
    case class DestLevel3(int: Long)

    val source = SourceToplevel1(Some(SourceLevel1(Some(SourceLevel2(SourceLevel3(1))))))
    val expected = DestToplevel1(Some(DestLevel1(Some(DestLevel2(Some(DestLevel3(11)))))))

    assertTransformsConfigured(source, expected)(
      Field.computedDeep(_.level1.element.level2.element.level3.element.int, (int: Int) => int.toLong + 10)
    )