Skip to content

Commit

Permalink
feat: add scala.collection.Seq codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrs committed Sep 14, 2023
1 parent 0ad7c3c commit 79856f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
7 changes: 5 additions & 2 deletions modules/core/src/main/scala/mess/codec/Decoder.scala
Expand Up @@ -7,6 +7,8 @@ import mess.Fmt
import mess.internal.ScalaVersionSpecifics._

import scala.annotation.tailrec
import scala.collection.Seq
import scala.collection.immutable
import scala.collection.mutable

trait Decoder[A] extends Serializable { self =>
Expand Down Expand Up @@ -128,7 +130,7 @@ private[codec] trait Decoder1 {
case Left(e) => Left(e)
}

@inline private[this] def decodeContainer[C[_], A](implicit
@inline private def decodeContainer[C[_], A](implicit
decodeA: Decoder[A],
factoryA: Factory[A, C[A]]
): Decoder[C[A]] =
Expand All @@ -150,7 +152,8 @@ private[codec] trait Decoder1 {
}
}

implicit def decodeSeq[A: Decoder]: Decoder[Seq[A]] = decodeContainer[Seq, A]
implicit def decodeSeq[A: Decoder]: Decoder[Seq[A]] = decodeContainer[Seq, A]
implicit def decodeISeq[A: Decoder]: Decoder[immutable.Seq[A]] = decodeContainer[immutable.Seq, A]

implicit def decodeSet[A: Decoder]: Decoder[Set[A]] = decodeContainer[Set, A]

Expand Down
5 changes: 5 additions & 0 deletions modules/core/src/main/scala/mess/codec/Encoder.scala
Expand Up @@ -5,6 +5,8 @@ import java.time.Instant
import mess.Fmt

import scala.annotation.tailrec
import scala.collection.Seq
import scala.collection.immutable
import scala.collection.mutable

trait Encoder[A] extends Serializable { self =>
Expand Down Expand Up @@ -74,6 +76,9 @@ private[codec] trait Encoder1 {
implicit final def encodeSeq[A: Encoder]: Encoder.AsArray[Seq[A]] =
a => Fmt.MArray(buildVector(a.iterator, Vector.newBuilder))

implicit final def encodeISeq[A: Encoder]: Encoder.AsArray[immutable.Seq[A]] =
a => Fmt.MArray(buildVector(a.iterator, Vector.newBuilder))

implicit final def encodeSet[A: Encoder]: Encoder.AsArray[Set[A]] =
a => Fmt.MArray(buildVector(a.iterator, Vector.newBuilder))

Expand Down
15 changes: 14 additions & 1 deletion modules/core/src/test/scala/mess/codec/DecoderSpec.scala
Expand Up @@ -4,6 +4,9 @@ import mess.Fmt
import mess.MsgpackHelper
import mess.codec.semiauto._

import scala.collection.Seq
import scala.collection.immutable

class DecoderSpec extends MsgpackHelper {
case class Bar(double: Double)

Expand Down Expand Up @@ -53,7 +56,7 @@ class DecoderSpec extends MsgpackHelper {
test("Decoder[Array[Byte]]") {
val m = Fmt.fromBytes(Array(0x11, 0x1a).map(_.toByte))
val xs = decode[Array[Byte]](m).toTry.get
assertEquals(xs.toSeq, Seq(0x11, 0x1a).map(_.toByte))
assertEquals(xs.toSeq, immutable.Seq(0x11, 0x1a).map(_.toByte))
}
test("Decoder[Char] failure") {
val msg = Fmt.fromString("ab")
Expand Down Expand Up @@ -188,6 +191,16 @@ class DecoderSpec extends MsgpackHelper {
val msg = Fmt.fromString("")
assertEquals(decode[Seq[Int]](msg), Left(TypeMismatchError("C[A]", msg)))
}
check("Decoder[immutable.Seq[A]]",
Seq(
(immutable.Seq(0 to 14: _*), Fmt.fromValues((0 to 14).map(Fmt.fromInt): _*)),
(immutable.Seq.empty[Int], Fmt.unit)
)
)
test("Decoder[immutable.Seq[A]] failure") {
val msg = Fmt.fromString("")
assertEquals(decode[immutable.Seq[Int]](msg), Left(TypeMismatchError("C[A]", msg)))
}
check("Decoder[Tuple2[A]]",
Seq(
((1, 2), Fmt.fromValues(Fmt.fromByte(1.toByte), Fmt.fromByte(2.toByte)))
Expand Down
10 changes: 10 additions & 0 deletions modules/core/src/test/scala/mess/codec/EncoderSpec.scala
Expand Up @@ -4,6 +4,9 @@ import mess.Fmt
import mess.MsgpackHelper
import org.msgpack.core.MessagePack

import scala.collection.Seq
import scala.collection.immutable

class EncoderSpec extends MsgpackHelper {
case class Bar(double: Double)

Expand Down Expand Up @@ -123,6 +126,13 @@ class EncoderSpec extends MsgpackHelper {
(Seq.empty[Int], Fmt.fromValues())
)
)
check(
"Encoder[immutable.Seq[A]]",
Seq(
(immutable.Seq(0 to 14: _*), Fmt.fromValues((0 to 14).map(a => Fmt.fromByte(a.toByte)): _*)),
(immutable.Seq.empty[Int], Fmt.fromValues())
)
)
check("Encoder[List[A]]",
Seq(
((0 to 14).toList, Fmt.fromValues((0 to 14).map(a => Fmt.fromByte(a.toByte)): _*))
Expand Down

0 comments on commit 79856f8

Please sign in to comment.