GithubHelp home page GithubHelp logo

Comments (8)

travisbrown avatar travisbrown commented on July 3, 2024

Thanks for this report! There are two issues here. The first is that you've got some maps with non-string keys. JSON doesn't support objects with non-string keys, and there are a number of ways to manage codecs for arbitrary maps—we're working on this in #186, and a solution will be available in 0.4.0, but for now you have to provide instances for Map[K, V] manually if K isn't String. This isn't too hard—you can write something like this:

def decodeMap[K, V: Decoder](fromString: String => K): Decoder[Map[K, V]] =
  Decoder[Map[String, V]].map(
    _.map {
      case (k, v) => (fromString(k), v)
    }
  )

def encodeMap[K, V: Encoder](toString: K => String): Encoder[Map[K, V]] =
  Encoder[Map[String, V]].contramap(
    _.map {
      case (k, v) => (toString(k), v)
    }
  )

And then use these to define instances for your types.

The more serious problem is that Scrooge's traits aren't sealed, so while it's possible to get a LabelledGeneric instance for demo.IntOpt.Immutable, it's not for demo.IntOpt, and that's what you need for the codecs, since that's the static type in the coproduct representations. You can derive instances for the non-Immutable types very painfully:

implicit def genIntOpt(implicit
  gen: LabelledGeneric[IntOpt.Immutable]
): LabelledGeneric.Aux[IntOpt, gen.Repr] = new LabelledGeneric[IntOpt] {
  type Repr = gen.Repr
  def to(t: IntOpt): Repr = t match {
    case i: IntOpt.Immutable => gen.to(i)
    case _ => gen.to(t.copy().asInstanceOf[IntOpt.Immutable])
  }
  def from(r: Repr): IntOpt = gen.from(r)
}

And then if you have instances for all of your maps with non-string keys (and for TFieldBlob, etc.), you can automatically derive codecs for your Scrooge types.

I'd suggest not doing this, though. It's a lot of hoops to jump through, and in many cases you're better off keeping the Scrooge-generated classes as close as possible to the boundaries of your program, anyway. If you really wanted, you could lobby for the generated traits to be sealed (see @notxcain's comments on Gitter), but I'm not sure how well-received that will be, since I don't know off the top of my head what the motivation was for leaving them open.

from circe.

arnihermann avatar arnihermann commented on July 3, 2024

in many cases you're better off keeping the Scrooge-generated classes as close as possible to the boundaries of your program, anyway

Could you expand on that? By boundaries do you mean e.g. after an RPC call to a thrift service it's better to map the data from the scrooge generated class into our own case classes?

from circe.

travisbrown avatar travisbrown commented on July 3, 2024

@arnihermann Right. I know there's some documentation somewhere that talks about best practices in that respect, but it may have been Twitter-internal—if I find it I'll post a link here.

from circe.

arnihermann avatar arnihermann commented on July 3, 2024

Thanks that'd be great, I'd love to know more about those best practices.

from circe.

dong77 avatar dong77 commented on July 3, 2024

I imported the following packages and used your code , but I still got error.

import demo._
import shapeless._
import io.circe.generic.semiauto._
import io.circe.parser._
import io.circe.syntax._
implicit val gen = LabelledGeneric[IntOpt.Immutable]

implicit def genIntOpt(implicit
  gen: LabelledGeneric[IntOpt.Immutable]
): LabelledGeneric.Aux[IntOpt, gen.Repr] = new LabelledGeneric[IntOpt] {
  type Repr = gen.Repr
  def to(t: IntOpt): Repr = t match {
    case i: IntOpt.Immutable => gen.to(i)
    case _ => gen.to(t.copy().asInstanceOf[IntOpt.Immutable])
  }
  def from(r: Repr): IntOpt = gen.from(r)
}

Compiler complains:

scala> IntOpt(Some(1)).asJson
<console>:43: error: could not find implicit value for parameter encoder: io.circe.Encoder[demo.IntOpt]
       IntOpt(Some(1)).asJson
                       ^

scala> deriveEncoder[IntOpt]
<console>:43: error: could not find Lazy implicit value of type io.circe.generic.encoding.DerivedObjectEncoder[demo.IntOpt]
       deriveEncoder[IntOpt]
                    ^

scala> deriveDecoder[IntOpt]
<console>:43: error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[demo.IntOpt]
       deriveDecoder[IntOpt]
                    ^

scala> IntOpt(Some(1)).asJson
<console>:44: error: could not find implicit value for parameter encoder: io.circe.Encoder[demo.IntOpt]
       IntOpt(Some(1)).asJson

Am I missing something?

from circe.

dong77 avatar dong77 commented on July 3, 2024

Any update? 👍

from circe.

wilkenstein avatar wilkenstein commented on July 3, 2024

This problem bit me too and confused me even further because sometimes my code would compile. I used the macro from https://github.com/travisbrown/scrooge-circe-demo/blob/master/src/main/scala/demo/package.scala to get rid of the passthrough fields, which cannot be generically derived (Map[Short, TFieldBlob]). Only after I moved the macro into its own compilation path did the code reliably compile. scalac was not expanding the macro during compilation, but wasn't giving the error that it wasn't expanding it. Now it works beautifully, though compiles slowly -- 1 minute when using finch with about 20 routes in one file.

from circe.

travisbrown avatar travisbrown commented on July 3, 2024

Closing this since scrooge-shapes now provides LabelledGeneric instances that work with circe-generic in many cases, and circe-derivation also supports derivation for Scrooge types directly in many cases.

from circe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.