Created
October 4, 2018 19:20
-
-
Save stephen-lazaro/cfa8556b56518c6a6eb30e1cc9751f6a 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
// "Sum up" all the decoders in a list! | |
def altSum[A](patterns: List[RowDecoder[A]]): RowDecoder[A] = | |
patterns.foldLeft(Alternative[RowDecoder].empty[A])(_ <+> _) | |
// Four different patterns! | |
val variableDateDecoder: RowDecoder[Date] = | |
altSum( | |
List( | |
withSlashes, | |
withDashes, | |
RowDecoder.from(x => Either.catchNonFatal( | |
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss").parse(x))), | |
RowDecoder.from(x => Either.catchNonFatal( | |
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh").parse(x))) | |
) | |
) | |
// Putting it all together… | |
implicit val loanDecoder: RowDecoder[LoanWithPaymentDate] = | |
map4( | |
decodeId, | |
decodeBalance, | |
decodeLoanState, | |
variableDateDecoder | |
)(LoanWithPaymentDate.apply) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment