Skip to content

Instantly share code, notes, and snippets.

@tabdulradi
Created January 21, 2020 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabdulradi/ed1bf3f3c90e051b56ae49330a2635a4 to your computer and use it in GitHub Desktop.
Save tabdulradi/ed1bf3f3c90e051b56ae49330a2635a4 to your computer and use it in GitHub Desktop.
object TapirRefined extends TapirRefinedLowPriority {
implicit def posIntsCodecs[CF <: CodecFormat, R](implicit codec: Codec[Int, CF, R]): Codec[PosInt, CF, R] =
refineCodec(codec.validate(Validator.min(0)))
}
trait TapirRefinedLowPriority {
protected def refineCodec[T, CF <: CodecFormat, R, P](
codec: Codec[T, CF, R]
)(implicit refine: Validate[T, P]): Codec[T Refined P, CF, R] =
codec.mapDecode(
a => refineV[P](a).fold[DecodeResult[T Refined P]](
e => DecodeResult.Error(e, new Exception),
DecodeResult.Value.apply
)
)(_.value)
implicit def fallbackInstance[T, CF <: CodecFormat, R, P](implicit codec: Codec[T, CF, R], refine: Validate[T, P]): Codec[T Refined P, CF, R] =
refineCodec(codec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment