Last active
August 29, 2015 14:04
-
-
Save bigjason/c7d8961bb69ebd153667 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
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