-
Notifications
You must be signed in to change notification settings - Fork 4
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
5 changed files
with
209 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
23 changes: 23 additions & 0 deletions
23
modules/circe/src/main/scala/com/github/tarao/record4s/circe/Codec.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,23 @@ | ||
package com.github.tarao.record4s | ||
package circe | ||
|
||
import io.circe.{Decoder, Encoder, HCursor, Json} | ||
|
||
object Codec { | ||
inline given encoder[R <: %, RR <: ProductRecord](using | ||
ar: typing.ArrayRecord.Aux[R, RR], | ||
enc: Encoder[RR], | ||
): Encoder[R] = new Encoder[R] { | ||
final def apply(record: R): Json = enc(ArrayRecord.from(record)) | ||
} | ||
|
||
inline given decoder[R <: %](using | ||
r: RecordLike[R], | ||
dec: Decoder[ArrayRecord[r.TupledFieldTypes]], | ||
c: typing.Record.Concat[%, ArrayRecord[r.TupledFieldTypes]], | ||
ev: c.Out =:= R, | ||
): Decoder[R] = new Decoder[R] { | ||
final def apply(c: HCursor): Decoder.Result[R] = | ||
dec(c).map(ar => ev(ar.toRecord)) | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
modules/circe/src/test/scala/com/github/tarao/record4s/circe/CodecSpec.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,154 @@ | ||
package com.github.tarao.record4s | ||
package circe | ||
|
||
import io.circe.generic.auto.* | ||
import io.circe.parser.parse | ||
import io.circe.syntax.* | ||
|
||
class CodecSpec extends helper.UnitSpec { | ||
describe("ArrayRecord") { | ||
// ArrayRecord can be encoded/decoded without any special codec | ||
|
||
describe("encoding") { | ||
it("should encode an array record to json") { | ||
val r = ArrayRecord(name = "tarao", age = 3) | ||
val json = r.asJson.noSpaces | ||
json shouldBe """{"name":"tarao","age":3}""" | ||
} | ||
|
||
it("should encode a nested array record to json") { | ||
val r = ArrayRecord( | ||
name = "tarao", | ||
age = 3, | ||
email = ArrayRecord(user = "tarao", domain = "example.com"), | ||
) | ||
val json = r.asJson.noSpaces | ||
json shouldBe """{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
} | ||
} | ||
|
||
describe("decoding") { | ||
it("should decode json to an array record") { | ||
val json = """{"name":"tarao","age":3}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = | ||
jsonObj.as[ArrayRecord[(("name", String), ("age", Int))]] | ||
record.name shouldBe "tarao" | ||
record.age shouldBe 3 | ||
} | ||
|
||
it("should decode json to a nested array record") { | ||
val json = | ||
"""{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = jsonObj.as[ArrayRecord[ | ||
( | ||
("name", String), | ||
("age", Int), | ||
("email", ArrayRecord[(("user", String), ("domain", String))]), | ||
), | ||
]] | ||
record.name shouldBe "tarao" | ||
record.age shouldBe 3 | ||
record.email.user shouldBe "tarao" | ||
record.email.domain shouldBe "example.com" | ||
} | ||
|
||
it("can decode partially") { | ||
locally { | ||
val json = """{"name":"tarao","age":3}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = | ||
jsonObj.as[ArrayRecord[("name", String) *: EmptyTuple]] | ||
record.name shouldBe "tarao" | ||
"record.age" shouldNot typeCheck | ||
} | ||
|
||
locally { | ||
val json = | ||
"""{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = jsonObj.as[ArrayRecord[ | ||
("email", ArrayRecord[("domain", String) *: EmptyTuple]) *: | ||
EmptyTuple, | ||
]] | ||
"record.name" shouldNot typeCheck | ||
"record.age" shouldNot typeCheck | ||
"record.email.user" shouldNot typeCheck | ||
record.email.domain shouldBe "example.com" | ||
} | ||
} | ||
} | ||
} | ||
|
||
describe("%") { | ||
import Codec.{decoder, encoder} | ||
|
||
describe("encoder") { | ||
it("should encode a record to json") { | ||
val r = %(name = "tarao", age = 3) | ||
val json = r.asJson.noSpaces | ||
json shouldBe """{"name":"tarao","age":3}""" | ||
} | ||
|
||
it("should encode a nested record to json") { | ||
val r = %( | ||
name = "tarao", | ||
age = 3, | ||
email = %(user = "tarao", domain = "example.com"), | ||
) | ||
val json = r.asJson.noSpaces | ||
json shouldBe """{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
} | ||
} | ||
|
||
describe("decoder") { | ||
it("should decode json to a record") { | ||
val json = """{"name":"tarao","age":3}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = | ||
jsonObj.as[% { val name: String; val age: Int }] | ||
record.name shouldBe "tarao" | ||
record.age shouldBe 3 | ||
} | ||
|
||
it("should decode json to a nested record") { | ||
val json = | ||
"""{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = jsonObj.as[ | ||
% { | ||
val name: String; val age: Int; | ||
val email: % { val user: String; val domain: String } | ||
}, | ||
] | ||
record.name shouldBe "tarao" | ||
record.age shouldBe 3 | ||
record.email.user shouldBe "tarao" | ||
record.email.domain shouldBe "example.com" | ||
} | ||
|
||
it("can decode partially") { | ||
locally { | ||
val json = """{"name":"tarao","age":3}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = jsonObj.as[% { val name: String }] | ||
record.name shouldBe "tarao" | ||
"record.age" shouldNot typeCheck | ||
} | ||
|
||
locally { | ||
val json = | ||
"""{"name":"tarao","age":3,"email":{"user":"tarao","domain":"example.com"}}""" | ||
val ShouldBeRight(jsonObj) = parse(json) | ||
val ShouldBeRight(record) = | ||
jsonObj.as[% { val email: % { val domain: String } }] | ||
"record.name" shouldNot typeCheck | ||
"record.age" shouldNot typeCheck | ||
"record.email.user" shouldNot typeCheck | ||
record.email.domain shouldBe "example.com" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
package helper | ||
|
||
trait EitherValues { | ||
self: org.scalatest.Assertions => | ||
|
||
object ShouldBeRight { | ||
def unapply[A, B](x: Either[A, B]): Some[B] = x match { | ||
case Right(value) => Some(value) | ||
case _ => fail(s"$x was not Right") | ||
} | ||
} | ||
} |
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