Skip to content

Commit

Permalink
simplify query param access (#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomTriple authored Sep 1, 2023
1 parent 7d990ee commit cfe9e35
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class EndpointBenchmark {
val collectHttpApp = Routes(
Method.GET / "users" / int("userId") / "posts" / int("postId") -> handler {
(userIdInt: Int, postIdInt: Int, req: Request) =>
val query = req.url.queryParams.get("query").flatMap(_.headOption).get
val query = req.url.queryParams.get("query").get

Response.json(ExampleData(userIdInt, postIdInt, query).toJson)
},
Expand Down
24 changes: 18 additions & 6 deletions zio-http/src/main/scala/zio/http/QueryParams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,28 @@ final case class QueryParams(map: Map[String, Chunk[String]]) {
QueryParams(map.filter(p.tupled))

/**
* Retrieves the query parameter values having the specified name.
* Retrieves all query parameter values having the specified name.
*/
def get(key: String): Option[Chunk[String]] = map.get(key)
def getAll(key: String): Option[Chunk[String]] = map.get(key)

/**
* Retrieves the query parameter value having the specified name, or else uses
* the default iterable.
* Retrieves the first query parameter value having the specified name.
*/
def getOrElse(key: String, default: => Iterable[String]): Chunk[String] =
map.getOrElse(key, Chunk.fromIterable(default))
def get(key: String): Option[String] = getAll(key).flatMap(_.headOption)

/**
* Retrieves all query parameter values having the specified name, or else
* uses the default iterable.
*/
def getAllOrElse(key: String, default: => Iterable[String]): Chunk[String] =
getAll(key).getOrElse(Chunk.fromIterable(default))

/**
* Retrieves the first query parameter value having the specified name, or
* else uses the default value.
*/
def getOrElse(key: String, default: => String): String =
get(key).getOrElse(default)

override def hashCode: Int = normalize.map.hashCode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private[codec] object EncoderDecoder {

val queryParamValue =
queryParams
.getOrElse(query.name, Nil)
.getAllOrElse(query.name, Nil)
.collectFirst(query.textCodec)

queryParamValue match {
Expand Down
12 changes: 12 additions & 0 deletions zio-http/src/test/scala/zio/http/QueryParamsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ object QueryParamsSpec extends ZIOHttpSpec {
}
},
),
suite("get - getAll")(
test("success") {
val name = "name"
val default = "default"
val unknown = "non-existent"
val queryParams = QueryParams(name -> "a", name -> "b")
assertTrue(queryParams.get(name).get == "a") &&
assertTrue(queryParams.getOrElse(unknown, default) == default) &&
assertTrue(queryParams.getAll(name).get.length == 2) &&
assertTrue(queryParams.getAllOrElse(unknown, Chunk(default)).length == 1)
},
),
suite("encode - decode")(
test("success") {
val genQueryParamsWithoutCornerCases =
Expand Down
4 changes: 2 additions & 2 deletions zio-http/src/test/scala/zio/http/codec/HttpCodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ object HttpCodecSpec extends ZIOHttpSpec {
test("paramBool encoding") {
val requestTrue = codecBool.encodeRequest(true)
val requestFalse = codecBool.encodeRequest(false)
assert(requestTrue.url.queryParams.get(isAge).get.head)(Assertion.equalTo("true")) &&
assert(requestFalse.url.queryParams.get(isAge).get.head)(Assertion.equalTo("false"))
assert(requestTrue.url.queryParams.get(isAge).get)(Assertion.equalTo("true")) &&
assert(requestFalse.url.queryParams.get(isAge).get)(Assertion.equalTo("false"))
},
) +
suite("Codec with examples") {
Expand Down

0 comments on commit cfe9e35

Please sign in to comment.