Skip to content

Instantly share code, notes, and snippets.

@bigjason
Last active August 29, 2015 14:04
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 bigjason/c7d8961bb69ebd153667 to your computer and use it in GitHub Desktop.
Save bigjason/c7d8961bb69ebd153667 to your computer and use it in GitHub Desktop.
def save[A](key: String)(f: CouchValueHandler[A])(implicit formatter: CouchFormat[A], context: CouchContext): Future[A] =
get[A](key).flatMap {
case Some(row) =>
update[A](key, row)(value => f(value.some))
case None =>
val value = f(None)
add(key, value) flatMap {
case false => // Failure to add means there is now a value, start the process over.
save(key)(f)
case true =>
Future.successful(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment