Skip to content

Commit

Permalink
Close #150 - [refined4s-pureconfig] Add type name to the error messag…
Browse files Browse the repository at this point in the history
…e of PureconfigRefinedConfigReader and refined4s.modules.pureconfig.derivation.instances.derivedRefinedConfigReader
  • Loading branch information
kevin-lee committed Dec 22, 2023
1 parent a5ae983 commit 160f8db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package refined4s.modules.pureconfig.derivation

import com.typesafe.config.ConfigValue
import pureconfig.error.UserValidationFailed
import pureconfig.{ConfigReader, ConfigWriter}
import refined4s.*
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 160f8db

Please sign in to comment.