Skip to content

Commit

Permalink
Get rid of implicit conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicoulaud-ledger committed Nov 18, 2023
1 parent 279f312 commit a6a5b4e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
13 changes: 7 additions & 6 deletions docs/_docs/modules/skunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ import io.github.iltotore.iron.skunk.given

type Username = String :| Not[Blank]

// refine a codec implicitly
val a: Query[Void, Username] = sql"SELECT name FROM users".query(varchar)
// refining a codec at usage site
val a: Query[Void, Username] = sql"SELECT name FROM users".query(varchar.refined)

// refine a codec explictly
val b: Query[Void, Username] = sql"SELECT name FROM users".query(varchar.refined)
// defining a codec for a refined opaque type
opaque type PositiveInt = Int :| Positive
object PositiveInt extends RefinedTypeOps[Int, Positive, PositiveInt]:
given codec: Codec[PositiveInt] = int4.refined[Positive]

// defining a codec for a refined case class
final case class User(name: Username, age: Int :| Positive)
given Codec[User] = (varchar.refined[Not[Blank]] *: int4.refined[Positive]).to[User]

given Codec[User] = (varchar.refined[Not[Blank]] *: PositiveInt.codec).to[User]
```
11 changes: 0 additions & 11 deletions skunk/src/io.github.iltotore.iron/skunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.github.iltotore.iron

import _root_.skunk.*

import scala.Conversion

object skunk:

/**
Expand All @@ -15,15 +13,6 @@ object skunk:
inline def refined[C](using inline constraint: Constraint[A, C]): Codec[A :| C] =
codec.eimap[A :| C](_.refineEither[C])(_.asInstanceOf[A])

/**
* Implicit conversion for refining a [[Codec]]. Decodes to the underlying type then checks the constraint.
*
* @param constraint the [[Constraint]] implementation to test the decoded value
*/
inline given [A, C](using inline constraint: Constraint[A, C]): Conversion[Codec[A], Codec[A :| C]] =
new Conversion[Codec[A], Codec[A :| C]]:
override def apply(codec: Codec[A]): Codec[A :| C] = codec.refined

/**
* A [[Codec]] for refined types. Decodes to the underlying type then checks the constraint.
*
Expand Down
12 changes: 7 additions & 5 deletions skunk/test/src/io/github/iltotore/iron/SkunkExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import io.github.iltotore.iron.skunk.given

type Username = String :| Not[Blank]

// refine a codec implicitly
val a: Query[Void, Username] = sql"SELECT name FROM users".query(varchar)
// refining a codec at usage site
val a: Query[Void, Username] = sql"SELECT name FROM users".query(varchar.refined)

// refine a codec explictly
val b: Query[Void, Username] = sql"SELECT name FROM users".query(varchar.refined)
// defining a codec for a refined opaque type
opaque type PositiveInt = Int :| Positive
object PositiveInt extends RefinedTypeOps[Int, Positive, PositiveInt]:
given codec: Codec[PositiveInt] = int4.refined[Positive]

// defining a codec for a refined case class
final case class User(name: Username, age: Int :| Positive)
given Codec[User] = (varchar.refined[Not[Blank]] *: int4.refined[Positive]).to[User]
given Codec[User] = (varchar.refined[Not[Blank]] *: PositiveInt.codec).to[User]
14 changes: 8 additions & 6 deletions skunk/test/src/io/github/iltotore/iron/SkunkSuite.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.github.iltotore.iron

import _root_.skunk.*
import _root_.skunk.given
import _root_.skunk.implicits.*
import _root_.skunk.codec.all.*
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.skunk.*
import io.github.iltotore.iron.skunk.given
import io.github.iltotore.iron.*
import utest.*

opaque type PositiveInt = Int :| Positive
object PositiveInt extends RefinedTypeOps[Int, Positive, PositiveInt]
object SkunkSuite extends TestSuite:

given Codec[Int] = int4
given Codec[Int] = int4

object SkunkSuite extends TestSuite:
opaque type PositiveInt = Int :| Positive
object PositiveInt extends RefinedTypeOps[Int, Positive, PositiveInt]:
given Codec[PositiveInt] = summon[Codec[Int]].refined

val tests: Tests = Tests {

Expand Down

0 comments on commit a6a5b4e

Please sign in to comment.