magnolia icon indicating copy to clipboard operation
magnolia copied to clipboard

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

Open vpavkin opened this issue 7 years ago • 4 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.

vpavkin avatar Jun 10 '18 07:06 vpavkin

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 avatar Jun 10 '18 07:06 joroKr21

@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?

vpavkin avatar Jun 10 '18 18:06 vpavkin

I achieved this like this: https://gist.github.com/nafg/ae190a71c2ede3d51b3ed026e44c8d02

nafg avatar Mar 08 '20 02:03 nafg

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

joroKr21 avatar Mar 08 '20 13:03 joroKr21