-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add readers and writers instances for cats non-empty collections #284
Merged
REDNBLACK
merged 4 commits into
tethys-json:master
from
Roman-Statsura:integration-cats
Apr 23, 2024
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions
26
...les/integrations/cats/src/main/scala-2.12/tethys/integration/cats/NonEmptySetReader.scala
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,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" | ||
) | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...es/integrations/cats/src/main/scala-2.13+/tethys/integration/cats/NonEmptySetReader.scala
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,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" | ||
) | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
modules/integrations/cats/src/main/scala-3/tethys.integration.cats/NonEmptySetReader.scala
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,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" | ||
) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
modules/integrations/cats/src/main/scala/tethys/integration/cats/instances.scala
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,6 @@ | ||
package tethys.integration.cats | ||
|
||
import tethys.integration.cats.readers.CatsReaders | ||
import tethys.integration.cats.writers.CatsWriters | ||
|
||
object instances extends CatsReaders with CatsWriters with NonEmptySetReader |
56 changes: 56 additions & 0 deletions
56
modules/integrations/cats/src/main/scala/tethys/integration/cats/readers/CatsReaders.scala
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,56 @@ | ||
package tethys.integration.cats.readers | ||
|
||
import cats.data._ | ||
import tethys.readers.tokens.TokenIterator | ||
import tethys.readers.{FieldName, ReaderError} | ||
import tethys.JsonReader | ||
import tethys.JsonReader.iterableReader | ||
|
||
trait CatsReaders { | ||
|
||
implicit def readerForNel[T: JsonReader]: JsonReader[NonEmptyList[T]] = | ||
new JsonReader[NonEmptyList[T]] { | ||
override def read( | ||
it: TokenIterator | ||
)(implicit fieldName: FieldName): NonEmptyList[T] = | ||
NonEmptyList.fromList(JsonReader[List[T]].read(it)) match { | ||
case Some(value) => value | ||
case None => | ||
ReaderError.wrongJson( | ||
s"List is empty and can't be converted to NonEmptyList" | ||
) | ||
} | ||
} | ||
|
||
implicit def readerForNev[T: JsonReader]: JsonReader[NonEmptyVector[T]] = | ||
new JsonReader[NonEmptyVector[T]] { | ||
override def read( | ||
it: TokenIterator | ||
)(implicit fieldName: FieldName): NonEmptyVector[T] = | ||
NonEmptyVector.fromVector(JsonReader[Vector[T]].read(it)) match { | ||
case Some(value) => value | ||
case None => | ||
ReaderError.wrongJson( | ||
s"Vector is empty and can't be converted to NonEmptyVector" | ||
) | ||
} | ||
} | ||
|
||
implicit def readerForChain[T: JsonReader]: JsonReader[Chain[T]] = | ||
JsonReader[Seq[T]].map(Chain.fromIterableOnce) | ||
|
||
implicit def readerForNec[T: JsonReader]: JsonReader[NonEmptyChain[T]] = | ||
new JsonReader[NonEmptyChain[T]] { | ||
override def read( | ||
it: TokenIterator | ||
)(implicit fieldName: FieldName): NonEmptyChain[T] = | ||
NonEmptyChain.fromChain(JsonReader[Chain[T]].read(it)) match { | ||
case Some(value) => value | ||
case None => | ||
ReaderError.wrongJson( | ||
s"Chain is empty and can't be converted to NonEmptyChain" | ||
) | ||
} | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
modules/integrations/cats/src/main/scala/tethys/integration/cats/writers/CatsWriters.scala
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,21 @@ | ||
package tethys.integration.cats.writers | ||
|
||
import cats.data._ | ||
import tethys.JsonWriter | ||
|
||
trait CatsWriters { | ||
implicit def writerForNev[T: JsonWriter]: JsonWriter[NonEmptyVector[T]] = | ||
JsonWriter[Vector[T]].contramap(_.toVector) | ||
|
||
implicit def writerForNel[T: JsonWriter]: JsonWriter[NonEmptyList[T]] = | ||
JsonWriter[List[T]].contramap(_.toList) | ||
|
||
implicit def writerForNes[T: JsonWriter]: JsonWriter[NonEmptySet[T]] = | ||
JsonWriter[Set[T]].contramap(_.toSortedSet) | ||
|
||
implicit def writerForChain[T: JsonWriter]: JsonWriter[Chain[T]] = | ||
JsonWriter[List[T]].contramap(_.toList) | ||
|
||
implicit def writerForNec[T: JsonWriter]: JsonWriter[NonEmptyChain[T]] = | ||
JsonWriter[Chain[T]].contramap(_.toChain) | ||
} |
44 changes: 44 additions & 0 deletions
44
modules/integrations/cats/src/test/scala/tethys/integration/cats/CatsSupportTests.scala
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,44 @@ | ||
package tethys.integration.cats | ||
|
||
import cats.data._ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import tethys.commons.TokenNode._ | ||
import tethys.integration.cats.instances._ | ||
import tethys.readers.ReaderError | ||
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[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 "write non-empty" in { | ||
nev.asTokenList shouldBe arr("a", "b") | ||
nel.asTokenList shouldBe arr(1, 2) | ||
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 "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(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]]) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set 0.28.4