Skip to content

Instantly share code, notes, and snippets.

View matterche's full-sized avatar

Matthias Erche matterche

View GitHub Profile
@matterche
matterche / postgres-json-cheatsheet.md
Last active July 4, 2017 14:45 — forked from rmtsrc/postgres-json-cheatsheet.md
Using JSON in Postgres by example

PostgreSQL JSON Cheatsheet

Using JSON in Postgres by example.

Quick setup via Docker

  1. Download and install: Docker Toolbox
  2. Open Docker Quickstart Terminal
  3. Start a new postgres container:
    docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
@matterche
matterche / StdOutErrLogger.scala
Last active January 15, 2016 16:24
Redirect System.out and System.err to Play Framework logger
package globals
import java.io.PrintStream
import play.api.Logger
object StdOutErrLogger {
val logger = Logger(this.getClass)
def redirectSystemOutAndErrToLog() = {
@matterche
matterche / ScalaZ Monad Transformers.scala
Last active October 3, 2015 13:20 — forked from eamelink/transformers.scala
Working with Eithers in Futures
import scala.concurrent.Future
object either {
// Scala standard library Either is sometimes used to distinguish between 'failure' and 'success' state. Like these two methods:
def getUser(id: String): Either[String, User] = ???
def getPreferences(user: User): Either[String, Preferences] = ???
// The Right side contains the success value by convention, because right is right, right?
@matterche
matterche / git-log2json.sh
Last active August 29, 2015 14:27 — forked from textarcana/git-log2json.sh
Convert Git logs to JSON. The first script (git-log2json.sh) is all you need, the other two files contain only optional bonus features :)
#!/usr/bin/env bash
# Use this one-liner to produce a JSON literal from the Git log:
git log \
--pretty=format:'{%n "commit": "%H",%n "author": "%an <%ae>",%n "date": "%ad",%n "message": "%f"%n},' \
$@ | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/'
@matterche
matterche / serialFutures.scala
Created July 20, 2015 15:20
Non-blocking serial execution of futures
// see http://www.michaelpollmeier.com/execute-scala-futures-in-serial-one-after-the-other-non-blocking/
def serialiseFutures[A, B](l: Iterable[A])(fn: A ⇒ Future[B])
(implicit ec: ExecutionContext): Future[List[B]] =
l.foldLeft(Future(List.empty[B])) {
(previousFuture, next) ⇒
for {
previousResults ← previousFuture
next ← fn(next)
} yield previousResults :+ next
}
@matterche
matterche / IntelliJTemplate
Created July 16, 2015 13:27
IntelliJ Idea Template for ScalaTest table based property test
import org.scalatest.prop.PropertyChecks._
import org.scalatest.prop.Tables.Table
import org.scalatest.WordSpec
class MyTableBasedPropertySpec extends WordSpec {
val $testData$ = Table(
("$p1$", "$p2$", "$p3$"),
("1", 2, 3.0),
("4",5,6.0)
)
@matterche
matterche / TableBasedSpec.scala
Created July 16, 2015 13:03
Template for ScalaTest table based property tests
import org.scalatest.prop.PropertyChecks._
import org.scalatest.prop.Tables.Table
import org.scalatest.WordSpec
class MyTableBasedPropertySpec extends WordSpec {
val testData = Table(
("p1", "p2", "p3"),
("1", 2, 3.0),
("4",5,6.0)
)
@matterche
matterche / zsh.md
Last active August 29, 2015 14:24 — forked from tsabat/zsh.md
@matterche
matterche / EnumUtils.scala
Last active August 29, 2015 14:24 — forked from mikesname/gist:5237809
Play Json enumeration read/write
package enumtest
import play.api.libs.json._
object EnumUtils {
def enumReads[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsString(s) => {
try {
JsSuccess(enum.withName(s))
#!/usr/bin/env scalas
/***
scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.play" %% "play-netty-server" % "2.4.1"
*/
import play.core.server._
import play.api.routing.sird._
import play.api.mvc._