sangria
sangria copied to clipboard
Add deriveInterfaceType
Would be cool to have this for traits
trait MyInterface {
def id: Int
name: String
}
val MyInterfaceType = deriveInterfaceType[Unit, MyInterface]()
I think that another solution that is closer to deriveObjectType could be
case class MyInterfaceImpl(id: Int, name: String) extends MyInterface
val MyInterfaceType = deriveInterfaceType[Unit, MyInterfaceImpl](InterfaceTypeName("MyInterface"))
This would help because you don't have to copy & paste the field names into another definition
Thanks for the suggestion! I was not sure whether it would be very useful since normally not many interfaces are exposed, so I haven't implemented it yet. But if you think that it can provide value, then we can add it.
One aspect we need to consider is when a trait extends another trait, e.g. trait MyInterface with MyInterface
. With case classes, it's a bit easier to figure out which fields are supposed to be exposed by default. With traits, we just get a set of members, so we need to be careful not to include such methods as equals
, hashCode
, etc.
You're right. We somehow have to blacklist some stuff and exclude default methods, such as equals by default.
For me, the solution with the case class implementing the interface would be a quick way to circumvent all those problems. I know, it's far away from being perfect, but it would deal with the (pretty standard) case that is stated in my original issue description. And the compiler would pick up any changes done in the interface which at least helps to be up to date.