Skip to content

Instantly share code, notes, and snippets.

@sam
sam / MultipartParsing.scala
Created December 2, 2016 15:21
When receiving a multipart POST, parse the application/json part into an entity, and capture the file upload.
View MultipartParsing.scala
pathEndOrSingleSlash {
(post & parameter("rev")) { rev =>
// This is very important, so we can provide a more specific
// implicit to overcome the Json4sSupport default unmarshaller
// that will require an `application/json` ContentType!
import Unmarshaller._
entity(as[Multipart.General]) { body =>
import scala.concurrent.duration._
@sam
sam / for-blahbrehensions.scala
Last active August 26, 2016 22:04
A trivial, common example and for-comprehensions failing at it.
View for-blahbrehensions.scala
// fold
client ! Projects(rows.headOption.fold(Nil)(_ \ "value" extract))
// map.getOrElse
client ! Projects(rows.headOption
.map(_ \ "value" extract)
.getOrElse(Nil))
// Option.toList
client ! Projects(rows.headOption.toList.map(_ \ "value" extract))
@sam
sam / build.sbt
Created August 26, 2016 18:14
There's got to be a better way...
View build.sbt
// Don't include private.conf in main package.
mappings in (Compile, packageBin) ~= {
_ filter {
case (_, "private.conf") => false
case _ => true
}
}
// Don't include private.conf in main sources package.
mappings in (Compile, packageSrc) ~= {
@sam
sam / IdSerializer.scala
Created August 23, 2016 14:22
Example of a json4s Format for a sealed trait (Id) with two implementations (NewId or IdWithRev).
View IdSerializer.scala
import org.json4s._
case object IdSerializer extends CustomSerializer[Id](formats => ( {
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil) => IdWithRev(prefix, id, rev)
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil) => NewId(prefix, id)
}, {
case NewId(prefix, id) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil)
case IdWithRev(prefix, id, rev) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil)
}
))
@sam
sam / 1_Password.scala
Created March 28, 2016 14:02
How to transform a JSON field into a composed class member during serialization.
View 1_Password.scala
// This is just a basic sealed trait implementing a couple cases.
sealed trait Password {
val value: String
def hashed: HashedPassword
def check(plainTextCandidate: String): Boolean
}
case class PlainTextPassword(value: String) extends Password {
def hashed = HashedPassword.Scrypt(value)
def check(plainTextCandidate: String) = plainTextCandidate == value
@sam
sam / FunctionZipping.scala
Created March 22, 2016 14:23
Trying to figure out how I can zip both 1-arg functions, and no-arg functions for use in ScalaTest test-fixture "loan" functions.
View FunctionZipping.scala
type FixtureFunction[T] = T => Any
implicit class FixtureFunctionZipper[A](f1: FixtureFunction[A] => Any) {
def zip[B](f2: FixtureFunction[B] => Any): ((A, B) => Any) => Any = { test =>
f1(a => f2(b => test(a, b)))
}
def zip(f2: (Unit => Any) => Any): (A => Any) => Any = { test =>
f1(a => f2(_ => test(a)))
}
@sam
sam / ActorPerRequest.scala
Created March 14, 2016 17:28
Trying to figure out how to clean up actor-per-request actors with akka-http.
View ActorPerRequest.scala
// Just inlining the revelant bits of https://github.com/hseeberger/akka-http-json/blob/master/akka-http-json4s/src/main/scala/de/heikoseeberger/akkahttpjson4s/Json4sSupport.scala#L44
// for the gist.
trait Json4sSupport {
implicit def json4sUnmarshallerConverter[A: Manifest](serialization: Serialization, formats: Formats): FromEntityUnmarshaller[A] =
json4sUnmarshaller(manifest, serialization, formats)
/**
* HTTP entity => `A`
*
* @tparam A type to decode
@sam
sam / BrokenMacro.scala
Created February 12, 2016 14:57
Not sure how to create a macro that would allow me to extend a trait and have generated code.
View BrokenMacro.scala
// Example actor:
class EchoActor extends Actor {
def receive = {
case message => context.sender ! message
}
}
// I want to be able to write:
object EchoActor extends ActorGenerator0
@sam
sam / CompilationExample.scala
Created February 5, 2016 19:55
Caching compilation results errors
View CompilationExample.scala
def compile(): List[CompiledScript] = {
val outputCache = directory / ".cache"
if(!outputCache.exists()) outputCache.mkdirs()
val engine: IMain = new ScriptEngineManager().getEngineByName("scala").asInstanceOf[IMain]
engine.settings.usejavacp.value = true
// TODO: This breaks things. No idea why.
// engine.settings.outdir.value = outputCache.getCanonicalPath
directory.listFiles().toList collect {
@sam
sam / ApplicationController.scala
Last active December 15, 2015 19:40
How to provide your own custom MessagesApi in PlayFramework when using a "base" subproject for branded projects.
View ApplicationController.scala
package com.wieck.base
package controllers
import play.api.{Application, Play, mvc, i18n}, mvc._, i18n._
abstract class ApplicationController extends Controller {
private val messagesApiCache = Application.instanceCache[BaseMessagesApi]
implicit def messages(implicit lang: Lang): Messages = new Messages(lang, messagesApiCache(Play.current))