Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 05:24
    travisbrown closed #1374
  • 05:24
    travisbrown commented #1374
  • Jan 27 06:40
    travisbrown commented #1375
  • Jan 27 06:32
    Sebruck commented #1375
  • Jan 26 15:03
    codecov-io commented #1375
  • Jan 26 15:02
    codecov-io commented #1375
  • Jan 26 15:00
    codecov-io commented #1375
  • Jan 26 14:42
    travisbrown opened #1375
  • Jan 26 14:42

    travisbrown on dotty-with-derivation

    Exclude Dotty benchmark module … (compare)

  • Jan 26 14:18

    travisbrown on dotty-with-derivation

    (compare)

  • Jan 26 14:09
    codecov-io commented #1374
  • Jan 26 14:09
    travisbrown synchronize #1374
  • Jan 26 14:09

    travisbrown on dotty

    Terrible initial derives implem… Update Dotty nightly (compare)

  • Jan 26 13:35
    codecov-io commented #1374
  • Jan 26 13:35
    travisbrown synchronize #1374
  • Jan 26 13:35

    travisbrown on dotty

    Update Dotty nightly (compare)

  • Jan 26 13:31
    codecov-io commented #1374
  • Jan 26 13:31
    travisbrown synchronize #1374
  • Jan 26 13:31

    travisbrown on dotty

    Terrible initial derives implem… (compare)

  • Jan 25 22:15
    codecov-io commented #1374
Travis Brown
@travisbrown
@steinybot no, not really in this case. the advantage of working at the decoder level is that if you only use applicative operators, you get error accumulation if you want it.
in this case you need the monadic flatMap, so you can't accumulate errors there anyway
David Leonhardt
@LeonhardtDavid
Hi! Is there a way to validate that a list has at least one element, I have a class that contains a list of other object and I need that it has at least one element.
Travis Brown
@travisbrown
@LeonhardtDavid if you can use cats.data.NonEmptyList, you get that for free during validation as well as statically encoded in your type afterwards:
scala> import io.circe.Decoder, io.circe.jawn.decode
import io.circe.Decoder
import io.circe.jawn.decode

scala> decode[cats.data.NonEmptyList[Int]]("""[]""")
res0: Either[io.circe.Error,cats.data.NonEmptyList[Int]] = Left(DecodingFailure(Attempt to decode value on failed cursor, List(DownArray)))

scala> decode[cats.data.NonEmptyList[Int]]("""[1]""")
res1: Either[io.circe.Error,cats.data.NonEmptyList[Int]] = Right(NonEmptyList(1))
if you're stuck with List, you can define a custom decoder with ensure:
scala> decode("""[]""")(Decoder.decodeList[Int].ensure(_.size > 0, "must be non-empty"))
res2: Either[io.circe.Error,List[Int]] = Left(DecodingFailure(must be non-empty, List()))

scala> decode("""[1]""")(Decoder.decodeList[Int].ensure(_.size > 0, "must be non-empty"))
res3: Either[io.circe.Error,List[Int]] = Right(List(1))
(you could make this decoder implicit if you really wanted to)
David Leonhardt
@LeonhardtDavid
That's great. Thank you very much @travisbrown !
Larry Bordowitz
@lbordowitz
@travisbrown True, this is not the default encoding. The default encoding has the name in it as an object: I get "{\"OkResponse\":{\"status\":\"shook\",\"id\":\"WhatAmIDoingWrong\"}}". I'm using circe to interop with the pre-existing Expo API written for javascript (probably type checkers like flow or typescript would work for the way they do it). I'd prefer not to use multiple json deserializers in my project; how do I salvage this?
Travis Brown
@travisbrown
@lbordowitz this Stack Overflow Q&A I did might help: https://stackoverflow.com/a/42165461/334519
Larry Bordowitz
@lbordowitz
Thank you. I also read that in the guide. I'll try out the shapeless-style approach.
Larry Bordowitz
@lbordowitz
@travisbrown it still does not work. Is that because the two case classes have the same parameter named status?
Larry Bordowitz
@lbordowitz
Adding this worked, nothing else in the stack overflow response did however.
  implicit val decodeTicketResponse: Decoder[TicketResponse] = (
    List[Decoder[TicketResponse]](
      Decoder[OkResponse].widen,
      Decoder[ErrorResponse].widen
    ).reduceLeft(_ or _)
  )
Dermot Haughey
@hderms
If I were to have a given field in a data model that could either be a local date or a zoned date time, what's the mechanism for expressing this? Is it to create a sealed trait that wraps these two fields and do something like this: https://scastie.scala-lang.org/k9Qxb1DtRoWr36SgECZAYg
etienne
@crakjie

Hello

implicit val encoder = deriveEncoder[List[T]]
implicit val decoder = deriveDecoder[List[T]]

can't be replaced trivialy by

implicit val codec = deriveCodec[List[T]]

because there is no codec for List.
But it's should be possible to make automaticaly a codec from an encoder and a decoder no?

something like implicit def from[A](implicit decodeA: Decoder[A], implicit encodeA: Encoder[A]): Codec[A]
Travis Brown
@travisbrown
@crakjie you shouldn't ever need or want to use generic derivation for List, and I really want to avoid encouraging anyone to use Codec as a constraint.
the idea is that Codec makes it easier to define instances, but that's it.
etienne
@crakjie
noted
Philipp Dörfler
@phdoerfler

I got a json object which sometimes has 80 fields and sometimes 81 fields (depending on some query parameters in the http api). And I want to have that in my code like this:

