Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instance for QueryParamDecoder[FUUID] #69

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lazy val http4s = project.in(file("modules/http4s"))
.settings(
name := "fuuid-http4s",
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-core" % http4sV,
"org.http4s" %% "http4s-dsl" % http4sV % Test
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.chrisdavenport.fuuid.http4s

import io.chrisdavenport.fuuid.FUUID
import cats.data.ValidatedNel
import cats.syntax.either._
import org.http4s.QueryParamDecoder
import org.http4s.QueryParameterValue
import org.http4s.ParseFailure

object implicits {
implicit val fuuidQueryParamDecoder: QueryParamDecoder[FUUID] =
new QueryParamDecoder[FUUID] {
def decode(value: QueryParameterValue): ValidatedNel[ParseFailure, FUUID] =
FUUID
.fromString(value.value)
.leftMap(
_ =>
ParseFailure(
"Failed to parse FUUID query parameter",
s"Could not parse ${value.value} as a FUUID"))
.toValidatedNel
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.chrisdavenport.fuuid.http4s

import io.chrisdavenport.fuuid.{FUUID, FUUIDArbitraries}
import io.chrisdavenport.fuuid.http4s.implicits._
import org.http4s.dsl.io._
import org.scalacheck._
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification

class FUUIDQueryParamDecoder extends Specification with ScalaCheck with FUUIDArbitraries {

object IdQueryParamMatcher extends QueryParamDecoderMatcher[FUUID]("id")

"FUUID QueryParamDecoder" should {

"work properly given a valid UUID" in prop { validFuuid: FUUID =>
IdQueryParamMatcher.unapply(Map("id" -> List(validFuuid.show))) must beSome(validFuuid)
}

"fail given an invalid UUID" in prop { invalidUuid: String =>
IdQueryParamMatcher.unapply(Map("id" -> List(invalidUuid))) must beNone
}.setArbitrary(Arbitrary(Gen.alphaStr))
}
}