Skip to content

Commit

Permalink
started introducing lenses
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 13, 2017
1 parent 1b8b892 commit f3b3649
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/test/scala/lens/LensExtractor.scala
Expand Up @@ -9,9 +9,16 @@ trait LensExtractor[IN, OUT] extends Function[IN, OUT] {
override def apply(target: IN): OUT

/**
* Lens operation to get the value from the target. Synonym for invoke(IN)
* Lens operation to get the value from the target. Synonym for apply(IN)
*
* @throws LensFailure if the value could not be retrieved from the target (missing/invalid etc)
*/
def extract(target: IN): OUT = apply(target)

/**
* Lens operation to get the value from the target. Synonym for apply(IN)
*
* @throws LensFailure if the value could not be retrieved from the target (missing/invalid etc)
*/
def <--(target: IN): OUT = apply(target)
}
14 changes: 11 additions & 3 deletions src/test/scala/lens/LensInjector.scala
Expand Up @@ -7,12 +7,20 @@ trait LensInjector[IN, OUT] {
def apply[R <: IN](value: OUT, target: R): R

/**
* Lens operation to set the value into the target. Synomym for invoke(OUT, IN)
* Lens operation to set the value into the target. Synomym for apply(OUT, IN)
*/
def inject[R <: IN](value: OUT, target: R): R = apply(value, target)

/**
* Bind this Lens to a value, so we can set it into a target
* Lens operation to set the value into the target. Synomym for apply(OUT, IN)
*/
def of[R <: IN](value: OUT): (R) => R = apply(value, _)
def -->[R <: IN](value: OUT): (R) => R = apply(value, _)
}

object Injector {
implicit class Injector[T](r: T) {
def inject(a: ((T) => T)*) = a.foldLeft(r) {
(memo, next) => next(memo)
}
}
}
17 changes: 17 additions & 0 deletions src/test/scala/lens/example/example.scala
@@ -0,0 +1,17 @@
package lens.example

import com.twitter.finagle.http.{Method, Request}
import io.fintrospect.ContentTypes
import lens.Injector._
import lens.{Body, Header}

object example extends App{

private val value = Body.string(ContentTypes.APPLICATION_ATOM_XML).toLens

case class Bob(i: Int)

val a = Header.int().map(Bob, (s: Bob) => s.i).required("booo")
println(Request(Method.Get, "/asd").inject(a --> Bob(123), value --> "asdasd").headerMap)

}
5 changes: 4 additions & 1 deletion src/test/scala/lens/parts.scala
Expand Up @@ -21,6 +21,8 @@ class BaseBidiLensSpec[T <: Message](

def string(): BiDiLensSpec[T, String, String] = this

def char(): BiDiLensSpec[T, String, Char] = this.map((s: String) => s.charAt(0), _.toString)

def nonEmptyString(): BiDiLensSpec[T, String, String] = this.map(nonEmpty, nonEmpty)

def int(): BiDiLensSpec[T, String, Int] = this.map(_.toInt, _.toString)
Expand Down Expand Up @@ -55,7 +57,8 @@ object Query extends BaseBidiLensSpec[Request]("query", StringParamType,
m
}
}, identity[String])
)
) {
}

object Header extends BaseBidiLensSpec[Message]("header", StringParamType,
new LensGet[Message, String, String]((name: String, target: Message) => target.headerMap.getAll(name), identity[String]),
Expand Down

0 comments on commit f3b3649

Please sign in to comment.