Created
January 21, 2020 13:50
-
-
Save tabdulradi/ed1bf3f3c90e051b56ae49330a2635a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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