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

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

The newest version!
package io.hireproof.structure

import cats.data.Validated
import cats.{Eval, Invariant}
import io.hireproof.screening.Validation

final case class Parameter[A](name: String, schema: Eval[Schema.Value[A]]) extends Structure[A] {
  override type Self[a] = Parameter[a]

  def mapSchema[T](f: Schema.Value[A] => Schema.Value[T]): Parameter[T] = copy(schema = schema.map(f))

  override private[structure] def vimap[T](
      f: A => Validated[Errors, T],
      g: T => A,
      validation: Option[Validation[_, _]]
  ): Parameter[T] = copy(schema = schema.map(_.vimap(f, g, validation)))

  def fromString(value: String): Validated[Errors, A] = schema.value.fromStringValue(value)
  def toString(value: A): String = schema.value.toStringValue(value)
}

object Parameter {
  def default[A](name: String, schema: => Schema.Value[A]): Parameter[A] =
    Parameter(name, Eval.later(schema))

  implicit val invariant: Invariant[Parameter] = new Invariant[Parameter] {
    override def imap[A, B](fa: Parameter[A])(f: A => B)(g: B => A): Parameter[B] = fa.imap(f)(g)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy