Skip to content

Commit

Permalink
cats instances more discoverable
Browse files Browse the repository at this point in the history
similar to what was done in cats, add the cats instances to the companion objects.
Those instances are better discoverable without additional import.
And the scala compilation is faster.
  • Loading branch information
yanns committed Jun 23, 2021
1 parent c7fe416 commit 99c9fb0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait FromJSON[@specialized A] extends Serializable {
val fields: Set[String] = FromJSON.emptyFieldsSet
}

object FromJSON {
object FromJSON extends FromJSONInstances {

private[FromJSON] val emptyFieldsSet: Set[String] = Set.empty

Expand Down
2 changes: 1 addition & 1 deletion json/json-core/src/main/scala/io/sphere/json/JSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.annotation.implicitNotFound
@implicitNotFound("Could not find an instance of JSON for ${A}")
trait JSON[A] extends FromJSON[A] with ToJSON[A]

object JSON extends JSONLowPriorityImplicits {
object JSON extends JSONInstances with JSONLowPriorityImplicits {
@inline def apply[A](implicit instance: JSON[A]): JSON[A] = instance
}

Expand Down
2 changes: 1 addition & 1 deletion json/json-core/src/main/scala/io/sphere/json/ToJSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait ToJSON[@specialized A] extends Serializable {

class JSONWriteException(msg: String) extends JSONException(msg)

object ToJSON {
object ToJSON extends ToJSONInstances {

@inline def apply[A](implicit instance: ToJSON[A]): ToJSON[A] = instance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import org.json4s.JValue

/** Cats instances for [[JSON]], [[FromJSON]] and [[ToJSON]]
*/
package object catsinstances extends JSONInstances
package object catsinstances extends JSONInstances with FromJSONInstances with ToJSONInstances

trait JSONInstances {
implicit val catsInvariantForJSON: Invariant[JSON] = new JSONInvariant
}

trait FromJSONInstances {
implicit val catsFunctorForFromJSON: Functor[FromJSON] = new FromJSONFunctor
}

trait ToJSONInstances {
implicit val catsContravariantForToJSON: Contravariant[ToJSON] = new ToJSONContravariant
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sphere.mongo.format

import io.sphere.mongo.MongoFormatInstances
import org.bson.BSONObject

import scala.annotation.implicitNotFound
Expand All @@ -15,7 +16,7 @@ trait MongoFormat[@specialized A] extends Serializable {
val fields: Set[String] = MongoFormat.emptyFieldsSet
}

object MongoFormat {
object MongoFormat extends MongoFormatInstances {

private[MongoFormat] val emptyFieldsSet: Set[String] = Set.empty

Expand Down

0 comments on commit 99c9fb0

Please sign in to comment.