softwaremill / magnolia

Easy, fast, transparent generic derivation of typeclass instances

Home Page:https://softwaremill.com/open-source/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Low priority] Option to provide semantics identical to circe semiauto

vpavkin opened this issue · comments

Let's talk about circe again :)

There's semiauto mode, which is quite handy in many cases. In this mode the deriver doesn't use itself recursively to derive underlying types. Essentially, the deriver itself is not implicit.

It's not currently possible to obtain such behaviour using magnolia. Even when I don't make it implicit, it uses itself to derive as much levels of underlying types as possible.

For example, I have this macro (notice no implicit):

object semiauto {

  type Typeclass[T] = Encoder[T]

  private[magnolia] def combine[T](caseClass: CaseClass[Typeclass, T]): Typeclass[T] =
    MagnoliaEncoder.combine(caseClass)

  private[magnolia] def dispatch[T](sealedTrait: SealedTrait[Typeclass, T]): Typeclass[T] =
    MagnoliaEncoder.dispatch(sealedTrait)

  def deriveMagnoliaEncoder[T]: Typeclass[T] = macro Magnolia.gen[T]
}

Then given this simple hierarchy, circe semiauto and magnolia behave differently.

case class I(v: String)
case class O(i: I)
import io.circe.magnolia.derivation.encoder.semiauto._

// this succeeds. Magnolia uses itself to derive codec for I as well
deriveMagnoliaEncoder[O]
import io.circe.generic.semiauto._

// this fails cause there's no implicit exists for I
deriveEncoder[O]

I wonder, if it's possible for magnolia to start using only existing implicits if the macro itself is not implicit.

I think I would prefer deriving as much as possible. If I have some nested ADT and I only care about having a codec for the top-level type, why should I have to write boilerplate for all branches and nested ADTs?

@joroKr21 makes total sense. Not proposing to change the current behaviour, just wondering how hard it is to support such a mode as an option.
I was asking mostly in the interest of making a compatible API, that behaves in exactly the same way as circe-generic semiauto. It would be convenient to have for users who want to switch to magnolia for their existing codecs.

I guess we could store this issue as a question/low_prio, in case there's interest in future. WDYT?

I guess we could just add another method to do this in Magnolia, but how would we call it?