io.hireproof.structure.Header.scala Maven / Gradle / Ivy
The newest version!
package io.hireproof.structure
import cats.data.{Chain, Validated}
import cats.syntax.all._
import cats.{Eval, Invariant}
import io.hireproof.screening.Validation
import org.typelevel.ci.CIString
final case class Header[A](name: CIString, schema: Eval[Schema[A]]) extends Structure.Product[A] { self =>
override type Self[a] = Header[a]
override type Element[a] = Header[a]
override type Group[a] = Headers[a]
override def zipAll[T](headers: Headers[T]): Headers[A |*| T] = toHeaders zipAll headers
override def zip[T](header: Header[T]): Headers[A |*| T] = toHeaders zip header
def mapSchema[T](f: Schema[A] => Schema[T]): Header[T] = copy(schema = schema.map(f))
def toHeaders: Headers[A] = Headers.fromHeader(this)
override private[structure] def vimap[T](
f: A => Validated[Errors, T],
g: T => A,
validation: Option[Validation[_, _]]
): Header[T] = copy(schema = schema.map(_.vimap(f, g, validation)))
def fromStringChain(headers: Chain[(CIString, String)]): Validated[Errors, (Chain[(CIString, String)], A)] =
collectAndRemoveFirst(headers) { case (`name`, value) => value }
.traverse(schema.value.fromString)
.leftMap(_.modifyHistory(name.toString /: _))
def toStringChain(a: A): Chain[(CIString, String)] = Chain.fromOption(schema.value.toString(a).tupleLeft(name))
}
object Header {
def default[A](name: CIString, schema: => Schema[A]): Header[A] = Header(name, Eval.later(schema))
implicit val invariant: Invariant[Header] = new Invariant[Header] {
override def imap[A, B](fa: Header[A])(f: A => B)(g: B => A): Header[B] = fa.imap(f)(g)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy