forked from zio/zio-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
195 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
zio-http/src/main/scala/zio/http/codec/QueryCardinality.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package zio.http.codec | ||
|
||
import zio.Chunk | ||
import zio.http.codec.HttpCodecError | ||
import zio.prelude._ | ||
|
||
sealed case class QueryCardinality[F[+_]: Covariant](extract: (String, Chunk[String]) => F[String]) { | ||
def coerce(values: Any): F[_] = values.asInstanceOf[F[_]] | ||
|
||
def decode[I](name: String, codec: TextCodec[I], values: Chunk[String]): F[I] = extract(name, values) map { | ||
(value: String) => | ||
if (codec.isDefinedAt(value)) codec(value) else throw HttpCodecError.MalformedQueryParam(name, codec) | ||
} | ||
} | ||
|
||
object QueryCardinality { | ||
object any extends QueryCardinality((_, values) => values) | ||
|
||
object oneOrMore | ||
extends QueryCardinality((name, values) => | ||
values | ||
.nonEmptyOrElse(throw HttpCodecError.WrongQueryParamCardinality(name, values.length, "one or more"))(identity), | ||
) | ||
|
||
object optional | ||
extends QueryCardinality((name, values) => | ||
if (values.length > 1) throw HttpCodecError.WrongQueryParamCardinality(name, values.length, "one or none") | ||
else values.headOption, | ||
) | ||
|
||
object one | ||
extends QueryCardinality[Id.Type]((name, values) => | ||
if (values.length == 1) Id(values.head) | ||
else throw HttpCodecError.WrongQueryParamCardinality(name, values.length, "exactly one"), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters