Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@afsalthaj
Copy link
Contributor

@afsalthaj afsalthaj commented Feb 13, 2023

Handling indexed sequence as given in the below example code, will allow more backends for ConfigProvider in zio-config.

The implementation is a simplified version of #7798, and it keeps backward compatibility.

object Hello extends App {

  val map =
    Map(
      "employees[0].age" -> "1",
      "employees[0].id" -> "1",
      "employees[1].age" -> "2",
      "employees[1].id" -> "2",
      "employees[2].id" -> "3",

    )

  val provider =
    ConfigProvider.fromMap(map)

  case class Employee(age: Option[Int], id: Int)

    val employee: Config[Employee] =
      Config.int("age").optional.zip(Config.int("id")).map(Employee.tupled)

  case class AppConfig(list1: List[Employee], dummyIds: List[Int])

  val config: Config[AppConfig] =
    Config.listOf("employees", employee).zip(Config.listOf("dummyIds.[0]", Config.int)).map(AppConfig.tupled)

  val pgm =
    provider.load(config)

  val result =
    Unsafe.unsafe(implicit u => zio.Runtime.default.unsafe.run(pgm.either).getOrThrowFiberFailure())

  assert(result == Right(AppConfig(List(Employee(Some(1),1), Employee(Some(2),2), Employee(None,3)),List(4, 6, 6))))

}

@afsalthaj afsalthaj changed the title Handle sequence for indices Handle indexed sequence, and allow more backends for ConfigProvider Feb 13, 2023
@afsalthaj afsalthaj marked this pull request as ready for review February 15, 2023 05:33
adamgfraser
adamgfraser previously approved these changes Feb 15, 2023
Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go when tests are passing!

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

Successfully merging this pull request may close these issues.

2 participants