Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for patching case classes with JSON #24

Merged
merged 1 commit into from
Aug 11, 2015

Conversation

travisbrown
Copy link
Member

As requested by @vkostyukov, this addition supports the derivation of case class "updaters":

scala> import io.circe.generic.auto._, io.circe.jawn._
import io.circe.generic.auto._
import io.circe.jawn._

scala> case class Foo(i: Int, xs: List[String])
defined class Foo

scala> val foo = Foo(0, Nil)
foo: Foo = Foo(0,List())

scala> decode[Foo => Foo]("""{ "i": 13 }""").map(_(foo))
res0: cats.data.Xor[io.circe.Error,Foo] = Right(Foo(13,List()))

scala> decode[Foo => Foo]("""{ "xs": ["a", "b"] }""").map(_(foo))
res1: cats.data.Xor[io.circe.Error,Foo] = Right(Foo(0,List(a, b)))

scala> decode[Foo => Foo]("""{ "xs": ["a", "b"], "i": 1 }""").map(_(foo))
res2: cats.data.Xor[io.circe.Error,Foo] = Right(Foo(1,List(a, b)))

This is closely related to "incomplete" decoder derivation and the motivation is similar: we want to avoid lots of case classes like PatchedTodo here:

case class Todo(id: String, title: String, completed: Boolean, order: Int)
case class PatchedTodo(title: Option[String], completed: Option[Boolean], order: Option[Int])

Decoder[Foo => Foo] lets us accomplish the same thing without an extra boilerplate-y case class.

@vkostyukov
Copy link
Collaborator

Omg! This is awesome! Thanks a lot.

Mind publishing a snapshot version? I'd love to use this feature in the workshop.

@codecov-io
Copy link

Current coverage is 57.14%

Merging #24 into master will increase coverage by +0.01% as of 2accfd5

Powered by Codecov. Updated on successful CI builds.

@travisbrown
Copy link
Member Author

@vkostyukov Yep, can definitely publish a snapshot. I also just found a few minor bugs in some of the cursor operations (a couple carried over from Argonaut and a couple of my own invention). These don't interfere with "normal" usage, but I might still try to pull together an 0.2.0 tonight. I'm stuck in LAX for two more hours thanks to another delayed plane, so I've got plenty of time…

travisbrown added a commit that referenced this pull request Aug 11, 2015
Add support for patching case classes with JSON
@travisbrown travisbrown merged commit 6a4800b into master Aug 11, 2015
@travisbrown travisbrown deleted the topic/patching branch August 22, 2015 19:21
julienrf pushed a commit to scalacenter/circe that referenced this pull request May 13, 2021
exclude scala-compiler dependency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants