Skip to content

Commit

Permalink
Merge pull request sherpal#6 from raquo/master
Browse files Browse the repository at this point in the history
Document optional params
  • Loading branch information
sherpal authored Mar 6, 2022
2 parents 2034256 + b413740 commit 134e854
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ The tupling of the type parameters works the same way as for path.
There are two main built in query parameters that you can use as building blocks for most of yours needs:

- `param[T](paramName: String)`: represents the value of the parameter with name `paramName` as a type `T` element
- `param[T](paramName: String).?`: optional param, with value encoded as `Option[T]`
- `listParam[T](paramName: Strig)`: same as `param` but for lists.

#### Query parameters examples
Expand Down
3 changes: 2 additions & 1 deletion shared/src/main/scala/urldsl/vocabulary/Param.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ object Param {
.map(_.split("=").toList)
.toList
.collect {
case first :: second :: Nil => first -> second
case first :: Nil if first.nonEmpty => first -> ""
case first :: second :: Nil if first.nonEmpty => first -> second
}
.groupBy(_._1)
.map { case (key, value) => key -> value.map(_._2) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ final class QueryParamsExamples extends AnyFlatSpec with Matchers {
*/
param[String]("does-not-exist").?.matchRawUrl(sampleUrl) should be(Right(None))
param[String]("bar").?.matchRawUrl(sampleUrl) should be(Right(Some("stuff")))
param[String]("empty").?.matchRawUrl(sampleUrl) should be(Right(Some("")))

/** Decoding failures on optional params result in None */
param[Int]("bar").?.matchRawUrl(sampleUrl) should be(Right(None))
param[Int]("empty").?.matchRawUrl(sampleUrl) should be(Right(None))

/**
* [[urldsl.language.QueryParameters]] have a filter method allowing to restrict the things it matches.
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/scala/urldsl/examples/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package urldsl
* url-dsl for generating urls, and not for parsing them.
*/
package object examples {
val sampleUrl = "http://www.some-domain.be/foo/23/true?bar=stuff&babar=other%20stuff&other=2&other=3&ok=true#the-ref"
val sampleUrl = "http://www.some-domain.be/foo/23/true?bar=stuff&babar=other%20stuff&other=2&other=3&empty=&ok=true#the-ref"
val sampleUrlWithoutFragment =
"http://www.some-domain.be/foo/23/true?bar=stuff&babar=other%20stuff&other=2&other=3&ok=true"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ final class QueryParamsUrlSpec extends AnyFlatSpec with Matchers {

listParam[String]("names").params(List("Alice", "Bob")) should be("names=Alice&names=Bob")

(param[String]("name") & param[Int]("age").?).params("Hello", None) should be("name=Hello")
(param[String]("name") & param[Int]("age").?).params("Hello", Some(0)) should be("name=Hello&age=0")
(param[String]("name") & param[Int]("age").?).params("Hello", Some(1)) should be("name=Hello&age=1")
(param[String]("name") & param[String]("cat").?).params("Hello", Some("")) should be("name=Hello&cat=")
(param[String]("name") & param[String]("cat").?).params("Hello", Some("Fluffy")) should be("name=Hello&cat=Fluffy")

// demonstrates quasi commutativity
(listParam[Int]("ages") & param[String]("name")).params(List(32, 23), "Bob") should be(
"ages=32&ages=23&name=Bob"
Expand Down

0 comments on commit 134e854

Please sign in to comment.