From 160f8dbc2221176a5c80d1c7a66c2198371a2857 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Sat, 23 Dec 2023 08:41:02 +1100 Subject: [PATCH] Close #150 - [refined4s-pureconfig] Add type name to the error message of PureconfigRefinedConfigReader and refined4s.modules.pureconfig.derivation.instances.derivedRefinedConfigReader --- build.sbt | 1 + .../pureconfig/derivation/instances.scala | 17 ++++++++++------- .../pureconfig/derivation/instancesSpec.scala | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/build.sbt b/build.sbt index b2559a4..cf37778 100644 --- a/build.sbt +++ b/build.sbt @@ -197,6 +197,7 @@ lazy val props = val RepoName = GitHubRepo.fold("refined4s")(_.nameToString) val Scala3Version = "3.1.3" +// val Scala3Version = "3.3.1" val ProjectScalaVersion = Scala3Version diff --git a/modules/refined4s-pureconfig/shared/src/main/scala/refined4s/modules/pureconfig/derivation/instances.scala b/modules/refined4s-pureconfig/shared/src/main/scala/refined4s/modules/pureconfig/derivation/instances.scala index f5da934..3b04cec 100644 --- a/modules/refined4s-pureconfig/shared/src/main/scala/refined4s/modules/pureconfig/derivation/instances.scala +++ b/modules/refined4s-pureconfig/shared/src/main/scala/refined4s/modules/pureconfig/derivation/instances.scala @@ -1,5 +1,6 @@ package refined4s.modules.pureconfig.derivation +import com.typesafe.config.ConfigValue import pureconfig.error.UserValidationFailed import pureconfig.{ConfigReader, ConfigWriter} import refined4s.* @@ -9,24 +10,26 @@ import refined4s.* */ trait instances { - given derivedNewtypeConfigReader[A, B](using coercible: Coercible[A, B], configReader: ConfigReader[A]): ConfigReader[B] = + inline given derivedNewtypeConfigReader[A, B](using coercible: Coercible[A, B], configReader: ConfigReader[A]): ConfigReader[B] = Coercible.unsafeWrapM(configReader) -// import refined4s.internal.typeTools.* + import refined4s.internal.typeTools.* @SuppressWarnings(Array("org.wartremover.warts.ToString")) - given derivedRefinedConfigReader[A, B](using refinedCtor: RefinedCtor[B, A], configReader: ConfigReader[A]): ConfigReader[B] = + inline given derivedRefinedConfigReader[A, B](using refinedCtor: RefinedCtor[B, A], configReader: ConfigReader[A]): ConfigReader[B] = configReader.emap { a => refinedCtor.create(a).left.map { err => -// val expectedType = getTypeName[B] + val expectedType = getTypeName[B] UserValidationFailed( - s"Invalid value found: ${a.toString} with error: $err" + s"The value ${a.toString} cannot be created as the expected type, $expectedType, due to the following error: $err" ) } } - given derivedConfigWriter[A, B](using coercible: Coercible[A, B], configWriter: ConfigWriter[B]): ConfigWriter[A] = - a => configWriter.to(coercible(a)) + inline given derivedConfigWriter[A, B](using coercible: Coercible[A, B], configWriter: ConfigWriter[B]): ConfigWriter[A] with { + override inline def to(a: A): ConfigValue = + configWriter.to(coercible(a)) + } } object instances extends instances diff --git a/modules/refined4s-pureconfig/shared/src/test/scala/refined4s/modules/pureconfig/derivation/instancesSpec.scala b/modules/refined4s-pureconfig/shared/src/test/scala/refined4s/modules/pureconfig/derivation/instancesSpec.scala index c8e49cd..98fec2a 100644 --- a/modules/refined4s-pureconfig/shared/src/test/scala/refined4s/modules/pureconfig/derivation/instancesSpec.scala +++ b/modules/refined4s-pureconfig/shared/src/test/scala/refined4s/modules/pureconfig/derivation/instancesSpec.scala @@ -78,7 +78,9 @@ object instancesSpec extends Properties { |} |""".stripMargin - val expected = s"Invalid value found: ${id.toString} with error: Invalid value: [${id.toString}]. It must be a positive Long" + val expected = + s"The value ${id.toString} cannot be created as the expected type, ${Id.getClass.getName.replace("$", ".")}Type, due to the following error: " + + s"Invalid value: [${id.toString}]. It must be a positive Long" ConfigSource .string(confString)