Field
Library is called Fields solely because it is built around Field data type.
Field[P] has path of type FieldPath and value of type P.
All validations are defined throught syntax available for Field
Syntaxβ
Createβ
import fields.value.FieldsDsl.default._
case class Request(name: String)
val request = Request("Ann")
// request: Request = Request("Ann")
Field(request.name)
// res0: Field[String] = Field(path = ., value = "Ann")
Field(FieldPath.parse("request.name"),request.name)
// res1: Field[String] = Field(path = .request.name, value = "Ann")
Field.from(request.name)
// res2: Field[String] = Field(path = .request.name, value = "Ann")
Field.sub(request.name)
// res3: Field[String] = Field(path = .name, value = "Ann")
Transformβ
case class B()
case class A(b: B)
val a = Field(FieldPath.fromPath("a"), A(B()))
// a: Field[A] = Field(path = .a, value = A(B()))
a.sub(_.b)
// res4: Field[B] = Field(path = .a.b, value = B())
a.down("b", a.value.b)
// res5: Field[B] = Field(path = .a.b, value = B())
a.downS("b", _.b)
// res6: Field[B] = Field(path = .a.b, value = B())
a.map(_.b)
// res7: Field[B] = Field(path = .a, value = B())
a.mapPath(_ + "A")
// res8: Field[A] = Field(path = .a.A, value = A(B()))
a.named("A")
// res9: Field[A] = Field(path = .A, value = A(B()))
a.withPath(FieldPath.fromPath("b"))
// res10: Field[A] = Field(path = .b, value = A(B()))
a.withValue(3)
// res11: Field[Int] = Field(path = .a, value = 3)
Specialβ
Field(1 -> "2").first
// res12: Field[Int] = Field(path = ., value = 1)
Field(1 -> "2").second
// res13: Field[String] = Field(path = ., value = "2")
Field(Option(1)).option
// res14: Option[Field[Int]] = Some(Field(path = ., value = 1))