-
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
272 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
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
/* | ||
* Copyright (c) 2023 record4s authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
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)) | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
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,175 @@ | ||
/* | ||
* Copyright (c) 2023 record4s authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
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,33 @@ | ||
/* | ||
* Copyright (c) 2023 record4s authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
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