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

orElse on ConfigSource does not work properly with empty collection #1336

Open
strokyl opened this issue Dec 22, 2023 · 2 comments
Open

orElse on ConfigSource does not work properly with empty collection #1336

strokyl opened this issue Dec 22, 2023 · 2 comments

Comments

@strokyl
Copy link

strokyl commented Dec 22, 2023

Here is a test:

import zio.Scope
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assertTrue}
import zio.config.{ConfigDescriptor, ConfigSource, read}
import zio.config.ConfigDescriptor.{list, string}
import zio.config.yaml.YamlConfigSource

object ConfigSpec extends ZIOSpecDefault {
  override def spec: Spec[TestEnvironment with Scope, Any] = suite("spec")(
    test("work") {
      val listDescriptor: ConfigDescriptor[List[String]] = list("some_data")(string)
      val source                                         = YamlConfigSource.fromYamlString("some_data: []")

      for {
        result <- read[List[String]](listDescriptor from source)
      } yield assertTrue(result == List.empty)
    },
    test("does not work but should") {
      val listDescriptor: ConfigDescriptor[List[String]] = list("some_data")(string)
      val source                                         = YamlConfigSource.fromYamlString("some_data: []")

      val secondarySource = ConfigSource.fromMap(Map.empty)
      for {
        result <- read[List[String]](listDescriptor from (source orElse secondarySource))
      } yield assertTrue(result == List.empty)
    },
    test("work (2)") {
      val listDescriptor: ConfigDescriptor[List[String]] = list("some_data")(string)
      val source                                         = YamlConfigSource.fromYamlString("""some_data: ["a"]""")

      val secondarySource = ConfigSource.fromMap(Map.empty)
      for {
        result <- read[List[String]](listDescriptor from (source orElse secondarySource))
      } yield assertTrue(result == List("a"))
    }
  )
}

Here is the output:

+ spec
  + work (2)
  + work
  - does not work but should
    Exception in thread "zio-fiber-130" zio.config.ReadError$MissingValue: ReadError:
    MissingValue
    path: some_data
    	at zio.config.ReadModule.read.loopSequence(ReadModule.scala:288)
    	at zio.config.ReadModule.read.loopSequence(ReadModule.scala:285)
    	at zio.config.ReadModule.read.loopAny(ReadModule.scala:305)
    	at zio.config.ReadModule.read(ReadModule.scala:354)
    	at zio.config.ReadModule.read(ReadModule.scala:355)
    	at zio.config.ReadModule.read(ReadModule.scala:352)
    	at io.conduktor.admin.license.onpremise.ConfigSpec.spec(ConfigSpec.scala:25)
    	at io.conduktor.admin.license.onpremise.ConfigSpec.spec(ConfigSpec.scala:19)
2 tests passed. 1 tests failed. 0 tests ignored.


  - spec - does not work but should
    Exception in thread "zio-fiber-130" zio.config.ReadError$MissingValue: ReadError:
    MissingValue
    path: some_data
    	at zio.config.ReadModule.read.loopSequence(ReadModule.scala:288)
    	at zio.config.ReadModule.read.loopSequence(ReadModule.scala:285)
    	at zio.config.ReadModule.read.loopAny(ReadModule.scala:305)
    	at zio.config.ReadModule.read(ReadModule.scala:354)
    	at zio.config.ReadModule.read(ReadModule.scala:355)
    	at zio.config.ReadModule.read(ReadModule.scala:352)
    	at io.conduktor.admin.license.onpremise.ConfigSpec.spec(ConfigSpec.scala:25)
    	at io.conduktor.admin.license.onpremise.ConfigSpec.spec(ConfigSpec.scala:19)
Executed in 257 ms
[info] Completed tests
[error] Failed tests:
[error] 	io.conduktor.admin.license.onpremise.ConfigSpec
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 22 déc. 2023, 09:43:11

I would expect that when the first datasource have an empty list for the given path and there is nothing on the second datasource, we get an empty list and not an error.

@IvanFinochenko
Copy link
Contributor

Hi
If we look implementation of orElse then we see that secondary ConfigSource will be used if we get exception while parsing config from first ConfigSource. And now ConfigSource is renamed to ConfigProvider.
I consider that it's correct behaviour.
For your case, we should create, for example, a merge method.

@strokyl
Copy link
Author

strokyl commented Apr 29, 2024

If we look implementation of orElse then we see that secondary ConfigSource will be used if we get exception while parsing config from first ConfigSource

In test("does not work but should") the first ConfigSource does not fail and return a valid result with is an empty list

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

2 participants