forked from zio/zio-json
-
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.
Added zio-json instances for http4s (zio#241)
* Fixed zio#240 - Added zio-json instances for http4s EntityDecoder/EntityEncoder * added interop to zio-json http4s package
- Loading branch information
1 parent
ab58a93
commit 6eb41b5
Showing
7 changed files
with
147 additions
and
2 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
3 changes: 3 additions & 0 deletions
3
zio-json-interop-http4s/src/main/scala/zio/json/interop/http4s/ZIOEntityCodec.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,3 @@ | ||
package zio.json.interop.http4s | ||
|
||
object ZIOEntityCodec extends ZIOEntityEncoder with ZIOEntityDecoder |
13 changes: 13 additions & 0 deletions
13
zio-json-interop-http4s/src/main/scala/zio/json/interop/http4s/ZIOEntityDecoder.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,13 @@ | ||
package zio.json.interop.http4s | ||
|
||
import cats.effect.Concurrent | ||
import org.http4s.EntityDecoder | ||
|
||
import zio.json.JsonDecoder | ||
|
||
trait ZIOEntityDecoder { | ||
implicit def zioEntityDecoder[F[_]: Concurrent, A: JsonDecoder]: EntityDecoder[F, A] = | ||
jsonOf[F, A] | ||
} | ||
|
||
object ZIOEntityDecoder extends ZIOEntityDecoder |
11 changes: 11 additions & 0 deletions
11
zio-json-interop-http4s/src/main/scala/zio/json/interop/http4s/ZIOEntityEncoder.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,11 @@ | ||
package zio.json.interop.http4s | ||
|
||
import org.http4s.EntityEncoder | ||
|
||
import zio.json.JsonEncoder | ||
|
||
trait ZIOEntityEncoder { | ||
implicit def zioEntityEncoder[F[_], A: JsonEncoder]: EntityEncoder[F, A] = jsonEncoderOf[F, A] | ||
} | ||
|
||
object ZIOEntityEncoder extends ZIOEntityEncoder |
27 changes: 27 additions & 0 deletions
27
zio-json-interop-http4s/src/main/scala/zio/json/interop/http4s/ZIOJsonInstances.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,27 @@ | ||
package zio.json.interop.http4s | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
import cats.effect.Concurrent | ||
import org.http4s.headers.`Content-Type` | ||
import org.http4s.{ EntityDecoder, EntityEncoder, MalformedMessageBodyFailure, MediaType } | ||
|
||
import zio.json._ | ||
|
||
trait ZIOJsonInstances { | ||
def jsonOf[F[_]: Concurrent, A: JsonDecoder]: EntityDecoder[F, A] = | ||
EntityDecoder.decodeBy[F, A](MediaType.application.json) { m => | ||
EntityDecoder.collectBinary(m).subflatMap { chunk => | ||
val str = new String(chunk.toArray, StandardCharsets.UTF_8) | ||
if (str.nonEmpty) | ||
str.fromJson.fold(e => Left(MalformedMessageBodyFailure(e, None)), Right(_)) | ||
else | ||
Left(MalformedMessageBodyFailure("Invalid JSON: empty body")) | ||
} | ||
} | ||
|
||
def jsonEncoderOf[F[_], A: JsonEncoder]: EntityEncoder[F, A] = EntityEncoder | ||
.stringEncoder[F] | ||
.contramap[A](_.toJson) | ||
.withContentType(`Content-Type`(MediaType.application.json)) | ||
} |
3 changes: 3 additions & 0 deletions
3
zio-json-interop-http4s/src/main/scala/zio/json/interop/http4s/package.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,3 @@ | ||
package zio.json.interop | ||
|
||
package object http4s extends ZIOJsonInstances |
69 changes: 69 additions & 0 deletions
69
zio-json-interop-http4s/src/test/scala/zio/json/interop/http4s/ZIOJsonInstancesSpec.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,69 @@ | ||
package zio.json.interop.http4s | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
import org.http4s._ | ||
|
||
import zio.Task | ||
import zio.interop.catz._ | ||
import zio.json._ | ||
import zio.test.Assertion._ | ||
import zio.test.{ DefaultRunnableSpec, _ } | ||
|
||
object ZIOJsonInstancesSpec extends DefaultRunnableSpec { | ||
final case class Test(string: String, int: Int) | ||
private implicit val decoder: JsonCodec[Test] = DeriveJsonCodec.gen[Test] | ||
|
||
def spec: ZSpec[Environment, Failure] = suite("json instances")( | ||
suite("jsonEncoderOf") { | ||
testM("returns an EntityEncoder that can encode for the given effect and type") { | ||
checkM(Gen.anyString, Gen.anyInt) { (s, i) => | ||
val result = jsonEncoderOf[Task, Test] | ||
.toEntity(Test(s, i)) | ||
.body | ||
.compile | ||
.toList | ||
.map(v => new String(v.toArray, StandardCharsets.UTF_8)) | ||
|
||
assertM(result)(equalTo(s"""{"string":"$s","int":$i}""")) | ||
} | ||
} | ||
}, | ||
suite("jsonOf")( | ||
testM("returns an EntityDecoder that can decode for the given effect and type")( | ||
checkM(Gen.anyString, Gen.anyInt) { (s, i) => | ||
val media = Request[Task]() | ||
.withEntity(s"""{"string":"$s","int":$i}""") | ||
.withHeaders(Header("Content-Type", "application/json")) | ||
|
||
assertM(jsonOf[Task, Test].decode(media, true).value)(isRight(equalTo(Test(s, i)))) | ||
} | ||
), | ||
testM("returns MalformedMessageBodyFailure when json is empty") { | ||
val media = Request[Task]() | ||
.withEntity("") | ||
.withHeaders(Header("Content-Type", "application/json")) | ||
|
||
assertM(jsonOf[Task, Test].decode(media, true).value)( | ||
isLeft(equalTo(MalformedMessageBodyFailure("Invalid JSON: empty body"))) | ||
) | ||
}, | ||
testM("returns MalformedMessageBodyFailure when json is invalid") { | ||
val media = Request[Task]() | ||
.withEntity("""{"bad" "json"}""") | ||
.withHeaders(Header("Content-Type", "application/json")) | ||
|
||
assertM(jsonOf[Task, Test].decode(media, true).value)( | ||
isLeft(equalTo(MalformedMessageBodyFailure("(expected ':' got '\"')"))) | ||
) | ||
}, | ||
testM("returns MalformedMessageBodyFailure when message body is not a json") { | ||
val media = Request[Task]() | ||
.withEntity("not a json") | ||
.withHeaders(Header("Content-Type", "text/plain")) | ||
|
||
assertM(jsonOf[Task, Test].decode(media, true).value)(isLeft(isSubtype[MediaTypeMismatch](anything))) | ||
} | ||
) | ||
) | ||
} |