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

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

At least to me it seems strange that the following code succeeds with an empty chunk.

object Example extends ZIOAppDefault {

  val configProvider =
    ConfigProvider.fromMap(???)

  val config =
    Config.chunkOf(Config.string)

  val run =
    configProvider.load(config)

This code doesn't even really make sense because the ConfigProvider.fromMap and in fact any flat ConfigProvider consists of key value pairs and the Config is attempting to load a value without specifying a key. So there is no conceivable input for Config.fromMap in which this would load any information.

Even when we do specify a configuration that makes sense here, we get what I think are somewhat counterintuitive results:

object Example extends ZIOAppDefault {

  val a = Map.empty[String, String]
  val b = Map("key" -> "")
  val c = Map("key" -> "value")
  val d = Map("key" -> "value1, value2")

  val config =
    Config.chunkOf("key", Config.string)

  val run =
    for {
      _ <- ConfigProvider.fromMap(a).load(config).debug("a")
      _ <- ConfigProvider.fromMap(b).load(config).debug("b")
      _ <- ConfigProvider.fromMap(c).load(config).debug("c")
      _ <- ConfigProvider.fromMap(d).load(config).debug("d")
    } yield ()
}
[info] a: Chunk()
[info] b: Chunk()
[info] c: Chunk(value)
[info] d: Chunk(value1,value2)

This is definitely debatable but it seems questionable whether we should be succeeding with an empty chunk if the key doesn't exist. In particular, it seems inconsistent that attempting to load a chunk in a map where the key doesn't exist succeeds with an empty chunk but attempting to load something else like a string in a map where the key doesn't exist will fail. You could say that the chunk has a natural "empty" element which other types don't but I think we need consistent behavior across data types.

So I would expect a to fail with a Config.Error instead of succeeding with an empty chunk.

This PR changes this behavior so that the first example and a both fail. So you can still have empty sequences but you at least need the key to be present. If you want the current behavior of succeeding with an empty chunk if the key is not present you could do chunkOf("key", Config.string).withDefault(Chunk.empty).

@jdegoes jdegoes merged commit a8a18ce into zio:series/2.x Jan 13, 2023
@adamgfraser adamgfraser deleted the sequence branch January 16, 2023 14:47
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.

Config.Sequence with a fallback value always returns the empty Chunk

3 participants