Skip to content

Rename ending with everyItem/matchingSome/etc is not recognized, despite error message listing it as a valid choice #718

@MateuszKubuszok

Description

@MateuszKubuszok

Discussed in #717

Originally posted by elpitero March 26, 2025
Hello
I recently started using chimney and during experiments I found a case where nested renames do not seem to work:

  case class A_inner_inner(fieldA_A: String)
  case class A_inner(fieldA: Option[A_inner_inner])
  case class A(field: List[A_inner])

  case class B_inner_inner(fieldB_B: String)
  case class B_inner(fieldB: Option[B_inner_inner])
  case class B(field: List[B_inner])

  private val transformer = Transformer.define[A, B]
    .withFieldRenamed(_.field.everyItem.fieldA, _.field.everyItem.fieldB)
    .withFieldRenamed(_.field.everyItem.fieldA.matchingSome.fieldA_A, _.field.everyItem.fieldB.matchingSome.fieldB_B)
    .enableMacrosLogging
    .buildTransformer

Compilation of code above fails with error:

 + Attempting expansion of rule ProductToProduct
                          + Resolved A_inner_inner getters: (`fieldA_A`: java.lang.String (ConstructorVal, declared)) and B_inner_inner constructor (`fieldB_B`: java.lang.String (ConstructorParameter, default = None)), using name-based matching
                          + B_inner_inner
                          |   field fieldB_B: renaming from _.matching[scala.Some[A_inner_inner]].value.fieldA_A is not allowed, try selecting only field names from _.field.everyItem.field.everyItem.fieldA.matching[scala.Some[A_inner_inner]].value (_.field.everyItem.field.everyItem.fieldA.matching[scala.Some[A_inner_inner]].value.field1.field2, etc)
                          | 

Notice double field in logs ( names from _.field.everyItem.field.everyItem.fieldA

To make this code compile it is enough to either remove Option from classes definition (and keep double nested withFieldRenamed but without matchingSome) or make fieldA and fieldB have the same name (have single withFieldRenamed).
I have read documentation, but I coundn't found any info about such limitation, but I could have missed something.
Is there any solution for this case?
I am using Scala 2.13.16 and Chimney 1.7.3

Originally posted by MateuszKubuszok March 28, 2025

It's a bug, I believe that here and additional check would have to be made, to check if non-.field-ending path (Option/Either/Collection/Map) is a valid use case (in past it wasn't and they had to be enabled on an allow-list-like fashion).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugErroneous behavior in existing featureslow hanging fruitTask that should be easy to implement - perfect for people that want to start contributing.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions