Skip to content

Instantly share code, notes, and snippets.

@stephen-lazaro
Created October 4, 2018 19:20
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 stephen-lazaro/cfa8556b56518c6a6eb30e1cc9751f6a to your computer and use it in GitHub Desktop.
Save stephen-lazaro/cfa8556b56518c6a6eb30e1cc9751f6a to your computer and use it in GitHub Desktop.
// "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