Skip to content

Commit

Permalink
Update library version, refactor code and add documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Briand committed Dec 8, 2023
1 parent c1cdbab commit 2aff796
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
```
Expand All @@ -37,7 +37,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation("net.orandja.kt:either:1.1.1")
implementation("net.orandja.kt:either:1.2.0")
}
```

Expand Down Expand Up @@ -229,11 +229,11 @@ assert(b is None)
```kotlin
val option: Option<String> = Some("value")

val a: Either<String, Int> = option.letNoneAsRight { 0 }
val b: Either<Int, String> = option.letNoneAsLeft { 0 }
val _: Either<String, Int> = option.letNoneAsRight { 0 }
val _: Either<Int, String> = option.letNoneAsLeft { 0 }

val c: Either<Long, Unit> = option.letAsLeft(onSome = { 0L }, onNone = {})
val d: Either<Unit, Long> = option.letAsRight(onSome = { 0L }, onNone = {})
val _: Either<Long, Unit> = option.letAsLeft(onSome = { 0L }, onNone = {})
val _: Either<Unit, Long> = 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)`
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/net/orandja/either/EitherSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<L, R>(
private val leftDelegate: KSerializer<L>,
private val rightDelegate: KSerializer<R>,
Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/net/orandja/either/Left.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ data class Left<out L>(override val left: L) : Either<L, Nothing>() {
companion object {
/**
* Static instance of [Left]'s Unit.
*
* Can be useful when result is [Left] without the need of a specific type. (Example: `Either<Unit, Failure>`)
*/
@JvmStatic
val Unit = Left(kotlin.Unit)
Expand Down
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/net/orandja/either/LeftSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<L>.
*
* @param L the type parameter of the Left class
* @property delegate the serializer for values of type L
*/
class LeftSerializer<L>(
private val delegate: KSerializer<L>,
) : KSerializer<Left<L>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(private val delegate: KSerializer<T>) : KSerializer<Option<T>> {
override val descriptor: SerialDescriptor = delegate.descriptor
Expand Down
3 changes: 0 additions & 3 deletions src/commonMain/kotlin/net/orandja/either/Right.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ data class Right<out R>(override val right: R) : Either<Nothing, R>() {
companion object {
/**
* Static instance of [Right]'s Unit.
*
*
* Can be useful when result is [Right] without the need of a specific type. (Example: `Either<Failure, Unit>`)
*/
@JvmStatic
val Unit = Right(kotlin.Unit)
Expand Down
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/net/orandja/either/RightSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>.
*
* @param R the type parameter of the Right class
* @property delegate the serializer for values of type R
*/
class RightSerializer<R>(
private val delegate: KSerializer<R>,
) : KSerializer<Right<R>> {
Expand Down
3 changes: 1 addition & 2 deletions src/commonTest/kotlin/net/orandja/test/EitherJson.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 2 additions & 3 deletions src/commonTest/kotlin/net/orandja/test/OptionJson.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 2aff796

Please sign in to comment.