Could not find implicit value for parameter lgen: shapeless.LabelledGeneric.Aux
Asked Answered
S

1

6

I am new to Shapeless. I have a helper class that leverages the shapeless 'Automatic Typeclass Derivation' (https://gist.github.com/negator/fbbcd1b2ce9a85b762b7) to help me populate json reads and writes.

import play.api.libs._
import json._

import shapeless.{ `::` => :#:, _ }
import poly._

object SReads extends LabelledTypeClassCompanion[Reads] {
  object typeClass extends LabelledTypeClass[Reads] {

    def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil))

    def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] {
      case obj @ JsObject(fields) =>
      for {
        head <- FHead.reads(obj \ name)
        tail <- FTail.reads(obj - name)
        } yield head :: tail

        case _ => JsError("Json object required")
      }

    def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads)

    def emptyCoproduct: Reads[CNil] = Reads[CNil](_ => JsError("CNil object not available"))

    def coproduct[L, R <: Coproduct](
      name: String,
      cl: => Reads[L],
      cr: => Reads[R]) = Reads[L :+: R]{ js =>

      js match {
        case js @ JsString(n) if n == name => cl.reads(js).map(Inl.apply)
        case js @ _                        => cr.reads(js).map(Inr.apply)
      }
    }
  }
}

This is how I used it:

case class TrialMember(
  @Key("_id") var id: String,
  var weeks: String,
  var `type`: Option[String] = Some("email"),
  var updatedDate: Date = DateTime.now.toDate
)

object TrialMemberDao extends ModelCompanion[TrialMember, String] {
  def collection = mongoCollection("trial_member")
  val dao = new SalatDAO[TrialMember, String](collection) {}
  def emails() = dao.find(MongoDBObject("type" -> "email")).toList.map(_.id)
  def domains() = dao.find(MongoDBObject("type" -> "domain")).toList.map(_.id)
  def isTrialMember(userEmail: String): Boolean = {
    val trialMembers = emails() // whitelisted emails
    val trialDomains = domains() // whitelisted domains
    trialMembers.contains(userEmail) ||
      trialDomains.filter(userEmail.contains(_)).headOption.isDefined
  }

}

object TrialMember {
  implicit val jsonWrites: Writes[TrialMember] = SWrites.deriveInstance
  implicit val jsonReads: Reads[TrialMember] = SReads.deriveInstance
}

But after upgrading to sbt 0.13.8 and Play 2.4, now it gives me this error:

could not find implicit value for parameter lgen: shapeless.LabelledGeneric.Aux[T,LKV]
[error]   implicit val reads: Reads[TrialMember] = SReads.deriveInstance
Stenosis answered 24/11, 2015 at 19:41 Comment(2)
Please supply your Foo definition. It looks working for my case classRaddled
@Raddled I edited the question. Please take a look.Stenosis
R
2

The trouble is somewhat funny, your typeclass deriver is perfectly implemented while compiler could not find Reads instance for Option[String] which should be used for the type field. Just supply your code with something like following definition:

object optionFormats {
  def noneReads[T]: Reads[Option[T]] = Reads(Function.const(JsSuccess(None)))

  implicit def optFormat[T](implicit w: Writes[T], r: Reads[T]) =
    Format[Option[T]](
      r.map[Option[T]](Some(_)).orElse(noneReads),
      Writes(_.fold[JsValue](JsNull)(w.writes)))
}

And then in any code just

import optionFormats._

And your instance should be constructed as desired.

Raddled answered 1/12, 2015 at 13:38 Comment(2)
Where should I provide this line? I used it in object TrialMember, and it is working. But then I have to do it in all object classes. Is there a way to fix SReads instead?Stenosis
@Raddled Do you know why upgrading to sbt 0.13.8 and play 2.4 meant that old code wouldn't compile anymore?Knobkerrie

© 2022 - 2024 — McMap. All rights reserved.