-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from theiterators/circe-support
kebs-circe - Scala 3 support
- Loading branch information
Showing
26 changed files
with
1,322 additions
and
347 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions
57
circe/src/main/scala-3/pl/iterators/kebs/circe/KebsCirce.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,57 @@ | ||
package pl.iterators.kebs.circe | ||
|
||
import io.circe.{ Decoder, Encoder } | ||
import scala.deriving._ | ||
import scala.util.Try | ||
import scala.quoted.Quotes | ||
import io.circe.HCursor | ||
import pl.iterators.kebs.macros.CaseClass1Rep | ||
import pl.iterators.kebs.instances.InstanceConverter | ||
import io.circe.generic.AutoDerivation | ||
import scala.quoted.Type | ||
import io.circe.derivation.ConfiguredDecoder | ||
import io.circe.derivation.Configuration | ||
import io.circe.Derivation | ||
import io.circe.DecoderDerivation | ||
import io.circe.EncoderDerivation | ||
import io.circe.derivation.ConfiguredEncoder | ||
import scala.NonEmptyTuple | ||
|
||
private[circe] trait KebsAutoDerivation { | ||
|
||
implicit val configuration: Configuration = Configuration.default | ||
|
||
inline implicit def exportDecoder[A](using conf: Configuration, inline m: Mirror.ProductOf[A]): ConfiguredDecoder[A] = | ||
ConfiguredDecoder.derived[A] | ||
|
||
inline implicit def exportEncoder[A](using conf: Configuration, inline m: Mirror.ProductOf[A]): ConfiguredEncoder[A] = | ||
ConfiguredEncoder.derived[A] | ||
} | ||
trait KebsCirce extends KebsAutoDerivation { | ||
|
||
inline given[T, A](using rep: CaseClass1Rep[T, A], decoder: Decoder[A]): Decoder[T] = { | ||
decoder.emap(obj => Try(rep.apply(obj)).toEither.left.map(_.getMessage)) | ||
} | ||
|
||
inline given[T, A](using rep: CaseClass1Rep[T, A], encoder: Encoder[A]): Encoder[T] = | ||
encoder.contramap(rep.unapply) | ||
|
||
inline given[T, A](using rep: InstanceConverter[T, A], encoder: Encoder[A]): Encoder[T] = | ||
encoder.contramap(rep.encode) | ||
|
||
inline given[T, A](using rep: InstanceConverter[T, A], decoder: Decoder[A]): Decoder[T] = | ||
decoder.emap(obj => Try(rep.decode(obj)).toEither.left.map(_.getMessage)) | ||
} | ||
|
||
object KebsCirce { | ||
trait NoFlat extends KebsCirce { | ||
} | ||
|
||
trait Snakified extends KebsCirce { | ||
override implicit val configuration: Configuration = Configuration.default.withSnakeCaseMemberNames | ||
} | ||
|
||
trait Capitalized extends KebsCirce { | ||
override implicit val configuration: Configuration = Configuration.default.withPascalCaseMemberNames | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
circe/src/main/scala-3/pl/iterators/kebs/circe/KebsEnumFormats.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,89 @@ | ||
package pl.iterators.kebs.circe | ||
|
||
import io.circe.Decoder.Result | ||
import io.circe._ | ||
import pl.iterators.kebs.macros.enums.{EnumOf} | ||
import scala.reflect.Enum | ||
import scala.util.Try | ||
import pl.iterators.kebs.enums.ValueEnum | ||
import pl.iterators.kebs.macros.enums.ValueEnumLike | ||
import pl.iterators.kebs.macros.enums.ValueEnumOf | ||
trait CirceEnum { | ||
@inline protected final def enumNameDeserializationError[E <: Enum](e: EnumOf[E], name: String): String = { | ||
val enumNames = e.`enum`.values.mkString(", ") | ||
s"$name should be one of $enumNames" | ||
} | ||
|
||
@inline protected final def enumValueDeserializationError[E <: Enum](e: EnumOf[E], value: Json): String = { | ||
val enumNames = e.`enum`.values.mkString(", ") | ||
s"$value should be a string of value $enumNames" | ||
} | ||
|
||
protected final def enumDecoder[E <: Enum](e: EnumOf[E], _comap: String => Option[E]): Decoder[E] = | ||
(c: HCursor) => | ||
Decoder.decodeString.emap(str => _comap(str).toRight("")) | ||
.withErrorMessage(enumValueDeserializationError(e, c.value))(c) | ||
|
||
protected final def enumEncoder[E <: Enum](e: EnumOf[E], _map: E => String): Encoder[E] = | ||
(obj: E) => Encoder.encodeString(_map(obj)) | ||
|
||
def enumDecoder[E <: Enum](e: EnumOf[E]): Decoder[E] = | ||
enumDecoder[E](e, s => e.`enum`.values.find(_.toString.equalsIgnoreCase(s))) | ||
|
||
def enumEncoder[E <: Enum](e: EnumOf[E]): Encoder[E] = | ||
enumEncoder[E](e, (e: Enum) => e.toString) | ||
|
||
def lowercaseEnumDecoder[E <: Enum](e: EnumOf[E]): Decoder[E] = | ||
enumDecoder[E](e, s => e.`enum`.values.find(_.toString.toLowerCase == s)) | ||
def lowercaseEnumEncoder[E <: Enum](e: EnumOf[E]): Encoder[E] = | ||
enumEncoder[E](e, (e: Enum) => e.toString.toLowerCase) | ||
|
||
def uppercaseEnumDecoder[E <: Enum](e: EnumOf[E]): Decoder[E] = | ||
enumDecoder[E](e, s => e.`enum`.values.find(_.toString().toUpperCase() == s)) | ||
def uppercaseEnumEncoder[E <: Enum](e: EnumOf[E]): Encoder[E] = | ||
enumEncoder[E](e, (e: Enum) => e.toString().toUpperCase()) | ||
} | ||
|
||
trait CirceValueEnum { | ||
@inline protected final def valueEnumDeserializationError[V, E <: ValueEnum[V] with Enum](e: ValueEnumOf[V, E], value: Json): String = { | ||
val enumValues = e.`enum`.values.map(_.value.toString()).mkString(", ") | ||
s"$value is not a member of $enumValues" | ||
} | ||
|
||
def valueEnumDecoder[V, E <: ValueEnum[V] with Enum](e: ValueEnumOf[V, E])(implicit decoder: Decoder[V]): Decoder[E] = | ||
(c: HCursor) => | ||
decoder.emap(obj => Try(e.`enum`.valueOf(obj)).toOption.toRight("")).withErrorMessage(valueEnumDeserializationError(e, c.value))(c) | ||
|
||
def valueEnumEncoder[V, E <: ValueEnum[V] with Enum](e: ValueEnumOf[V, E])(implicit encoder: Encoder[V]): Encoder[E] = | ||
(obj: E) => { encoder(obj.value) } | ||
} | ||
|
||
trait KebsEnumFormats extends CirceEnum with CirceValueEnum { | ||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Decoder[E] = enumDecoder(ev) | ||
|
||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Encoder[E] = enumEncoder(ev) | ||
|
||
implicit inline given[V, E <: ValueEnum[V] with Enum](using ev: ValueEnumOf[V, E], decoder: Decoder[V]): Decoder[E] = | ||
valueEnumDecoder(ev) | ||
|
||
implicit inline given[V, E <: ValueEnum[V] with Enum](using ev: ValueEnumOf[V, E], encoder: Encoder[V]): Encoder[E] = | ||
valueEnumEncoder(ev) | ||
|
||
trait Uppercase extends CirceEnum { | ||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Decoder[E] = | ||
uppercaseEnumDecoder(ev) | ||
|
||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Encoder[E] = | ||
uppercaseEnumEncoder(ev) | ||
} | ||
|
||
trait Lowercase extends CirceEnum { | ||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Decoder[E] = | ||
lowercaseEnumDecoder(ev) | ||
|
||
implicit inline given[E <: Enum](using ev: EnumOf[E]): Encoder[E] = | ||
lowercaseEnumEncoder(ev) | ||
} | ||
} | ||
|
||
object KebsEnumFormats extends KebsEnumFormats |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
File renamed without changes.
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,56 @@ | ||
import io.circe._ | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import pl.iterators.kebs.circe.KebsEnumFormats | ||
import scala.reflect.Enum | ||
|
||
class CirceEnumDecoderEncoderTests extends AnyFunSuite with Matchers { | ||
|
||
enum Greeting { | ||
case Hello, GoodBye, Hi, Bye | ||
} | ||
|
||
object KebsProtocol extends KebsEnumFormats | ||
object KebsProtocolUppercase extends KebsEnumFormats.Uppercase | ||
object KebsProtocolLowercase extends KebsEnumFormats.Lowercase | ||
|
||
|
||
|
||
import Greeting._ | ||
test("enum JsonFormat") { | ||
import KebsProtocol._ | ||
val decoder = implicitly[Decoder[Greeting]] | ||
val encoder = implicitly[Encoder[Greeting]] | ||
decoder(Json.fromString("hElLo").hcursor) shouldBe Right(Hello) | ||
decoder(Json.fromString("goodbye").hcursor) shouldBe Right(GoodBye) | ||
encoder(Hello) shouldBe Json.fromString("Hello") | ||
encoder(GoodBye) shouldBe Json.fromString("GoodBye") | ||
} | ||
|
||
test("enum name deserialization error") { | ||
import KebsProtocol._ | ||
val decoder = implicitly[Decoder[Greeting]] | ||
decoder(Json.fromInt(1).hcursor) shouldBe Left( | ||
DecodingFailure("1 should be a string of value Hello, GoodBye, Hi, Bye", List.empty[CursorOp])) | ||
} | ||
|
||
test("enum JsonFormat - lowercase") { | ||
import KebsProtocol._ | ||
val decoder = implicitly[Decoder[Greeting]] | ||
val encoder = implicitly[Encoder[Greeting]] | ||
decoder(Json.fromString("hello").hcursor) shouldBe Right(Hello) | ||
decoder(Json.fromString("goodbye").hcursor) shouldBe Right(GoodBye) | ||
encoder(Hello) shouldBe Json.fromString("Hello") | ||
encoder(GoodBye) shouldBe Json.fromString("GoodBye") | ||
} | ||
|
||
test("enum JsonFormat - uppercase") { | ||
import KebsProtocol._ | ||
val decoder = implicitly[Decoder[Greeting]] | ||
val encoder = implicitly[Encoder[Greeting]] | ||
decoder(Json.fromString("HELLO").hcursor) shouldBe Right(Hello) | ||
decoder(Json.fromString("GOODBYE").hcursor) shouldBe Right(GoodBye) | ||
encoder(Hello) shouldBe Json.fromString("Hello") | ||
encoder(GoodBye) shouldBe Json.fromString("GoodBye") | ||
} | ||
} |
Oops, something went wrong.