zio-config icon indicating copy to clipboard operation
zio-config copied to clipboard

mapFieldName doesn't work

Open mygoalistokillbill opened this issue 3 years ago • 2 comments

zio-config verison is 1.0.6

second assertion fails

link to javadoc

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import zio.config.read
import zio.config.magnolia.DeriveConfigDescriptor
import zio.config.typesafe.TypesafeConfigSource

sealed trait Credentials

object Credentials {
  final case class UsernamePassword(
    username: String,
    password: String
  ) extends Credentials

  final case class Token(
    username: String,
    tokenId:  String
  ) extends Credentials
}

final case class MyConfig(auth: Credentials)

class AppConfigTest extends AnyFlatSpec with Matchers {
  "example" should "work" in {
    val customDerivation = new DeriveConfigDescriptor {
      override def mapFieldName(name: String): String = name.toUpperCase
    }

    val config =
      """{
        |auth : {
        |  UsernamePassword : {
        |     username : xyz
        |     password : abc
        |  }
        |}
        |}
        |""".stripMargin

    TypesafeConfigSource
      .fromHoconString(config)
      .flatMap(source => read(customDerivation.descriptor[MyConfig].from(source)))
      .isInstanceOf[Left[_, _]] shouldEqual false

    val config2 =
      """{
        |auth : {
        |  UsernamePassword : {
        |     USERNAME : xyz
        |     PASSWORD : abc
        |  }
        |}
        |}
        |""".stripMargin

    TypesafeConfigSource
      .fromHoconString(config2)
      .flatMap(source => read(customDerivation.descriptor[MyConfig].from(source)))
      .isInstanceOf[Left[_, _]] shouldEqual false //fails
  }
}

error

org.scalatest.exceptions.TestFailedException: The Either on which value was invoked was not defined as a Right; it was Left(ReadError:
╥
╠══╦══╗
║  ║  ║
║  ║  ╠─MissingValue
║  ║  ║ path: auth.Token.tokenId
║  ║  ║ Details: value of type string
║  ║  ▼
║  ║
║  ╠─MissingValue
║  ║ path: auth.Token.username
║  ║ Details: value of type string
║  ▼
║
╠══╦══╗
║  ║  ║
║  ║  ╠─MissingValue
║  ║  ║ path: auth.UsernamePassword.password
║  ║  ║ Details: value of type string
║  ║  ▼
║  ║
║  ╠─MissingValue
║  ║ path: auth.UsernamePassword.username
║  ║ Details: value of type string
║  ▼
▼).

mygoalistokillbill avatar Jul 30 '21 17:07 mygoalistokillbill

Not sure it's a bug, from DeriveConfigDescription.descriptor[T] docs

   * `descriptor[MyConfig]` works only if all the types that forms `MyConfig` has an instance of `Descriptor`.
   * For almost all the important types, zio-config-magnolia already provides implicit instances for `Descriptor`.

In this example, providing an implicit descriptor for Credentials trait should be enough to make it work

implicit def credentialsDescriptor: Descriptor[Credentials] = customDerivation.getDescriptor[Credentials]

Don't really know if there is another way to automatically infer this ....

jgoday avatar Aug 31 '21 20:08 jgoday

@mygoalistokillbill I hear the issue. Will take a look once free from rest of the work that I am into. Sorry for the late response.

afsalthaj avatar Sep 02 '21 08:09 afsalthaj