argonaut-shapeless icon indicating copy to clipboard operation
argonaut-shapeless copied to clipboard

Support derivation of "incomplete" decode instances for case classes

Open travisbrown opened this issue 10 years ago • 4 comments

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.

travisbrown avatar Jun 19 '15 20:06 travisbrown

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 avatar Jun 20 '15 13:06 alexarchambault

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?

alexarchambault avatar Jun 20 '15 13:06 alexarchambault

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.

travisbrown avatar Jun 20 '15 18:06 travisbrown

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

kaiserpelagic avatar Jul 01 '15 23:07 kaiserpelagic