refined
The vulcan-refined module provides Codecs for refined refinement types.
Getting Started
To get started with sbt, add the following line to your build.sbt file.
libraryDependencies += "com.github.fd4s" %% "vulcan-refined" % "1.14.1"
Usage
Refinement types are encoded using their base type (e.g. Int for PosInt). When decoding, Codecs check to ensure values conform to the predicate of the refinement type (e.g. Positive for PosInt), and raise an error for values which do not conform.
import eu.timepit.refined.auto._
import eu.timepit.refined.types.numeric.PosInt
import vulcan.Codec
import vulcan.refined._
Codec[PosInt]
// res1: Codec[eu.timepit.refined.api.Refined[Int, eu.timepit.refined.numeric.Positive]] = ImapErrors(
// codec = Codec("int"),
// f = vulcan.refined.package$$$Lambda/0x00007f9f060961a0@45884a8a,
// g = vulcan.Codec$$Lambda/0x00007f9f05f357a0@5b566a0e
// )
Codec.encode[PosInt](1)
// res2: Either[vulcan.AvroError, Codec[eu.timepit.refined.api.Refined[Int, eu.timepit.refined.numeric.Positive]]#AvroType] = Right(
// value = 1
// )
Codec.decode[PosInt](0)
// res3: Either[vulcan.AvroError, PosInt] = Left(
// value = AvroError(Predicate failed: (0 > 0).)
// )