case class ThingWithExpensiveData(thing: Thing, data: ExpensiveData)
case class Thing(/* 80+ fields */)

I want to minimise boiler plate but I'm a bit stuck as to how I best tell Circe to do the heavy lifting for me. I think I am looking for something like this:

implicit val meep: Decoder[ThingWithExpensiveData] = deriveDecoder[Thing].map(thing => ThingWithExpensiveData(thing, /* yeah and here I'm a bit stuck */)
David Leonhardt
@LeonhardtDavid
@phdoerfler and why not to use an Option for the optional field?
Philipp Dörfler
@phdoerfler
@LeonhardtDavid I wish to avoid the run time check that comes with using Option
Colin Williams
@KE7CFN_twitter
given some JSON model example, is there a way to infer case classes which can used to decode this data?
Guillaume Massé
@MasseGuillaume
Hey, is it possible to customize deriveDecoder with Configuration ?
ah ops nvm I saw the import is different import io.circe.generic.extras.semiauto._ vs import io.circe.generic.semiauto._
Anthony Cerruti
@srnb_gitlab
Can I remove coproduct names with automatic derivation?
I have a sealed trait and 4 case classes that extend it, all of those case classes have unique required fields and I need them without their names
Anthony Cerruti
@srnb_gitlab

I.e. given

sealed trait Foo {
  val a: Option[String]
}
case class Bar(b: String, a: Option[String]) extends Foo
case class Baz(c: String, a: Option[String]) extends Foo

I want

{
  "b": "yes"
}

instead of

{
  "Bar": {
    "b": "yes"
  }
}
Philipp Dörfler
@phdoerfler
@KE7CFN_twitter yes there is. I know of two websites that do this for you, both flawed in different ways. First there is https://transform.tools/json-to-scala-case-class and then there is https://json2caseclass.cleverapps.io. I would argue the former is slightly less flawed than then latter but YMMV.
@KE7CFN_twitter Things you should keep in mind: Instead of creating an Option[String] (for Strings that are sometimes null) it creates lowercase string. It also fails to detect JS numbers that are larger than what can fit in a regular Int. It will still create an Int field in the case class, so that's something you have to watch out for. Circe handles BigInteger just fine so you might want to use that to be on the safe side.
@KE7CFN_twitter (That's for the converter from transform.tools. The latter one is even worse if you ask me)
Philipp Dörfler
@phdoerfler
[error] [E1] GlympseClient/src/main/scala/io/doerfler/glympse/GlympseTypes.scala
[error]      macro annotation could not be expanded (you cannot use a macro annotation in the same compilation run that defines it)
[error]      L16: @ConfiguredJsonCodec case class Login (
What am I doing wrong?
The macro is in @ConfiguredJsonCodec and it's from Circe and I'm simply using Circe, not compiling it. So what is happening here?
I'm following https://circe.github.io/circe/codecs/custom-codecs.html but it has no mention of this error
Travis Brown
@travisbrown
@srnb_gitlab does this help? https://stackoverflow.com/a/42165461/334519
Anthony Cerruti
@srnb_gitlab
@travisbrown .widen was what I was looking for!
tried to do it myself and couldn't
Travis Brown
@travisbrown
@phdoerfler these errors aren't always very helpful, and that one in particular is just wrong. what Scala version are you using? do you have the macro-paradise plugin / scalac option enabled?
Anthony Cerruti
@srnb_gitlab
Hmm actually @travisbrown if I have a ton of sealed-trait-style ADTs (like a TON) will circe-generic-extras help me or will circe-shapes do it better?
Travis Brown
@travisbrown
There are tradeoffs. Personally if I had a ton of ADTs right now I’d use codegen, probably.
Philipp Dörfler
@phdoerfler
@travisbrown I am using ThisBuild / scalaVersion := "2.13.1" and scalacOptions := Seq("-unchecked", "-deprecation", "-Ywarn-value-discard", "-language:_", "-encoding", "UTF-8", "-target:jvm-1.8"),. That's it. Am I missing a scalac option?
Oh yes I am missing -Ymacro-annotations, thank you for the hint @travisbrown
Is there a way to make the error message more helpful? I'm thinking something like the @ImplicitNotFound annotation.
I'm afraid I just read "you need macro paradise if you're using an old Scala. Otherwise you're fine". Which is of course not quite what the docs say, so mea culpa
Travis Brown
@travisbrown
@phdoerfler I don’t think so, unfortunately. I’ve not tried reporting the current incorrect error message, but it might be worth doing.
But yeah, we should update the Circe docs if they don’t mention the scalac flag on 2.13.
Billzabob
@Billzabob

I’m new to circe and trying to figure out how to get a Configuration to work with semi automatic derivation. I have this and it’s not working

case class ThirdPartyAppMessage(appId: String)

object ThirdPartyAppMessage {
  implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames
  implicit val decoder: Decoder[ThirdPartyAppMessage] = deriveDecoder
  implicit val encoder: Encoder[ThirdPartyAppMessage] = deriveEncoder
}

But appId isn’t showing up as app_id like I want it to. I’ve also tried putting the config above where I call asJson but that didn’t work either.

Travis Brown
@travisbrown
@Billzabob are you importing deriveDecoder and deriveEncoder from io.circe.generic.extras.semiauto? if you import the ordinary circe-generic ones the configuration will just be ignored.
This is annoying but I don't really know how to improve it.
Colin Williams
@KE7CFN_twitter
@phdoerfler thanks for sharing. That seems like it could be pretty useful if needing to create a few CC's based on deeply nested and large json objects.. Thought maybe it would be some convenience function. And maybe I'm being sort of lazy.