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

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

Comments

3 participants
@travisbrown

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

This comment has been minimized.

Show comment
Hide comment
@alexarchambault

alexarchambault Jun 20, 2015

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 :-)

Owner

alexarchambault commented Jun 20, 2015

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

This comment has been minimized.

Show comment
Hide comment
@alexarchambault

alexarchambault Jun 20, 2015

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?

Owner

alexarchambault commented Jun 20, 2015

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

This comment has been minimized.

Show comment
Hide comment
@travisbrown

travisbrown Jun 20, 2015

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.

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

This comment has been minimized.

Show comment
Hide comment
@kaiserpelagic

kaiserpelagic Jul 1, 2015

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

+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