valentiay / phobos

Efficient and expressive XML data-binding library for Scala
Apache License 2.0
20 stars 5 forks source link

`withElementNamesTransformed` conf not work with `renamed` annotation #17

Closed geny200 closed 5 months ago

geny200 commented 6 months ago

Hi, I tried to use the rename annotation with the withElementNamesTransformed config, but I did not get the expected result, instead it turned out that the name specified in the annotation cannot be modified even using the config.

Here is a minimal example:

import phobos.configured.ElementCodecConfig
import phobos.configured.naming.snakeCase
import phobos.derivation.semiauto._
import phobos.encoding._
import phobos.syntax.renamed

case class Bar(
    @renamed("FooBar") fooBar: String,
)

object Bar {
  val config: ElementCodecConfig = ElementCodecConfig.default
    .withElementNamesTransformed(snakeCase)

  implicit val barXmlEncoder: XmlEncoder[Bar] =
    deriveXmlEncoderConfigured[Bar]("Bar", config)
}

object App {
  def main(args: Array[String]): Unit = {
    println(XmlEncoder[Bar].encode(Bar("qwerty")))
    // was <?xml version='1.0' encoding='UTF-8'?><Bar><FooBar>qwerty</FooBar></Bar>
    // but I expect <?xml version='1.0' encoding='UTF-8'?><Bar><foo_bar>qwerty</foo_bar></Bar>
  }
}
valentiay commented 5 months ago

Hi! Thank you for reporting, but this is intended behavior. The transformElementNames setting is only applied to case class field names. The @renamed annotation sets XML element name manually. In my opinion, making it the other way around would make the logic of @renamed more difficult to understand