Skip to content

Commit

Permalink
Close #278 - [refined4s-tapir] Add Schema type-class instances with a…
Browse files Browse the repository at this point in the history
…uto deriving
  • Loading branch information
kevin-lee committed Apr 5, 2024
1 parent 8f1905c commit f339773
Show file tree
Hide file tree
Showing 2 changed files with 690 additions and 0 deletions.
@@ -0,0 +1,39 @@
package refined4s.modules.tapir.derivation.generic

import refined4s.*
import sttp.tapir.Schema

/** @author Kevin Lee
* @since 2024-04-03
*/
trait auto {
inline given derivedNewtypeSchema[A, B](
using coercibleA2B: Coercible[A, B],
coercibleB2A: Coercible[B, A],
schemaB: Schema[B],
): Schema[A] = auto.derivedNewtypeSchema

inline given derivedRefinedSchema[A, B](
using refinedCtor: RefinedCtor[A, B],
coercibleA2B: Coercible[A, B],
schemaB: Schema[B],
): Schema[A] = auto.derivedRefinedSchema

}
object auto {

inline given derivedNewtypeSchema[A, B](
using coercibleA2B: Coercible[A, B],
coercibleB2A: Coercible[B, A],
schemaB: Schema[B],
): Schema[A] =
schemaB.map(b => Some(coercibleB2A(b)))(coercibleA2B(_))

inline given derivedRefinedSchema[A, B](
using refinedCtor: RefinedCtor[A, B],
coercibleA2B: Coercible[A, B],
schemaB: Schema[B],
): Schema[A] =
schemaB.map(refinedCtor.create(_).toOption)(coercibleA2B(_))

}

0 comments on commit f339773

Please sign in to comment.