Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@name annotation should be used if there's a naming style collision #1365

Open
GeorgeII opened this issue Mar 2, 2024 · 0 comments
Open

Comments

@GeorgeII
Copy link

GeorgeII commented Mar 2, 2024

Hi!

Let's say I have a config where everything is in kebab case except for one substructure (bar in the example below):

foo {
  foo-foo = "value1"
}
another-foo {
  nested-another-foo = "value2"
}
bar {
  bArBaR-Bar = "value3"
}

I wrote case classes to describe these entities:

import zio.{Config, Scope, ZIO, ZIOAppArgs, ZIOAppDefault}
import zio.config._
import zio.config.magnolia._
import zio.config.typesafe.TypesafeConfigProvider

object ZioConfig extends ZIOAppDefault {
  case class Foo(fooFoo: String)
  case class AnotherFoo(nestedAnotherFoo: String)
  case class Bar(@name("bArBaR-Bar") barBarBar: String)

  case class MyConfig(foo: Foo, anotherFoo: AnotherFoo, bar: Bar)

  val myConfigAutomatic: Config[MyConfig] = deriveConfig[MyConfig].mapKey(toKebabCase)

  override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = {
    read(myConfigAutomatic.from(TypesafeConfigProvider.fromResourcePath())).orDie
      .tap(conf => ZIO.succeed(println(conf)))
  }
}

I need everything in kebab case except for the barBarBar field that was explicitly marked with the @name annotation in my case class. It seems like the @name annotation does not work as expected as it's overridden by .mapKey(toKebabCase) and I get the exception:

zio.Config$Error$MissingData: (Missing data at bar.b-ar-ba-r-bar: Expected barb-ar-ba-r-bar to be set in properties)

Probably, the @name annotation should have a higher priority even when mixed with .mapKey(toKebabCase) since a manual field name definition is almost always more important than an automatic one?

@GeorgeII GeorgeII changed the title @name annotation should beused if there's a naming collision @name annotation should be used if there's a naming style collision Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant