Skip to content

Commit

Permalink
fixing .mapTo and .collect
Browse files Browse the repository at this point in the history
0.11.7
  • Loading branch information
yurique committed Jan 21, 2021
1 parent 7578d90 commit beef16d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 19 deletions.
29 changes: 20 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# Changelog

### 0.11.7

* Bugfix: `.collect` and `.mapTo` were not storing the mapped value (in the internal state)

An example that would show this behavior:

```scala
(pathEnd.mapTo(true) | path("page-1").mapTo(false)) { isIndex =>
render(Page(isIndex))
}
```

### 0.11.6

## Disjunction bug fix.
* Bugfix: disjunction
* Bugfix: `.map` was not storing the mapped value (in the internal state)

The value of the disjunction was not preserved, thus a disjunction would not
match multiple times in a row (even if the provided value was different).
The value of the disjunction was not stored (in the internal state), thus a disjunction would not
match multiple times in a row (even if the resulting values were different).

An example that would show this behavior:

Expand All @@ -15,12 +28,10 @@ An example that would show this behavior:
}
```

## New utilities

* `.some` – transforms a `Directive[A]` into a `Directive[Option[A]]` (with `Some(_)` value)
* `.none` – transforms a `Directive[Option[A]]` into a `Directive[Option[A]]` (with `None` value)
* `.mapOption` – transforms a `Directive[Option[A]]` into a `Directive[Option[B]]` (applying the provided function to the `Option`)

* API: new combinators for directives
* `.some` – transforms a `Directive[A]` into a `Directive[Option[A]]` (with `Some(_)` value)
* `.none` – transforms a `Directive[A]` into a `Directive[Option[A]]` (with `None` value)
* `.mapOption` – transforms a `Directive[Option[A]]` into a `Directive[Option[B]]` (applying the provided function to the `Option`)

### 0.11.5

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Built on top of:
`frontroute` is available for [Scala.js](http://www.scala-js.org/) v1.3.1+ (published for Scala 2.12 and 2.13).

```scala
libraryDependencies += "io.frontroute" %%% "frontroute" % "0.11.6"
libraryDependencies += "io.frontroute" %%% "frontroute" % "0.11.7"
```

```scala
Expand Down Expand Up @@ -542,8 +542,14 @@ The basic ones are:

* `flatMap[R](next: L => Directive[R]): Directive[R]`
* `map[R](f: L => R): Directive[R]`
* `mapTo[R](otherValue: => R): Directive[R]`
* `collect[R](f: PartialFunction[L, R]): Directive[R]`
* `filter(predicate: L => Boolean): Directive[L]`
* `def some: Directive[Option[L]]` – wraps the value in `Some(_)`
* `none[R]: Directive[Option[R]]` – replaces the value with `None`

For a `Directive[Option[A]]`:
* `def mapOption[R](f: A => R): Directive[Option[R]]` – maps the value inside the `Option`

### Conjunction

Expand Down
10 changes: 3 additions & 7 deletions src/main/scala/io/frontroute/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ class Directive[L](

@inline def none[R]: Directive[Option[R]] = mapTo(Option.empty[R])

def mapTo[R](otherValue: => R): Directive[R] =
Directive[R] { inner =>
self.tapply { _ => (location, previous, state) =>
inner(otherValue)(location, previous, state.path(".mapTo"))
}
}
@inline def mapTo[R](otherValue: => R): Directive[R] = map(_ => otherValue)

def &[R](magnet: ConjunctionMagnet[L]): magnet.Out = magnet(this)

Expand All @@ -60,7 +55,8 @@ class Directive[L](
Directive[R] { inner =>
self.tapply { value => (location, previous, state) =>
if (f.isDefinedAt(value)) {
inner(f(value))(location, previous, state.path(".collect"))
val mapped = f(value)
inner(mapped)(location, previous, state.path(".collect").setValue(mapped))
} else {
rejected
}
Expand Down
56 changes: 55 additions & 1 deletion src/test/scala/io/frontroute/RoutingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ object RoutingTests extends TestSuite {
}
}

test("disjunction") {
test("disjunction and .map") {
case class Page(isIndex: Boolean)
routeTest(
route = probe =>
Expand All @@ -433,6 +433,60 @@ object RoutingTests extends TestSuite {
}
}

test("disjunction and .mapTo") {
case class Page(isIndex: Boolean)
routeTest(
route = probe =>
(pathEnd.mapTo(true) | path("page-1").mapTo(false)) { isIndex =>
complete {
probe.append(Page(isIndex).toString)
}
},
init = locationProvider => {
locationProvider.path()
locationProvider.path("page-1")
locationProvider.path()
locationProvider.path("page-1")
locationProvider.path()
}
) { probe =>
probe.toList ==> Seq(
Page(true).toString,
Page(false).toString,
Page(true).toString,
Page(false).toString,
Page(true).toString
)
}
}

test("disjunction and .collect") {
case class Page(isIndex: Boolean)
routeTest(
route = probe =>
(pathEnd.collect { case _ => true } | path("page-1").collect { case _ => false }) { isIndex =>
complete {
probe.append(Page(isIndex).toString)
}
},
init = locationProvider => {
locationProvider.path()
locationProvider.path("page-1")
locationProvider.path()
locationProvider.path("page-1")
locationProvider.path()
}
) { probe =>
probe.toList ==> Seq(
Page(true).toString,
Page(false).toString,
Page(true).toString,
Page(false).toString,
Page(true).toString
)
}
}

test("disjunction signal") {
var pathSignal: Signal[String] = null
var signals: Future[List[String]] = null
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.11.6"
version in ThisBuild := "0.11.7"

0 comments on commit beef16d

Please sign in to comment.