-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5800cbd
commit fa8c6e6
Showing
6 changed files
with
88 additions
and
27 deletions.
There are no files selected for viewing
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" | ||
) | ||
} | ||
} | ||
} |
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
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
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