forked from arrow-kt/arrow
-
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.
* fix and try java instances * move collection codecs to its own file * create and test java time instances * add todos * generate encoders/decoders properly * format
- Loading branch information
1 parent
c15cc32
commit 520ed1d
Showing
11 changed files
with
585 additions
and
497 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
101 changes: 101 additions & 0 deletions
101
helios-core/src/main/kotlin/helios/instances/instancesCollections.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,101 @@ | ||
package helios.instances | ||
|
||
import arrow.core.* | ||
import arrow.core.extensions.either.applicative.applicative | ||
import arrow.core.extensions.either.applicative.map2 | ||
import arrow.data.extensions.list.foldable.foldLeft | ||
import arrow.data.extensions.list.traverse.sequence | ||
import arrow.data.fix | ||
import arrow.extension | ||
import helios.core.* | ||
import helios.typeclasses.Decoder | ||
import helios.typeclasses.Encoder | ||
import helios.typeclasses.KeyDecoder | ||
import helios.typeclasses.KeyEncoder | ||
import kotlin.collections.mapOf | ||
|
||
|
||
@extension | ||
interface ListEncoderInstance<in A> : Encoder<List<A>> { | ||
|
||
fun encoderA(): Encoder<A> | ||
|
||
override fun List<A>.encode(): Json = | ||
JsArray(map { encoderA().run { it.encode() } }) | ||
|
||
companion object { | ||
operator fun <A> invoke(encoderA: Encoder<A>): Encoder<List<A>> = | ||
object : ListEncoderInstance<A> { | ||
override fun encoderA(): Encoder<A> = encoderA | ||
} | ||
} | ||
|
||
} | ||
|
||
@extension | ||
interface ListDecoderInstance<out A> : Decoder<List<A>> { | ||
|
||
fun decoderA(): Decoder<A> | ||
|
||
override fun decode(value: Json): Either<DecodingError, List<A>> = | ||
value.asJsArray().toList() | ||
.flatMap { arr -> | ||
arr.value.map { decoderA().decode(it) } | ||
}.sequence(Either.applicative()).fix().map { it.fix().toList() } | ||
|
||
companion object { | ||
operator fun <A> invoke(decoderA: Decoder<A>): Decoder<List<A>> = | ||
object : ListDecoderInstance<A> { | ||
override fun decoderA(): Decoder<A> = decoderA | ||
} | ||
} | ||
|
||
} | ||
|
||
@extension | ||
interface MapEncoderInstance<A, B> : Encoder<Map<A, B>> { | ||
|
||
fun keyEncoderA(): KeyEncoder<A> | ||
fun encoderB(): Encoder<B> | ||
|
||
override fun Map<A, B>.encode(): Json = | ||
JsObject(this.map { (key, value) -> (keyEncoderA().run { key.keyEncode() } to encoderB().run { value.encode() }) }.toMap()) | ||
|
||
companion object { | ||
operator fun <A, B> invoke(keyEncoderA: KeyEncoder<A>, encoderB: Encoder<B>): Encoder<Map<A, B>> = | ||
object : MapEncoderInstance<A, B>, Encoder<Map<A, B>> { | ||
override fun keyEncoderA(): KeyEncoder<A> = keyEncoderA | ||
override fun encoderB(): Encoder<B> = encoderB | ||
} | ||
} | ||
|
||
} | ||
|
||
@extension | ||
interface MapDecoderInstance<A, B> : Decoder<Map<A, B>> { | ||
|
||
fun keyDecoderA(): KeyDecoder<A> | ||
fun decoderB(): Decoder<B> | ||
|
||
override fun decode(value: Json): Either<DecodingError, Map<A, B>> = | ||
value.asJsObject().fold({ ObjectDecodingError(value).left() }, { obj -> | ||
obj.value.map { (key, value) -> | ||
val maybeKey: Either<DecodingError, A> = | ||
Json.parseFromString(key).mapLeft { StringDecodingError(value) }.flatMap { keyDecoderA().keyDecode(it) } | ||
val maybeValue: Either<DecodingError, B> = decoderB().decode(value) | ||
maybeKey.map2(maybeValue) { mapOf(it.toPair()) } | ||
} | ||
.foldLeft<Either<DecodingError, Map<A, B>>, Either<DecodingError, Map<A, B>>>(arrow.core.mapOf<A, B>().right()) { acc, either -> | ||
acc.map2(either) { it.a + it.b } | ||
} | ||
}) | ||
|
||
companion object { | ||
operator fun <A, B> invoke(keyDecoderA: KeyDecoder<A>, decoderB: Decoder<B>): Decoder<Map<A, B>> = | ||
object : MapDecoderInstance<A, B> { | ||
override fun keyDecoderA(): KeyDecoder<A> = keyDecoderA | ||
override fun decoderB(): Decoder<B> = decoderB | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.