Open DamianReeves opened 2 months ago
I'm taking a look at this
I have been working on something similar ,and I think it would be good to have the following questions answered sooner or later:
case class Foo(a:Int, @flatten b:Bar)
case class Bar(a:Int)
Do we allow overriding? What are the overriding rules (do we allow overriding with subclasses?)
If not, how we report, do we emit compiletime errors?
Are nested flattens allowed?
case class Foo(a:Int, @flatten b:Bar)
case class Bar(@flatten c: Baz,d:String)
case class Baz(e:Int)
Is @flatten allowed for generics?
case class Foo[T](@flatten a:T)
It might be impossible to emit compilation errors in case of generics.
Adding another question:
@flatten
on fields referring to members of sealed hierarchies? If so:
String
$type
(or other configured discriminator key) field also be flattened?sealed trait Foo
case object Bar extends Foo
case class Baz() extends Foo
case class Test(str: String, @flatten foo: Foo)
write(Bar) // "Bar"
write(Baz()) // {"$type":"Baz"}
write(Test("test", Bar)) // how would this work?
write(Test("test", Baz())) // would this be encoded like this? {"str":"test","$type":"Baz"}
@flatten c: Baz
in your example@flatten
only to case class
es is fine for now, no need to worry about sealed traits.Overall I think I'd be happy with any answer to these questions, as long as the answer is documented, tested and properly justified. Some limitations are inevitable and we just need to be clear about where we draw the line and why.
- I think nested flattens could be allowed, but you'd need it to be
@flatten c: Baz
in your example
Yeah , I forgot @flatten c:Baz, will edit
From the maintainer Li Haoyi: I'm putting a 500USD bounty on this issue, payable by bank transfer on a merged PR implementing this.
Sometimes when processing JSON you want to use a shared structure across various models. The serde crate in Rust wanted to provide support for this feature and provided it through the flatten attribute. It would be very useful if upickle also supported this.
Lets say we have an API which supports pagination as a general concept and both our users and products endpoints support pagination.
Usecase: Sharing via Flattening
It would be useful if we could use the following models to capture this.
Usecase: Extra Fields
This can also be used as a mechanism for handling extra fields like the Rust crate also does.