-
Notifications
You must be signed in to change notification settings - Fork 163
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 #899 from lenguyenthanh/revive-refined-support
Flesh out Refined module a bit ( 2 years later :D)
- Loading branch information
Showing
6 changed files
with
98 additions
and
44 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 was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
modules/refined/src/main/scala/skunk/refined/codec/RefTypeCodecs.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,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 |
23 changes: 23 additions & 0 deletions
23
modules/refined/src/main/scala/skunk/refined/codec/RefinedCodecs.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,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
26
modules/refined/src/main/scala/skunk/refined/codec/Syntax.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,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
21
modules/tests/shared/src/test/scala/codec/RefinedCodecTest.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,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)) | ||
|
||
} |