argonaut-shapeless
argonaut-shapeless copied to clipboard
Support derivation of "incomplete" decode instances for case classes
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.
Interesting.
It seems that a "naïve" recursive implementation could be written, like:
- defining implicits for
DecodeJson[(H1 :: T1) => L2]if one forDecodeJson[T1 => L2]exists, and - for
DecodeJson[L1 => (H2 :: T2)]if some forDecodeJson[L1 => T2]andDecodeJson[H2]exist, withL?, T? <: HListin both cases, - then use some
GenericandFnToProductto 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 :-)
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?
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.
+1 I've been looking for something like this.