All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hireproof.structure.Authorization.scala Maven / Gradle / Ivy

The newest version!
package io.hireproof.structure

import cats.syntax.all._
import cats.{Applicative, Eval, Traverse}

final case class Authorization[A, B](token: A, payload: B)

object Authorization {
  sealed abstract class Response[+A] extends Product with Serializable {
    final def map[T](f: A => T): Authorization.Response[T] = this match {
      case Response.Authorized(value) => Response.Authorized(f(value))
      case Response.Unauthorized      => Response.Unauthorized
    }

    final def traverse[F[_]: Applicative, T](f: A => F[T]): F[Authorization.Response[T]] = this match {
      case Response.Authorized(value) => f(value).map(Response.Authorized[T])
      case Response.Unauthorized      => Response.Unauthorized.pure[F].widen
    }
  }

  object Response {
    final case class Authorized[A](value: A) extends Response[A]
    case object Unauthorized extends Response[Nothing]

    implicit val traverse: Traverse[Authorization.Response] = new Traverse[Authorization.Response] {
      override def map[A, B](fa: Response[A])(f: A => B): Response[B] = fa.map(f)

      override def traverse[G[_]: Applicative, A, B](fa: Response[A])(f: A => G[B]): G[Response[B]] =
        fa.traverse(f)

      override def foldLeft[A, B](fa: Response[A], b: B)(f: (B, A) => B): B = fa match {
        case Authorized(value) => f(b, value)
        case Unauthorized      => b
      }

      override def foldRight[A, B](fa: Response[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = fa match {
        case Authorized(value) => f(value, lb)
        case Unauthorized      => lb
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy