Skip to content

Instantly share code, notes, and snippets.

View joost-de-vries's full-sized avatar

Joost de Vries joost-de-vries

View GitHub Profile
@joost-de-vries
joost-de-vries / map.scala
Last active August 24, 2017 16:18
Play Json Map[K,V]
/** Play Json only supports Map[String,V]. This function creates a format for Map[K,V]. The type K should produce a JsString.
* Otherwise the serialisation will fail. Which is good enough since in valid json keys can only be strings. */
def mapFormat[K, V](implicit fk: Format[K], fv: Format[V]): Format[Map[K, V]] = new OFormat[Map[K, V]] {
override def writes(o: Map[K, V]): JsObject = {
val stringMap = o.map { case (k, v) => (Json.toJson[K](k).as[JsString].value, v) }
Json.toJson(stringMap).as[JsObject]
}
override def reads(json: JsValue): JsResult[Map[K, V]] = {
for {
@joost-de-vries
joost-de-vries / slick311.conf
Created August 22, 2017 06:07
Slick 3.1.1 conf to prevent deadlock
slick {
dbs {
default {
driver = "slick.driver.PostgresDriver$"
db {
driver = "org.postgresql.Driver"
url = ${rds.url}
user = ${rds.username}
password = ${rds.password}
package shared
import swave.core.{Pipe, Spout, StreamOps}
import scala.concurrent.{ExecutionContext, Future}
object StreamingUtil {
import scala.language.higherKinds
implicit class ContinueAfter[T, S[X] <: StreamOps[X]](val underlying: S[T]) extends AnyVal {
import swave.core.{Coupling, Spout, StreamEnv, UnterminatedSynchronousStreamException}
import scala.concurrent.{Await, Future, TimeoutException}
import scala.concurrent.duration._
import scala.util.{Success, Try}
trait MyEvent
class EventA extends MyEvent
@joost-de-vries
joost-de-vries / pretty-print.scala
Last active March 7, 2017 22:07 — forked from carymrobbins/pretty-print.scala
Pretty print Scala case classes and other data structures.
/**
* Pretty prints a Scala value similar to its source represention.
* Particularly useful for case classes.
* @param a - The value to pretty print.
* @param indentSize - Number of spaces for each indent.
* @param maxElementWidth - Largest element size before wrapping.
* @param depth - Initial depth to pretty print indents.
* @return
*/
object PrettyPrint {
Caused by: swave.core.UnclosedStreamGraphException: Unconnected Port in MapStage@15faa2dd/awaitingOnSubscribe
at swave.core.impl.stages.StageImpl._xSeal(StageImpl.scala:313)
at swave.core.impl.stages.inout.MapStage._xSeal(MapStage.scala:16)
at swave.core.impl.stages.StageImpl.xSeal(StageImpl.scala:291)
at swave.core.impl.stages.inout.NopStage._xSeal(NopStage.scala:15)
at swave.core.impl.stages.StageImpl.xSeal(StageImpl.scala:291)
at swave.core.impl.stages.inout.MapStage._xSeal(MapStage.scala:16)
at swave.core.impl.stages.StageImpl.xSeal(StageImpl.scala:291)
at swave.core.impl.stages.drain.PublisherDrainStage._xSeal(PublisherDrainStage.scala:20)
@joost-de-vries
joost-de-vries / rsFromActor.scala
Last active January 10, 2017 14:03
Create a Reactive Streams Publisher from an actor
def scheduledPublisher(buffer: Int, initialDelay: FiniteDuration, interval: FiniteDuration)(
implicit actorSystem: ActorSystem): Publisher[Unit] = {
val actorSource = Source.actorRef[Unit](50, OverflowStrategy.fail)
val (actorRef, publisher) = actorSource.toMat(Sink.asPublisher(fanout = false))(Keep.both).run()
actorSystem.scheduler.schedule(initialDelay, interval, actorRef, ())
publisher
}
import scala.concurrent.{ ExecutionContext, Future }
import scala.util.{ Failure, Success, Try }
import scala.util.control.NonFatal
/** the resulting exception when the until function becomes true */
class CanceledException extends Exception
/**
* A function that is comparable to Future.sequence.
* Future.sequence processes in parallel and in case of failure it returns only the failure.
import {Observable} from "rxjs/Observable"
import {Subscriber} from "rxjs/Subscriber"
import {Operator} from "rxjs/Operator"
/**
* adds a 'debug' operator to rxjs/Observable
*
* example:
* import "./debug.observable"
public Mono<Void> gitterSlackRelay() {
ObjectMapper mapper = new ObjectMapper();
return create()
.get(gitterStreamUrl, gitterStreamHandler())
.flatMap(replies -> replies
.receiveByteArray()
.filter(b -> b.length > 2) // ignore gitter keep-alives (\r)
.map(b -> {
try {
return mapper.readValue(b, Map.class);