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

Support derivation of "incomplete" decode instances for case classes #15

Open
travisbrown opened this issue Jun 19, 2015 · 4 comments
Open

Comments

@travisbrown
Copy link

Suppose we've got a case class like this:

case class Foo(i: Int, s: String, blah: Boolean)

It shouldn't be too hard to derive "incomplete" DecodeJson instances:

implicitly[DecodeJson[Int => Foo]] // Decodes e.g. { "s": "foo", "blah": false }
implicitly[DecodeJson[(Int, String) => Foo]] // Decodes { "blah": false }

The input type could optionally be a record in cases where the case class has multiple members of the same type and we need to disambiguate.

@alexarchambault
Copy link
Owner

Interesting.

It seems that a "naïve" recursive implementation could be written, like:

  • defining implicits for DecodeJson[(H1 :: T1) => L2] if one for DecodeJson[T1 => L2] exists, and
  • for DecodeJson[L1 => (H2 :: T2)] if some for DecodeJson[L1 => T2] and DecodeJson[H2] exist, with L?, T? <: HList in both cases,
  • then use some Generic and FnToProduct to get the ones of your example.

It's hard to tell in advance if it will work as is (and if it doesn't, then the real combat with scalac begins :-)

@alexarchambault
Copy link
Owner

I don't know if I'll be able to find time for it soon... (If you or anyone else want to give it a try, please do.)

Is there a particular context for it?

@travisbrown
Copy link
Author

Sorry, I should have mentioned that I'm working on this now, and just put together a rough implementation.

The use case I have in mind is something like a REST API where the user can post a "partial" JSON object (e.g. a user's information but not an identifier). Having DecodeJson instances for Id => User would eliminate the need for separate partial case classes.

@kaiserpelagic
Copy link

+1 I've been looking for something like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants