Skip to content

Instantly share code, notes, and snippets.

@johanandren
johanandren / RateLimit.scala
Created July 14, 2017 10:05
Sample for a custom rate limiting directive for Akka HTTP
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package http
import java.util.concurrent.atomic.AtomicInteger
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, StatusCodes, Uri}
1. Setup a project
2. Add groovy SDK support:
https://www.bonusbits.com/wiki/HowTo:Add_Groovy_SDK_to_IntelliJ_IDEA
3. Download http://(yourjenkinsurl)/job/(yourpipelinejob)/pipeline-syntax/gdsl
- this will give you the .gdsl file - download this to the src folder of your project.
4. Finally follow this step - right click on the src folder -> Mark directory as -> Sources Root
@jpallari
jpallari / Main.scala
Last active February 5, 2024 08:29
Ways to pattern match generic types in Scala
object Main extends App {
AvoidLosingGenericType.run()
AvoidMatchingOnGenericTypeParams.run()
TypeableExample.run()
TypeTagExample.run()
}
class Funky[A, B](val foo: A, val bar: B) {
override def toString: String = s"Funky($foo, $bar)"
}
@leandrob13
leandrob13 / SlickQueryOps.scala
Last active July 14, 2022 21:09
Trait that adds two functions to the slick Query type. I am using typelevel.cats as the functional library. Works for play-slick 3.1
trait SlickQueryOps {
databaseConfig: HasDatabaseConfig[ JdbcProfile ] =>
import cats.Apply
import cats.std.list._
import driver.api._
type BooleanOp = ( Rep[ Boolean ], Rep[ Boolean ] ) => Rep[ Boolean ]
implicit class OptionFilter[ X, Y ]( query: Query[ X, Y, Seq ] ) {
@leandrob13
leandrob13 / OptionFilter.scala
Created September 25, 2015 13:36
Based on MaybeFilter (https://gist.github.com/cvogt/9193220), it uses implicit class to add the methods to the Query objects. FilteredBy works for filtering lists and it returns the query if no filters were applied. FoundBy works for finding a single register, if no filter defined it returns None.
implicit class OptionFilter[ X, Y ]( query: Query[ X, Y, Seq ] ) {
def filteredBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query }
}
def foundBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query.take( 0 ) }
}
}