Skip to content

Commit

Permalink
Add @implicitNotFound for From/To/FromTo (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
htmldoug authored Jul 14, 2021
1 parent d85c163 commit d20954c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rallyhealth.weepickle.v1.core

import scala.annotation.implicitNotFound
import scala.language.experimental.macros
import scala.reflect.ClassTag

Expand All @@ -13,6 +14,7 @@ trait Types { types =>
/**
* A combined [[To]] and [[From]], along with some utility methods.
*/
@implicitNotFound("Could not find an implicit WeePickle.FromTo[${T}]. Consider adding one with `object ${T} { implicit val pickler: WeePickle.FromTo[${T}] = macroFromTo }`")
trait FromTo[T] extends From[T] with To[T] {
override def narrow[K]: FromTo[K] = this.asInstanceOf[FromTo[K]]
def bimap[In](f: In => T, g: T => In): FromTo[In] = {
Expand Down Expand Up @@ -60,6 +62,7 @@ trait Types { types =>
* A thin wrapper around [[Visitor]], but needs to be it's own class in order
* to make type inference automatically pick up it's implicit values.
*/
@implicitNotFound("Could not find an implicit WeePickle.To[${T}]. Consider adding one with `object ${T} { implicit val pickleTo: WeePickle.To[${T}] = macroTo }`")
trait To[T] extends com.rallyhealth.weepickle.v1.core.Visitor[Any, T] {

override def map[Z](f: T => Z): To[Z] = new To.MapTo[T, T, Z](To.this) {
Expand Down Expand Up @@ -105,6 +108,7 @@ trait Types { types =>
* Generally nothing more than a way of applying the `In` to
* a [[Visitor]], along with some utility methods
*/
@implicitNotFound("Could not find an implicit WeePickle.From[${In}]. Consider adding one with `object ${In} { implicit val pickleFrom: WeePickle.From[${In}] = macroFrom }`")
trait From[In] {
def narrow[K] = this.asInstanceOf[From[K]]
def transform[Out](in: In, out: Visitor[_, Out]): Out = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ object Twilight {
object Pony {
implicit val pickler = WeePickle.macroFromTo[Pony]
}
case class MissingPicklers()

object MacroTests extends TestSuite {

Expand Down Expand Up @@ -314,6 +315,12 @@ object MacroTests extends TestSuite {
test("map") {
rw[Pony](Twilight(), """{"$type": "twi"}""")
}

test("compile errors") {
compileError("implicitly[WeePickle.To[MissingPicklers]]").msg ==> """Could not find an implicit WeePickle.To[com.rallyhealth.weepickle.v1.MissingPicklers]. Consider adding one with `object com.rallyhealth.weepickle.v1.MissingPicklers { implicit val pickleTo: WeePickle.To[com.rallyhealth.weepickle.v1.MissingPicklers] = macroTo }`"""
compileError("implicitly[WeePickle.From[MissingPicklers]]").msg ==> """Could not find an implicit WeePickle.From[com.rallyhealth.weepickle.v1.MissingPicklers]. Consider adding one with `object com.rallyhealth.weepickle.v1.MissingPicklers { implicit val pickleFrom: WeePickle.From[com.rallyhealth.weepickle.v1.MissingPicklers] = macroFrom }`"""
compileError("implicitly[WeePickle.FromTo[MissingPicklers]]").msg ==> """Could not find an implicit WeePickle.FromTo[com.rallyhealth.weepickle.v1.MissingPicklers]. Consider adding one with `object com.rallyhealth.weepickle.v1.MissingPicklers { implicit val pickler: WeePickle.FromTo[com.rallyhealth.weepickle.v1.MissingPicklers] = macroFromTo }`"""
}
}
}

Expand Down

0 comments on commit d20954c

Please sign in to comment.