forked from softwaremill/tapir
-
Notifications
You must be signed in to change notification settings - Fork 0
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 softwaremill#2382 from massimosiani/support-monix-…
…newtypes softwaremill#2361 add monix newtypes integration
- Loading branch information
Showing
9 changed files
with
140 additions
and
22 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 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 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 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 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
16 changes: 16 additions & 0 deletions
16
.../monix-newtype/src/main/scala/sttp/tapir/codec/monix/newtype/TapirCodecMonixNewType.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,16 @@ | ||
package sttp.tapir.codec.monix.newtype | ||
|
||
import monix.newtypes.{HasBuilder, HasExtractor} | ||
import sttp.tapir._ | ||
|
||
trait TapirCodecMonixNewType { | ||
implicit def schemaForMonixNewType[T, S](implicit ev1: HasExtractor.Aux[T, S], ev2: HasBuilder.Aux[T, S], schema: Schema[S]): Schema[T] = | ||
schema.map(ev2.build(_).toOption)(ev1.extract(_)) | ||
|
||
implicit def codecForMonixNewType[L, A, B, CF <: CodecFormat](implicit | ||
ev1: HasExtractor.Aux[B, A], | ||
ev2: HasBuilder.Aux[B, A], | ||
codec: Codec[L, A, CF] | ||
): Codec[L, B, CF] = | ||
codec.mapDecode(v => DecodeResult.fromEitherString(v.toString, ev2.build(v).left.map(_.toReadableString)))(ev1.extract(_)) | ||
} |
3 changes: 3 additions & 0 deletions
3
integrations/monix-newtype/src/main/scala/sttp/tapir/codec/monix/newtype/newtype.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,3 @@ | ||
package sttp.tapir.codec.monix | ||
|
||
package object newtype extends TapirCodecMonixNewType |
56 changes: 56 additions & 0 deletions
56
...ix-newtype/src/test/scala/sttp/tapir/codec/monix/newtype/TapirCodecMonixNewTypeTest.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,56 @@ | ||
package sttp.tapir.codec.monix.newtype | ||
|
||
import monix.newtypes._ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.tapir.Codec.PlainCodec | ||
import sttp.tapir.CodecFormat.TextPlain | ||
import sttp.tapir.DecodeResult.Value | ||
import sttp.tapir.{Codec, Schema} | ||
|
||
class TapirCodecMonixNewTypeTest extends AnyFlatSpec with Matchers { | ||
import TapirCodecMonixNewTypeTest._ | ||
|
||
it should "find schema for newtype which is equal to the schema of its underlying value" in { | ||
implicitly[Schema[Foo]] shouldBe Schema.schemaForString | ||
} | ||
|
||
it should "find schema for newsubtype which is equal to the schema of its underlying value" in { | ||
implicitly[Schema[Bar]] shouldBe Schema.schemaForInt | ||
} | ||
|
||
"Provided PlainText codec for a newtype" should "equal to the codec of its underlying value" in { | ||
val newTypeCodec = implicitly[PlainCodec[Foo]] | ||
newTypeCodec.schema shouldBe Codec.string.schema | ||
newTypeCodec.format shouldBe Codec.string.format | ||
} | ||
|
||
it should "correctly deserialize everything it serialize" in { | ||
val newTypeCodec = implicitly[Codec[String, Foo, TextPlain]] | ||
val foo = Foo("foo") | ||
|
||
newTypeCodec.decode(newTypeCodec.encode(foo)) shouldBe Value(foo) | ||
} | ||
|
||
"Provided PlainText codec for a newsubtype" should "equal to the codec of its underlying value" in { | ||
val newSubTypeCodec = implicitly[PlainCodec[Bar]] | ||
newSubTypeCodec.schema shouldBe Codec.int.schema | ||
newSubTypeCodec.format shouldBe Codec.int.format | ||
} | ||
|
||
it should "correctly deserialize everything it serialize" in { | ||
val newSubTypeCodec = implicitly[PlainCodec[Bar]] | ||
val bar = Bar(1) | ||
|
||
newSubTypeCodec.decode(newSubTypeCodec.encode(bar)) shouldBe Value(bar) | ||
} | ||
|
||
} | ||
|
||
object TapirCodecMonixNewTypeTest { | ||
type Foo = Foo.Type | ||
object Foo extends NewtypeWrapped[String] | ||
|
||
type Bar = Bar.Type | ||
object Bar extends NewsubtypeWrapped[Int] | ||
} |
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