Skip to content

Commit

Permalink
reimplementing <--? for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Feb 4, 2017
1 parent d7dbe8c commit 6189434
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 13 additions & 9 deletions core/src/main/scala/io/fintrospect/parameters/Parameter.scala
@@ -1,7 +1,7 @@
package io.fintrospect.parameters

import io.fintrospect.util.ExtractionError.{Invalid, Missing}
import io.fintrospect.util.{Extracted, Extraction, ExtractionFailed}
import io.fintrospect.util.{Extracted, Extraction, ExtractionError, ExtractionFailed}

import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -36,9 +36,11 @@ trait OptionalParameter[From, T, Bnd <: Binding] extends Optional[From, T]
/**
* Attempt to manually deserialize from the message object, using a validation predicate and reason for failure.
*/
def <--?(from: From, reason: String, predicate: T => Boolean): Extraction[Option[T]] = ???

// <--?(from).flatMap[T](v => if (v.forall(predicate)) Extraction(v) else ExtractionFailed(ExtractionError(this, reason)))
def <--?(from: From, reason: String, predicate: T => Boolean): Extraction[Option[T]] =
<--?(from) match {
case Extracted(x) => if(x.forall(predicate)) Extraction(x) else ExtractionFailed(ExtractionError(this, reason))
case e => e
}

/**
* Attempt to manually deserialize from the message object, using a validation predicate and reason for failure.
Expand All @@ -60,9 +62,11 @@ trait MandatoryParameter[From, T, Bnd <: Binding] extends Mandatory[From, T]
/**
* Attempt to manually deserialize from the message object, using a validation predicate and reason for failure.
*/
def <--?(from: From, reason: String, predicate: T => Boolean): Extraction[T] = ???

// <--?(from).flatMap[T](v => if (v.forall(predicate)) Extraction(v) else ExtractionFailed(ExtractionError(this, reason)))
def <--?(from: From, reason: String, predicate: T => Boolean): Extraction[T] =
<--?(from) match {
case Extracted(x) => if(predicate(x)) Extraction(x) else ExtractionFailed(ExtractionError(this, reason))
case e => e
}

/**
* Attempt to manually deserialize from the message object, using a validation predicate and reason for failure.
Expand Down Expand Up @@ -122,7 +126,7 @@ abstract class MultiMandatoryParameter[T, From, B <: Binding](val name: String,
def <--?(from: From): Extraction[Seq[T]] = from match {
case req: ExtractedRouteRequest => req.get(this)
case _ => eab.valuesFrom(this, from)
.map(values => Try(values.map(spec.deserialize)) match {
.map(xs => Try(xs.map(spec.deserialize)) match {
case Success(x) => Extracted(x)
case Failure(_) => ExtractionFailed(Invalid(this))
}).getOrElse(ExtractionFailed(Missing(this)))
Expand All @@ -140,7 +144,7 @@ abstract class MultiOptionalParameter[T, From, B <: Binding](val name: String, v
def <--?(from: From): Extraction[Option[Seq[T]]] = from match {
case req: ExtractedRouteRequest => req.get(this)
case _ => eab.valuesFrom(this, from)
.map(x => Try(x.map(spec.deserialize)) match {
.map(xs => Try(xs.map(spec.deserialize)) match {
case Success(x) => Extracted(Some(x))
case Failure(_) => ExtractionFailed(Invalid(this))
}).getOrElse(Extracted(None))
Expand Down
5 changes: 2 additions & 3 deletions core/src/test/scala/io/fintrospect/util/ExtractorTest.scala
Expand Up @@ -23,7 +23,7 @@ class ExtractorTest extends FunSpec with Matchers {
} yield (req, opt)
}

ex <--? Request("/") shouldBe Extracted(Some((None, None)))
ex <--? Request("/") shouldBe Extracted((None, None))
}

it("does not short circuit if last parameter in a for comprehension is optional") {
Expand All @@ -35,7 +35,7 @@ class ExtractorTest extends FunSpec with Matchers {
} yield (req, opt)
}

ex <--? Request("/?req=123") shouldBe Extracted(Some((Some(123), None)))
ex <--? Request("/?req=123") shouldBe Extracted((123, None))
}

describe("non-embedded extraction") {
Expand Down Expand Up @@ -121,7 +121,6 @@ class ExtractorTest extends FunSpec with Matchers {

it("combine") {
Extraction.combine(Seq(Extracted(None), Extracted(None))) shouldBe Extracted(())
Extraction.combine(Seq(Extracted(None), Extracted(Some(1)))) shouldBe Extracted(None)
Extraction.combine(Seq(Extracted(None), Extracted(Some(1)), ExtractionFailed(missing), ExtractionFailed(invalid))) shouldBe ExtractionFailed(Seq(missing, invalid))
}
}
Expand Down

0 comments on commit 6189434

Please sign in to comment.