Skip to content

Instantly share code, notes, and snippets.

@ClaireNeveu
Last active August 29, 2015 14:07
Show Gist options
  • Save ClaireNeveu/5911e1ba9a32caa05ce0 to your computer and use it in GitHub Desktop.
Save ClaireNeveu/5911e1ba9a32caa05ce0 to your computer and use it in GitHub Desktop.
Version.scala
package versioning
case class Version(n : Int)
object Version {
class CurriedVersion private[versioning] (v : Version, lb : Int) {
def and(ub : Int) = v.n >= lb && v.n < ub
}
class CurriedVersion0 private[versioning] (v : Version, lb : Int) {
def <(ub : Int) : Boolean = v.n >= lb && v.n < ub
def <=(ub : Int) : Boolean = v.n >= lb && v.n <= ub
}
implicit class VersionWrapper(val v : Version) extends AnyVal {
def between(lb : Int) : CurriedVersion = new CurriedVersion(v, lb)
def <(ub : Int) : Boolean = v.n < ub
}
implicit class VIntWrapper(val lb : Int) extends AnyVal {
def >(v : Version) : CurriedVersion0 = new CurriedVersion0(v, lb+1)
def >=(v : Version) : CurriedVersion0 = new CurriedVersion0(v, lb)
}
}
package object usage {
class Request(val v : Version)
class Response
object Response extends Response
class Future[T](t : T)
object Future {
def successful[T](t : T) = new Future(t)
}
def versionedResponse(vs : PartialFunction[(Request, Version), Future[Response]])(implicit r : Request) =
vs((r, r.v))
def syncVersionedResponse(vs : PartialFunction[(Request, Version), Response])(implicit r : Request) =
vs((r, r.v))
def versionedResponse2(vs: (Int, Request => Response)*)(cur: Request => Response)(implicit r : Request) = {
val version = r.v
val action = vs.find(_._1 >= version.n).map(_._2).getOrElse(cur)
action(r)
}
def versionedResponse3(vs: (Int, Request => Response, String)*)(cur: Request => Response)(implicit r : Request) = {
val version = r.v
val action = vs.find(_._1 >= version.n).map(_._2).getOrElse(cur)
action(r)
}
implicit val request = new Request(Version(5))
/*
def exampleControllerOne = versionedResponse {
case (r, v) if 1 >= v < 3 =>
println("Using deprecated #1")
Future.successful(Response)
case (r, v) if 3 >= v < 8 =>
println("Using deprecated #2")
Future.successful(Response)
case (r, v) =>
println("Using Current")
Future.successful(Response)
}
def exampleControllerTwo = syncVersionedResponse {
case (r, v) if v between 1 and 3 =>
println("Using deprecated #1")
Response
case (r, v) if v between 3 and 8 =>
println("Using deprecated #2")
Response
case (r, v) =>
println("Using Current")
Response
}
*/
def exampleControllerThree = syncVersionedResponse {
case (r, v) if v < 3 => // Deprecated on 2014-09-12
println("Using deprecated #1")
Response
case (r, v) if v < 8 => // Deprecated on 2014-09-30
println("Using deprecated #2")
Response
case (r, v) =>
println("Using Current")
Response
}
/*
def exampleControllerFour = versionedResponse2(
3 -> { r =>
println("Using deprecated #1")
Response
},
8 -> { r =>
println("Using deprecated #1")
Response
}) { r =>
println("Using Current")
Response
}
def exampleControllerFive = versionedResponse3(
(3, { r =>
println("Using deprecated #1")
Response
}, "Deprecated on 2014-09-12"),
(8, { r =>
println("Using deprecated #1")
Response
}, "Deprecated on 2014-09-12")) { r =>
println("Using Current")
Response
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment