From 2aff796e62ea45f8a73ffbe25b31a2bb12791232 Mon Sep 17 00:00:00 2001 From: Lionel Briand Date: Fri, 8 Dec 2023 11:08:05 +0100 Subject: [PATCH] Update library version, refactor code and add documentation. --- README.MD | 12 ++++++------ gradle.properties | 2 +- .../kotlin/net/orandja/either/EitherSerializer.kt | 8 ++++++++ src/commonMain/kotlin/net/orandja/either/Left.kt | 2 -- .../kotlin/net/orandja/either/LeftSerializer.kt | 8 ++++++++ .../kotlin/net/orandja/either/OptionSerializer.kt | 1 - src/commonMain/kotlin/net/orandja/either/Right.kt | 3 --- .../kotlin/net/orandja/either/RightSerializer.kt | 8 ++++++++ src/commonTest/kotlin/net/orandja/test/EitherJson.kt | 3 +-- src/commonTest/kotlin/net/orandja/test/OptionJson.kt | 5 ++--- 10 files changed, 34 insertions(+), 18 deletions(-) diff --git a/README.MD b/README.MD index 77b061f..6d6a208 100644 --- a/README.MD +++ b/README.MD @@ -25,7 +25,7 @@ repositories { } val commonMain by getting { dependencies { - implementation("net.orandja.kt:either:1.1.1") + implementation("net.orandja.kt:either:1.2.0") } } ``` @@ -37,7 +37,7 @@ repositories { mavenCentral() } dependencies { - implementation("net.orandja.kt:either:1.1.1") + implementation("net.orandja.kt:either:1.2.0") } ``` @@ -229,11 +229,11 @@ assert(b is None) ```kotlin val option: Option = Some("value") -val a: Either = option.letNoneAsRight { 0 } -val b: Either = option.letNoneAsLeft { 0 } +val _: Either = option.letNoneAsRight { 0 } +val _: Either = option.letNoneAsLeft { 0 } -val c: Either = option.letAsLeft(onSome = { 0L }, onNone = {}) -val d: Either = option.letAsRight(onSome = { 0L }, onNone = {}) +val _: Either = option.letAsLeft(onSome = { 0L }, onNone = {}) +val _: Either = option.letAsRight(onSome = { 0L }, onNone = {}) ``` If you know the type of the Option, just create a left or right value with it. `val either = Left(option.value)` diff --git a/gradle.properties b/gradle.properties index 1b2eb25..7d206a4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ kotlin.code.style=official kotlin.js.compiler=ir # Global information group=net.orandja.kt -version=1.1.1 +version=1.2.0 # Dependencies version.serialization=1.6.2 # Artifact related diff --git a/src/commonMain/kotlin/net/orandja/either/EitherSerializer.kt b/src/commonMain/kotlin/net/orandja/either/EitherSerializer.kt index f2b2b9d..b521273 100644 --- a/src/commonMain/kotlin/net/orandja/either/EitherSerializer.kt +++ b/src/commonMain/kotlin/net/orandja/either/EitherSerializer.kt @@ -6,6 +6,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.buildClassSerialDescriptor import kotlinx.serialization.encoding.* +/** + * Serializer for both Left and Right implementation of the Either class. + * + * @param L the type of the value on the left side of [Either] + * @param R the type of the value on the right side of [Either] + * @property leftDelegate the serializer for the type [L] + * @property rightDelegate the serializer for the type [R] + */ class EitherSerializer( private val leftDelegate: KSerializer, private val rightDelegate: KSerializer, diff --git a/src/commonMain/kotlin/net/orandja/either/Left.kt b/src/commonMain/kotlin/net/orandja/either/Left.kt index ec1da31..1c085f3 100644 --- a/src/commonMain/kotlin/net/orandja/either/Left.kt +++ b/src/commonMain/kotlin/net/orandja/either/Left.kt @@ -17,8 +17,6 @@ data class Left(override val left: L) : Either() { companion object { /** * Static instance of [Left]'s Unit. - * - * Can be useful when result is [Left] without the need of a specific type. (Example: `Either`) */ @JvmStatic val Unit = Left(kotlin.Unit) diff --git a/src/commonMain/kotlin/net/orandja/either/LeftSerializer.kt b/src/commonMain/kotlin/net/orandja/either/LeftSerializer.kt index ca4a5f2..d92c3ac 100644 --- a/src/commonMain/kotlin/net/orandja/either/LeftSerializer.kt +++ b/src/commonMain/kotlin/net/orandja/either/LeftSerializer.kt @@ -6,6 +6,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.buildClassSerialDescriptor import kotlinx.serialization.encoding.* +/** + * Serializer for the Left implementation of the Either class. + * + * This serializer is responsible for serializing and deserializing values of type Left. + * + * @param L the type parameter of the Left class + * @property delegate the serializer for values of type L + */ class LeftSerializer( private val delegate: KSerializer, ) : KSerializer> { diff --git a/src/commonMain/kotlin/net/orandja/either/OptionSerializer.kt b/src/commonMain/kotlin/net/orandja/either/OptionSerializer.kt index fa60380..c3c2b0c 100644 --- a/src/commonMain/kotlin/net/orandja/either/OptionSerializer.kt +++ b/src/commonMain/kotlin/net/orandja/either/OptionSerializer.kt @@ -18,7 +18,6 @@ import kotlinx.serialization.encoding.Encoder * * Doing it this way allows the deserializer fallback to none when the field is not present inside a json object. * If the field is present and null, it deserializes to Some(null) - * */ class OptionSerializer(private val delegate: KSerializer) : KSerializer> { override val descriptor: SerialDescriptor = delegate.descriptor diff --git a/src/commonMain/kotlin/net/orandja/either/Right.kt b/src/commonMain/kotlin/net/orandja/either/Right.kt index cd4d9d6..98ad88f 100644 --- a/src/commonMain/kotlin/net/orandja/either/Right.kt +++ b/src/commonMain/kotlin/net/orandja/either/Right.kt @@ -16,9 +16,6 @@ data class Right(override val right: R) : Either() { companion object { /** * Static instance of [Right]'s Unit. - * - * - * Can be useful when result is [Right] without the need of a specific type. (Example: `Either`) */ @JvmStatic val Unit = Right(kotlin.Unit) diff --git a/src/commonMain/kotlin/net/orandja/either/RightSerializer.kt b/src/commonMain/kotlin/net/orandja/either/RightSerializer.kt index 2070bea..f08c760 100644 --- a/src/commonMain/kotlin/net/orandja/either/RightSerializer.kt +++ b/src/commonMain/kotlin/net/orandja/either/RightSerializer.kt @@ -7,6 +7,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.buildClassSerialDescriptor import kotlinx.serialization.encoding.* +/** + * Serializer for the Right implementation of the Either class. + * + * This serializer is responsible for serializing and deserializing values of type Right. + * + * @param R the type parameter of the Right class + * @property delegate the serializer for values of type R + */ class RightSerializer( private val delegate: KSerializer, ) : KSerializer> { diff --git a/src/commonTest/kotlin/net/orandja/test/EitherJson.kt b/src/commonTest/kotlin/net/orandja/test/EitherJson.kt index 369f857..3e72777 100644 --- a/src/commonTest/kotlin/net/orandja/test/EitherJson.kt +++ b/src/commonTest/kotlin/net/orandja/test/EitherJson.kt @@ -1,6 +1,5 @@ package net.orandja.test -import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import net.orandja.either.Either @@ -30,7 +29,7 @@ class EitherJson { } @Test - fun deserializationLeft() { + fun deserialize() { assertEquals(lData, codec.decodeFromString(lJson)) assertEquals(leData, codec.decodeFromString(lJson)) assertEquals(rData, codec.decodeFromString(rJson)) diff --git a/src/commonTest/kotlin/net/orandja/test/OptionJson.kt b/src/commonTest/kotlin/net/orandja/test/OptionJson.kt index 48ac75b..4fe8bcf 100644 --- a/src/commonTest/kotlin/net/orandja/test/OptionJson.kt +++ b/src/commonTest/kotlin/net/orandja/test/OptionJson.kt @@ -1,7 +1,6 @@ package net.orandja.test import kotlinx.serialization.Serializable -import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import net.orandja.either.None @@ -25,14 +24,14 @@ class OptionJson { private val someJson = """{"value":"value"}""" @Test - fun serialization() { + fun serialize() { assertEquals(noneJson, codec.encodeToString(noneData)) assertEquals(nullJson, codec.encodeToString(nullData)) assertEquals(someJson, codec.encodeToString(someData)) } @Test - fun deserialization() { + fun deserialize() { assertEquals(noneData, codec.decodeFromString(noneJson)) assertEquals(nullData, codec.decodeFromString(nullJson)) assertEquals(someData, codec.decodeFromString(someJson))