Skip to content

Instantly share code, notes, and snippets.

@RAtioNAn
RAtioNAn / SlickQueryOps.scala
Created July 14, 2022 21:09 — forked from leandrob13/SlickQueryOps.scala
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 ] ) {
@RAtioNAn
RAtioNAn / RateLimit.scala
Created May 16, 2022 09:34 — forked from johanandren/RateLimit.scala
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
@RAtioNAn
RAtioNAn / OptionFilter.scala
Created February 20, 2021 18:06 — forked from leandrob13/OptionFilter.scala
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 ) }
}
}
@RAtioNAn
RAtioNAn / gist:56396ca193576f80ac6d19316dfcf111
Created February 20, 2021 18:06 — forked from cvogt/gist:9193220
Slick: Dynamic query conditions using the **MaybeFilter** (Updated to support nullable columns)
import scala.slick.lifted.CanBeQueryCondition
// optionally filter on a column with a supplied predicate
case class MaybeFilter[X, Y](val query: scala.slick.lifted.Query[X, Y]) {
def filter[T,R:CanBeQueryCondition](data: Option[T])(f: T => X => R) = {
data.map(v => MaybeFilter(query.filter(f(v)))).getOrElse(this)
}
}
// example use case
import java.sql.Date
@RAtioNAn
RAtioNAn / Main.scala
Created December 16, 2020 15:31 — forked from jpallari/Main.scala
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)"
}