Created
June 24, 2021 13:08
-
-
Save rudogma/55f86f78488fbeabf3c093df0bd5f377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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