Skip to content

Commit

Permalink
Merge pull request #899 from lenguyenthanh/revive-refined-support
Browse files Browse the repository at this point in the history
Flesh out Refined module a bit ( 2 years later :D)
  • Loading branch information
mpilquist authored Oct 10, 2023
2 parents 2563cde + 92c0cfa commit c0303dd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 44 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ lazy val circe = crossProject(JVMPlatform, JSPlatform, NativePlatform)
lazy val tests = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("modules/tests"))
.dependsOn(core, circe)
.dependsOn(core, circe, refined)
.enablePlugins(AutomateHeaderPlugin, NoPublishPlugin)
.settings(commonSettings)
.settings(
Expand All @@ -175,6 +175,7 @@ lazy val tests = crossProject(JVMPlatform, JSPlatform, NativePlatform)
"org.typelevel" %%% "cats-laws" % "2.10.0",
"org.typelevel" %%% "discipline-munit" % "2.0.0-M3",
"org.typelevel" %%% "cats-time" % "0.5.1",
"eu.timepit" %%% "refined-cats" % "0.10.3",
),
testFrameworks += new TestFramework("munit.Framework"),
testOptions += {
Expand Down
43 changes: 0 additions & 43 deletions modules/refined/src/main/scala/codec/NumericCodecs.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk.refined.codec

import skunk.{ Codec, Encoder, Decoder }
import eu.timepit.refined.api.{ RefType, Validate }

trait RefTypeCodecs {
def refTypeCodec[T, P, F[_,_]](codecT: Codec[T])(
implicit validate: Validate[T, P], refType: RefType[F]): Codec[F[T, P]] =
codecT.eimap[F[T,P]](
refType.refine[P](_)(validate))(
refType.unwrap
)

def refTypeEncoder[T, P, F[_,_]](writeT: Encoder[T])(implicit refType: RefType[F]): Encoder[F[T,P]] =
writeT.contramap[F[T,P]](refType.unwrap)

def refTypeDecoder[T, P, F[_,_]](readT: Decoder[T])(
implicit validate: Validate[T, P], refType: RefType[F]): Decoder[F[T,P]] =
readT.emap[F[T,P]](refType.refine[P](_)(validate))
}

object refType extends RefTypeCodecs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk.refined.codec

import skunk.{ Codec, Encoder, Decoder }
import eu.timepit.refined.api.{ Refined, Validate }

trait RefinedCodecs {

def refinedCodec[T, P](codecT: Codec[T])(implicit v: Validate[T, P]): Codec[Refined[T, P]] =
refType.refTypeCodec[T, P, Refined](codecT)

def refinedDecoder[T, P](decoderT: Decoder[T])(implicit v: Validate[T, P]): Decoder[Refined[T, P]] =
refType.refTypeDecoder[T, P, Refined](decoderT)

def refinedEncoder[T, P](encoderT: Encoder[T]): Encoder[Refined[T, P]] =
refType.refTypeEncoder[T, P, Refined](encoderT)

}

object refined extends RefinedCodecs
26 changes: 26 additions & 0 deletions modules/refined/src/main/scala/skunk/refined/codec/Syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk.refined.codec

import skunk.{ Codec, Encoder, Decoder }
import eu.timepit.refined.api.{ Refined, Validate }


object syntax {
implicit class RefineCodecOps[T](val c: Codec[T]) {
def refine[P](implicit v: Validate[T, P]): Codec[Refined[T, P]] =
refined.refinedCodec(c)
}

implicit class RefineEncoderOps[T](val c: Encoder[T]) {
def refine[P]: Encoder[Refined[T, P]] =
refined.refinedEncoder(c)
}

implicit class RefineDecoderOps[T](val c: Decoder[T]) {
def refine[P](implicit v: Validate[T, P]): Decoder[Refined[T, P]] =
refined.refinedDecoder(c)
}
}
21 changes: 21 additions & 0 deletions modules/tests/shared/src/test/scala/codec/RefinedCodecTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package tests.codec

import skunk.codec.all._
import skunk.refined.codec.syntax._
import eu.timepit.refined.collection.NonEmpty
import eu.timepit.refined.types.string.NonEmptyString
import eu.timepit.refined.types.numeric.{PosInt, NonNaNDouble}
import eu.timepit.refined.numeric.{Positive, NonNaN}
import eu.timepit.refined.cats._

class RefinedCodecTest extends CodecTest {

roundtripTest(varchar.refine[NonEmpty])(NonEmptyString.unsafeFrom("foo"))
roundtripTest(int4.refine[Positive])(PosInt.unsafeFrom(42))
roundtripTest(float8.refine[NonNaN])(NonNaNDouble.unsafeFrom(2.0))

}

0 comments on commit c0303dd

Please sign in to comment.