Skip to content

Commit

Permalink
fix problem with SortedSet
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman-Statsura committed Apr 12, 2024
1 parent 5800cbd commit fa8c6e6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tethys.integration.cats

import cats.data.NonEmptySet
import tethys.JsonReader
import tethys.readers.{FieldName, ReaderError}
import tethys.readers.tokens.TokenIterator

import scala.collection.immutable.{Seq, SortedSet}

trait NonEmptySetReader {

implicit def readerForNes[T: JsonReader: Ordering]
: JsonReader[NonEmptySet[T]] =
new JsonReader[NonEmptySet[T]] {
override def read(
it: TokenIterator
)(implicit fieldName: FieldName): NonEmptySet[T] =
NonEmptySet.fromSet(SortedSet(JsonReader[Seq[T]].read(it): _*)) match {
case Some(value) => value
case None =>
ReaderError.wrongJson(
s"Seq is empty and can't be converted to NonEmptySet"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tethys.integration.cats

import cats.data.NonEmptySet
import tethys.JsonReader
import tethys.readers.{FieldName, ReaderError}
import tethys.readers.tokens.TokenIterator

import scala.collection.immutable.SortedSet

trait NonEmptySetReader {

implicit def readerForNes[T: JsonReader: Ordering]
: JsonReader[NonEmptySet[T]] =
new JsonReader[NonEmptySet[T]] {
override def read(
it: TokenIterator
)(implicit fieldName: FieldName): NonEmptySet[T] =
NonEmptySet.fromSet(SortedSet.from(JsonReader[Seq[T]].read(it))) match {
case Some(value) => value
case None =>
ReaderError.wrongJson(
s"Seq is empty and can't be converted to NonEmptySet"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tethys.integration.cats

import cats.data.NonEmptySet
import tethys.JsonReader
import tethys.readers.{FieldName, ReaderError}
import tethys.readers.tokens.TokenIterator

import scala.collection.immutable.SortedSet

trait NonEmptySetReader {

implicit def readerForNes[T: JsonReader: Ordering]
: JsonReader[NonEmptySet[T]] =
new JsonReader[NonEmptySet[T]] {
override def read(
it: TokenIterator
)(implicit fieldName: FieldName): NonEmptySet[T] =
NonEmptySet.fromSet(SortedSet.from(JsonReader[Seq[T]].read(it))) match {
case Some(value) => value
case None =>
ReaderError.wrongJson(
s"Seq is empty and can't be converted to NonEmptySet"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package tethys.integration.cats
import tethys.integration.cats.readers.CatsReaders
import tethys.integration.cats.writers.CatsWriters

object instances extends CatsReaders with CatsWriters
object instances extends CatsReaders with CatsWriters with NonEmptySetReader
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import tethys.readers.{FieldName, ReaderError}
import tethys.JsonReader
import tethys.JsonReader.iterableReader

import scala.collection.immutable.SortedSet

trait CatsReaders {

implicit def readerForNel[T: JsonReader]: JsonReader[NonEmptyList[T]] =
Expand Down Expand Up @@ -38,23 +36,8 @@ trait CatsReaders {
}
}

implicit def readerForNes[T: JsonReader: Ordering]
: JsonReader[NonEmptySet[T]] =
new JsonReader[NonEmptySet[T]] {
override def read(
it: TokenIterator
)(implicit fieldName: FieldName): NonEmptySet[T] =
NonEmptySet.fromSet(SortedSet.from(JsonReader[Set[T]].read(it))) match {
case Some(value) => value
case None =>
ReaderError.wrongJson(
s"Set is empty and can't be converted to NonEmptySet"
)
}
}

implicit def readerForChain[T: JsonReader]: JsonReader[Chain[T]] =
JsonReader[Seq[T]].map(Chain.fromSeq)
JsonReader[Seq[T]].map(Chain.fromIterableOnce)

implicit def readerForNec[T: JsonReader]: JsonReader[NonEmptyChain[T]] =
new JsonReader[NonEmptyChain[T]] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ import tethys.writers.tokens.SimpleTokenWriter._
class CatsSupportTests extends AnyFlatSpec with Matchers {
val nev: NonEmptyVector[String] = NonEmptyVector.of("a", "b")
val nel: NonEmptyList[Int] = NonEmptyList.of(1, 2)
val nes: NonEmptySet[String] = NonEmptySet.of("a", "b")
val chain: Chain[Int] = Chain.fromSeq(Seq(1, 2, 3))
val nes: NonEmptySet[Int] = NonEmptySet.of(1, 2, 3, 4)
val chain: Chain[Int] = Chain.fromIterableOnce(Seq(1, 2, 3))
val nec: NonEmptyChain[String] = NonEmptyChain.of("a", "b", "c")

behavior of "CatsWriters"
it should "work with non empty" in {
it should "write non-empty" in {
nev.asTokenList shouldBe arr("a", "b")
nel.asTokenList shouldBe arr(1, 2)
nes.asTokenList shouldBe arr("a", "b")
nes.asTokenList shouldBe arr(1, 2, 3, 4)
chain.asTokenList shouldBe arr(1, 2, 3)
nec.asTokenList shouldBe arr("a", "b", "c")
}

behavior of "CatsReaders"
it should "work with non empty" in {
it should "read non-empty" in {
nev shouldBe arr("a", "b").tokensAs[NonEmptyVector[String]]
assertThrows[ReaderError](Nil.tokensAs[NonEmptyVector[String]])

nel shouldBe arr(1, 2).tokensAs[NonEmptyList[Int]]
assertThrows[ReaderError](Nil.tokensAs[NonEmptyList[Int]])

nes shouldBe arr("a", "b").tokensAs[NonEmptySet[String]]
assertThrows[ReaderError](Nil.tokensAs[NonEmptySet[String]])
nes shouldBe arr(1, 2, 3, 4).tokensAs[NonEmptySet[Int]]
nes shouldBe arr(4, 4, 1, 3, 3, 2).tokensAs[NonEmptySet[Int]]
assertThrows[ReaderError](Nil.tokensAs[NonEmptySet[Int]])

chain shouldBe arr(1, 2, 3).tokensAs[Chain[Int]]

nec shouldBe arr("a", "b", "c").tokensAs[NonEmptyChain[String]]
assertThrows[ReaderError](Nil.tokensAs[NonEmptyChain[String]])

}
}

0 comments on commit fa8c6e6

Please sign in to comment.