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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions core-tests/shared/src/test/scala/zio/ConfigProviderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,46 @@ object ConfigProviderSpec extends ZIOBaseSpec {
result <- configProvider.load(config)
} yield assertTrue(result == List((1, 2), (3, 4)))
} +
test("indexed sequence of multiple products nested") {
val configProvider = ConfigProvider
.fromMap(
Map(
"parent.child.employees[0].age" -> "1",
"parent.child.employees[0].id" -> "2",
"parent.child.employees[1].age" -> "3",
"parent.child.employees[1].id" -> "4"
)
)
.nested("child")
.nested("parent")

val product = Config.int("age").zip(Config.int("id"))
val config = Config.listOf("employees", product)

for {
result <- configProvider.load(config)
} yield assertTrue(result == List((1, 2), (3, 4)))
} +
test("indexed sequence of multiple products unnested") {
val configProvider = ConfigProvider
.fromMap(
Map(
"employees[0].age" -> "1",
"employees[0].id" -> "2",
"employees[1].age" -> "3",
"employees[1].id" -> "4"
)
)
.unnested("parent")
.unnested("child")

val product = Config.int("age").zip(Config.int("id"))
val config = Config.listOf("employees", product).nested("child").nested("parent")

for {
result <- configProvider.load(config)
} yield assertTrue(result == List((1, 2), (3, 4)))
} +
test("indexed sequence of multiple products with missing fields") {
val configProvider = ConfigProvider.fromMap(
Map("employees[0].age" -> "1", "employees[0].id" -> "2", "employees[1].age" -> "3", "employees[1]" -> "4")
Expand Down Expand Up @@ -723,6 +763,44 @@ object ConfigProviderSpec extends ZIOBaseSpec {
result <- configProvider.load(config)
} yield assertTrue(result == Nil)
} +
test("empty list with product nested") {
val configProvider =
ConfigProvider
.fromMap(
Map(
"parent.child.departments" -> "<nil>"
)
)
.nested("child")
.nested("parent")

val member = Config.int("age").zip(Config.string("name"))

val config = Config.listOf("departments", member)

for {
result <- configProvider.load(config)
} yield assertTrue(result == Nil)
} +
test("empty list with product unnested") {
val configProvider =
ConfigProvider
.fromMap(
Map(
"departments" -> "<nil>"
)
)
.unnested("parent")
.unnested("child")

val member = Config.int("age").zip(Config.string("name"))

val config = Config.listOf("departments", member).nested("child").nested("parent")

for {
result <- configProvider.load(config)
} yield assertTrue(result == Nil)
} +
//FIXME: Failing test
test("empty list within indexed list") {
val configProvider =
Expand Down
8 changes: 6 additions & 2 deletions core/shared/src/main/scala/zio/ConfigProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,17 @@ object ConfigProvider {

case Sequence(config) =>
for {
patchedPrefix <- ZIO.fromEither(flat.patch(prefix))
indices <- flat
.enumerateChildren(prefix)
.enumerateChildren(patchedPrefix)
.flatMap(set => indicesFrom(set))

values <-
if (indices.isEmpty) {
returnEmptyListIfValueIsNil(prefix = prefix, continue = loop(_, config, split = true).map(Chunk(_)))
returnEmptyListIfValueIsNil(
prefix = patchedPrefix,
continue = loop(_, config, split = true).map(Chunk(_))
)
} else
ZIO
.foreach(Chunk.fromIterable(indices)) { index =>
Expand Down