Skip to content
New issue

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

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

Already on GitHub? # to your account

.kebabCase is not working for array in hocon config #1095

Closed
bilal-fazlani opened this issue Mar 3, 2023 · 5 comments · Fixed by #1099
Closed

.kebabCase is not working for array in hocon config #1095

bilal-fazlani opened this issue Mar 3, 2023 · 5 comments · Fixed by #1099

Comments

@bilal-fazlani
Copy link

bilal-fazlani commented Mar 3, 2023

object TestApp2 extends zio.ZIOAppDefault:

  val config = """arr = ["a", "b", "c"]"""

  case class CombinedArrayValue(arr: List[String])

  object CombinedArrayValue:
    val config: Config[CombinedArrayValue] = deriveConfig[CombinedArrayValue]

  override val bootstrap: ZLayer[Any, Nothing, Unit] =
    Runtime.setConfigProvider(
      TypesafeConfigProvider
          .fromHoconString(config)
          .kebabCase  // <-- problem
    )

  def run = ZIO
    .config(CombinedArrayValue.config)
    .debug("result")
    .exitCode

Output:

<FAIL> result: Fail((Missing data at arr: Expected arr to be set in properties),Stack trace for thread "zio-fiber-4":
        at example.TestApp2.run(TestApp.scala:114)
        at example.TestApp2.run(TestApp.scala:115)
        at example.TestApp2.run(TestApp.scala:116))

If I remove the .kebabCase call, it works.

Output after removing .kebabCase

result: CombinedArrayValue(List(a, b, c))
@bilal-fazlani bilal-fazlani changed the title .kebabCase is not working for custom configs .kebabCase is not working for array in hocon config Mar 3, 2023
@bilal-fazlani
Copy link
Author

zioConfigVersion = "4.0.0-RC10"

@afsalthaj
Copy link
Collaborator

@bilal-fazlani I will take a look. There are some ongoing work back in ZIO to handle a few cases.

@bilal-fazlani
Copy link
Author

Thanks!

@afsalthaj
Copy link
Collaborator

afsalthaj commented Mar 5, 2023

.kebabCase in RC version falls back to ZIO core's Flat and Flat which really couldn't represent the array structure in the above format.

To unblock yourself during the RC versions, you can use the standard methods in zio-config such as mapKey

import zio.config._

CombinedArrayValue.config.mapKey(toKebabCase)) // and avoid .kebabCase in the provider

Solution to this inconsistency is on it's way!

With zio/zio#7823 (merged earlier, but we couldn't use it yet) and zio/zio#7891 (merged recently), zio-config can now depend on latest ZIO to allow your original code to work

@bilal-fazlani
Copy link
Author

@afsalthaj is this available in a published zio and zio-config version?

The below is failing for me with error

zio.Config$Error$MissingData: (Missing data at settings.an-array: 
Expected settingsan-array to be set in properties)
case class Settings(
    anArray: List[String]
)
object Settings {
  val config: Config[Settings] =
    (deriveConfig[Settings]).nested("settings")
}

object TestMain extends ZIOAppDefault {

  override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] =
    Runtime.setConfigProvider(configProvider)

  lazy val configProvider = TypesafeConfigProvider
    .fromHoconString(s"""
  |settings={
  |  an-array = ["a", "b", "c"] 
  |}
  """.stripMargin)
    .kebabCase

  def run = for {
    c <- ZIO.config(Settings.config)
    _ <- Console.printLine(c.toString)
  } yield ()
}

I am using

val zioVersion       = "2.0.10"
val zioConfigVersion = "4.0.0-RC14"

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants