Skip to content

Instantly share code, notes, and snippets.

abstract class BaseResource(implicit protected val swagger: Swagger)
extends ScalatraServlet with FutureSupport with JacksonJsonSupport with SwaggerSupport with Imports {
override protected val applicationName = Some("messaging-api")
protected def applicationDescription = "Messaging Services API"
protected implicit val defaultTimeout = Timeout(5 seconds)
override val jsonpCallbackParameterNames: Iterable[String] = Some("callback")
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns
@casualjim
casualjim / scala-interview1.scala
Created May 1, 2013 19:55 — forked from oxbowlakes/scala-interview1.scala
simple exercise around map and flatmap
object GOption {
def some[A](a: A): GOption[A] = new GOption[A] {
def cata[B](n: => B, s: A => B): B = sys.error("Implement me")
}
def none[A]: GOption[A] = new GOption[A] {
def cata[B](n: => B, s: A => B): B = sys.error("Implement me")
}
}
trait GOption[+A] {
implicit class PagePatternHelper(val sc: StringContext) extends AnyVal {
def page(args: Any*): PagePattern = {
val template = sc.standardInterpolator(treatEscapes, args)
val templateUrl = rl.Uri(template)
val path = templateUrl.path
val routeMatcher = new SinatraRouteMatcher(path)
val queryMultiParams = rl.MapQueryString.parseString(templateUrl.query.rawValue)
val queryParams = queryMultiParams.mapValues(_.head)
@casualjim
casualjim / simple_serializer.scala
Last active June 16, 2022 02:20
A simple example of a custom serializer for json4s
import org.json4s._
case class MyClass(id: Int) {
lazy val blah = "lazy string"
}
class MyClassSerializer extends Serializer[MyClass] {
private val MyClassClass = classOf[MyClass]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), MyClass] = {
@casualjim
casualjim / jrebel.scala
Last active December 11, 2015 10:59
Generate rebel.xml for a webapp with the web-plugin for sbt
val generateJRebel = TaskKey[Unit]("generate-jrebel", "Generates a rebel.xml for a webapp")
def generateJRebelXmlTask =
(resourceManaged in Compile, crossTarget in Compile, crossTarget in Test, webappResources in Compile, streams) map {
(tgt, src, tst, extra, s) =>
val content = (scala.xml.Utility trimProper {
<application
xsi:schemaLocation="http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.zeroturnaround.com"
@casualjim
casualjim / session_token_strategy.scala
Last active December 8, 2017 13:56
Session token strategy example for scalatra auth (scentry)
package org.example
package auth
import org.scalatra.auth.ScentryStrategy
import org.scalatra.{CookieSupport, ScalatraBase}
trait RedisClient {
def get(key: String): Option[String]
}

Core

  • Adds GZipSupport, contributed by @Marza
  • Folds TypedParamSupport into core making the params.getAs[Int] methods available everywhere

JSON

  • fix reading json from post without the right header
  • Include JValueResult by default in the NativeJsonSupport and JacksonJsonSupport
  • Make JValueResult only serialize Traversable and Product classes

SLF4J

class MyAppBinding[T](b: FieldDescriptor[T]) {
def unique[TId](collection: MongoCollection, currentItem: Option[TId] = None) = b.validateWith(_ ⇒ {
_ flatMap { Validators.uniqueField(b.name, collection, currentItem).validate(_) }
})
}
abstract class MyAppCommand[S](implicit mf: Manifest[S], protected val jsonFormats: Formats) extends ModelCommand[S] with JsonCommand {
type Result = S
implicit def myAppValidators[T](b: FieldDescriptor[T]) = new MyAppBinding[T](b)
organization := "com.github.casualjim"
name := "playground"
version := "0.1.0-SNAPSHOT"
scalaVersion := "2.10.0-RC2"
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation")