Skip to content

Instantly share code, notes, and snippets.

View TobiasPfeifer's full-sized avatar

Tobias Pfeifer TobiasPfeifer

View GitHub Profile
@TobiasPfeifer
TobiasPfeifer / ErrorAwareOptional.scala
Created May 3, 2021 08:39
Monocle ErrorAwareOptional
case class ErrorAwareOptional[E, S, T, A, B] private (underlying: POptional[Either[E, S], Either[E, T], Either[E, A], Either[E, B]]) {
self =>
def errorAs[E1 >: E] = self.asInstanceOf[ErrorAwareOptional[E1, S, T, A, B]]
def andThen[C, D](other: POptional[A, B, C, D]): ErrorAwareOptional[E, S, T, C, D] = {
val lifted: ErrorAwareOptional[E, A, B, C, D] = lift(other).errorAs[E]
self.andThen(lifted)
}
def andThen[E1 >: E, C, D](other: ErrorAwareOptional[E1, A, B, C, D]): ErrorAwareOptional[E1, S, T, C, D] = {
@TobiasPfeifer
TobiasPfeifer / TypeSystem.scala
Last active May 22, 2016 07:18
sample DSL using Parametric Polymorphism ("Java Generics") and F-Bounded Polymorphism
sealed trait Entity[E <: Entity[E]]
sealed case class Forward[FROM <: Entity[FROM], TO <: Entity[TO]](from: FROM, to: TO)
trait EntityRef[FROM <: Entity[FROM], TO <: Entity[TO]] { self =>
def ref(entity: TO) : Forward[FROM, TO]
}
trait UserRef[FROM <: Entity[FROM]] extends EntityRef[FROM, User] { self: Entity[FROM] =>
override def ref(entity: User): Forward[UserRef[FROM] with Entity[FROM], User] = Forward(self, entity)
}
trait GroupRef[FROM <: Entity[FROM]] extends EntityRef[FROM, Group] { self: Entity[FROM] =>
trait RequestRateLimiter[T] {
def check(ctx: RequestContext, realIp: Option[RemoteAddress], proxiedIp: Option[RemoteAddress]) : RequestRateResult[T]
}
sealed trait RequestRateResult[T]
case class Pass[T](result: T) extends RequestRateResult[T]
case class Block[T](rejection: Rejection) extends RequestRateResult[T]
def limit[T](requestRateLimiter: RequestRateLimiter[T]): Directive1[T] = {
def remoteAddress: Directive1[Option[RemoteAddress]] = optionalHeaderValuePF { case `Remote-Address`(address) ⇒ address }