From d7b4f52656b16e4794cd00707686f5ba217499bb Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Wed, 21 Nov 2018 16:01:29 +0100 Subject: [PATCH 1/2] Add instance for QueryParamDecoder[FUUID] --- build.sbt | 2 +- .../fuuid/http4s/implicits.scala | 23 ++++++++++++++++++ .../fuuid/http4s/FUUIDQueryParamDecoder.scala | 24 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 modules/http4s/src/main/scala/io/chrisdavenport/fuuid/http4s/implicits.scala create mode 100644 modules/http4s/src/test/scala/io/chrisdavenport/fuuid/http4s/FUUIDQueryParamDecoder.scala diff --git a/build.sbt b/build.sbt index c9eaca39..8d19866e 100644 --- a/build.sbt +++ b/build.sbt @@ -51,7 +51,7 @@ lazy val http4s = project.in(file("modules/http4s")) .settings( name := "fuuid-http4s", libraryDependencies ++= Seq( - "org.http4s" %% "http4s-dsl" % http4sV % Test + "org.http4s" %% "http4s-dsl" % http4sV ) ) .dependsOn(coreJVM % "compile->compile;test->test") diff --git a/modules/http4s/src/main/scala/io/chrisdavenport/fuuid/http4s/implicits.scala b/modules/http4s/src/main/scala/io/chrisdavenport/fuuid/http4s/implicits.scala new file mode 100644 index 00000000..8bd08512 --- /dev/null +++ b/modules/http4s/src/main/scala/io/chrisdavenport/fuuid/http4s/implicits.scala @@ -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 + } +} diff --git a/modules/http4s/src/test/scala/io/chrisdavenport/fuuid/http4s/FUUIDQueryParamDecoder.scala b/modules/http4s/src/test/scala/io/chrisdavenport/fuuid/http4s/FUUIDQueryParamDecoder.scala new file mode 100644 index 00000000..a1c6d843 --- /dev/null +++ b/modules/http4s/src/test/scala/io/chrisdavenport/fuuid/http4s/FUUIDQueryParamDecoder.scala @@ -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)) + } +} From 560668370bfd697bed191ed1cd4cc64a4299c9b8 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Wed, 21 Nov 2018 16:38:19 +0100 Subject: [PATCH 2/2] Make http4s module depend on http4s-core instead of http4s-dsl --- build.sbt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 8d19866e..513db3a4 100644 --- a/build.sbt +++ b/build.sbt @@ -51,7 +51,8 @@ lazy val http4s = project.in(file("modules/http4s")) .settings( name := "fuuid-http4s", libraryDependencies ++= Seq( - "org.http4s" %% "http4s-dsl" % http4sV + "org.http4s" %% "http4s-core" % http4sV, + "org.http4s" %% "http4s-dsl" % http4sV % Test ) ) .dependsOn(coreJVM % "compile->compile;test->test")