-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add arrow-core-serialization SerializersModule (#3413)
* feat: add SerializersModule for contextual/root usage of Arrow types not including Option due to type restriction * feat: remove `Any` restriction on `OptionSerializer` allows adding `Option` support to `ArrowModule`
- Loading branch information
Showing
5 changed files
with
119 additions
and
4 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
13 changes: 13 additions & 0 deletions
13
...ow-core-serialization/src/commonMain/kotlin/arrow/core/serialization/SerializersModule.kt
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,13 @@ | ||
package arrow.core.serialization | ||
|
||
import arrow.core.* | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
public val ArrowModule: SerializersModule = SerializersModule { | ||
contextual(Either::class) { (a, b) -> EitherSerializer(a, b) } | ||
contextual(Ior::class) { (a, b) -> IorSerializer(a, b) } | ||
contextual(NonEmptyList::class) { (t) -> NonEmptyListSerializer(t) } | ||
contextual(NonEmptySet::class) { (t) -> NonEmptySetSerializer(t) } | ||
contextual(Option::class) { (t) -> OptionSerializer(t) } | ||
} | ||
|
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
81 changes: 81 additions & 0 deletions
81
...ore/arrow-core-serialization/src/commonTest/kotlin/arrow/core/serialization/ModuleTest.kt
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,81 @@ | ||
package arrow.core.serialization | ||
|
||
import arrow.core.* | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.int | ||
import io.kotest.property.arbitrary.map | ||
import io.kotest.property.arbitrary.string | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.nullable | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import kotlinx.serialization.json.encodeToJsonElement | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlin.test.Test | ||
|
||
|
||
@Serializable | ||
data class ContextualEitherInside<A, B>(@Contextual val thing: Either<A, B>) | ||
|
||
@Serializable | ||
data class ContextualIorInside<A, B>(@Contextual val thing: Ior<A, B>) | ||
|
||
@Serializable | ||
data class ContextualOptionInside<A>(@Contextual val thing: Option<A>) | ||
|
||
@Serializable | ||
data class ContextualNonEmptyListInside<A>(@Contextual val thing: NonEmptyList<A>) | ||
|
||
@Serializable | ||
data class ContextualNonEmptySetInside<A>(@Contextual val thing: NonEmptySet<A>) | ||
|
||
class ModuleTest { | ||
private val jsonWithModule = Json { | ||
serializersModule = SerializersModule { | ||
include(ArrowModule) | ||
} | ||
} | ||
|
||
@Test | ||
fun backAgainEither() = | ||
backAgain(Arb.either(Arb.string(), Arb.int()), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainIor() = | ||
backAgain(Arb.ior(Arb.string(), Arb.int()), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainOption() = | ||
backAgain(Arb.option(Arb.string()), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainNonEmptyList() = | ||
backAgain(Arb.nonEmptyList(Arb.int()), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainNonEmptySet() = | ||
backAgain(Arb.nonEmptySet(Arb.int()), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainContextualEither() = | ||
backAgain(Arb.either(Arb.string(), Arb.int()).map(::ContextualEitherInside), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainContextualIor() = | ||
backAgain(Arb.ior(Arb.string(), Arb.int()).map(::ContextualIorInside), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainContextualOption() = | ||
backAgain(Arb.option(Arb.string()).map(::ContextualOptionInside), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainContextualNonEmptyList() = | ||
backAgain(Arb.nonEmptyList(Arb.int()).map(::ContextualNonEmptyListInside), jsonWithModule) | ||
|
||
@Test | ||
fun backAgainContextualNonEmptySet() = | ||
backAgain(Arb.nonEmptySet(Arb.int()).map(::ContextualNonEmptySetInside), jsonWithModule) | ||
} |