Skip to content

Instantly share code, notes, and snippets.

@rudogma
Created June 24, 2021 13:08
Show Gist options
  • Save rudogma/55f86f78488fbeabf3c093df0bd5f377 to your computer and use it in GitHub Desktop.
Save rudogma/55f86f78488fbeabf3c093df0bd5f377 to your computer and use it in GitHub Desktop.
import io.circe.Encoder
import io.circe.syntax._
trait HasId[ID] {
def id: ID
}
case class MyEntity(field1: Int, id: String) extends HasId[String]
case class CustomId(field1:Int)
object CustomId {
implicit val myid:MyId[CustomId,Int] = _.field1
}
trait MyId[-E,Id]{
def apply(v:E):Id
}
object MyId {
implicit def myId_hasId_compat[E <: HasId[T], T]:MyId[HasId[T],T] = (v: HasId[T]) => v.id
}
def kokoko[Entity,T](entity: Entity)(implicit Id:MyId[Entity,T], enc: Encoder[T]): Unit = {
println(Id(entity).asJson)
}
val inst = MyEntity(1, "kek")
kokoko(inst)
kokoko(CustomId(555))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment