Skip to content

Commit

Permalink
Support for circe KeyDecoder and KeyEncoder (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbergeron authored Oct 20, 2023
1 parent 1397bfd commit 6224b94
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion circe/src/io/github/iltotore/iron/circe.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.iltotore.iron

import io.circe.{Decoder, DecodingFailure, Encoder, HCursor}
import io.circe.*

/**
* Implicit [[Encoder]]s and [[Decoder]]s for refined types.
Expand Down Expand Up @@ -28,3 +28,27 @@ object circe:

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: Encoder[mirror.IronType]): Encoder[T] =
ev.asInstanceOf[Encoder[T]]

/**
* A [[KeyDecoder]] for refined types. Decodes to the underlying type then checks the constraint.
*
* @param decoder the [[KeyDecoder]] of the underlying type.
* @param constraint the [[Constraint]] implementation to test the decoded value.
*/
inline given [A, B](using inline decoder: KeyDecoder[A], inline constraint: Constraint[A, B]): KeyDecoder[A :| B] =
KeyDecoder.instance: input =>
decoder.apply(input).flatMap[A :| B](_.refineOption)

/**
* An [[KeyEncoder]] instance for refined types. Basically the underlying type [[KeyEncoder]].
*
* @param encoder the [[KeyEncoder]] of the underlying type.
*/
inline given [A, B](using inline encoder: KeyEncoder[A]): KeyEncoder[A :| B] = encoder.asInstanceOf[KeyEncoder[A :| B]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyDecoder[mirror.IronType]): KeyDecoder[T] =
ev.asInstanceOf[KeyDecoder[T]]

inline given[T](using mirror: RefinedTypeOps.Mirror[T], ev: KeyEncoder[mirror.IronType]): KeyEncoder[T] =
ev.asInstanceOf[KeyEncoder[T]]

0 comments on commit 6224b94

Please sign in to comment.