Last active
October 31, 2017 20:08
-
-
Save andyscott/b8b7375b7db653224438c28ed8e8d887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 shapeless.Generic | |
import shapeless.HList | |
import shapeless.ops.hlist.FilterNot | |
import java.util.UUID | |
// compare case classes but ignore UUID fields in the comparison | |
object ExampleApp { | |
final case class Foo( | |
a: String, | |
b: Int, | |
c: UUID, | |
d: Int, | |
e: UUID) | |
def main(args: Array[String]): Unit = { | |
val foo0 = Foo("hello", 1, UUID.randomUUID, 0, UUID.randomUUID) | |
val foo1 = Foo("hello", 1, UUID.randomUUID, 0, UUID.randomUUID) | |
println(foo0 == foo1) // false | |
println(DataEq[Foo].eqv(foo0, foo1)) // true | |
} | |
} | |
/** A type class used to determine the equality of the data between two | |
* types | |
*/ | |
sealed trait DataEq[A] { | |
/** Returns true of the data for x and the data for y | |
* are equivalent | |
*/ | |
def eqv(x: A, y: A): Boolean | |
} | |
object DataEq { | |
def apply[A](implicit ev: DataEq[A]): DataEq[A] = ev | |
def instance[A](f: (A, A) => Boolean): DataEq[A] = new DataEq[A] { | |
def eqv(x: A, y: A): Boolean = f(x, y) | |
} | |
implicit def genProductDataEqIgnoreUUID[A, L0 <: HList, L1 <: HList]( | |
implicit | |
gen: Generic.Aux[A, L0], | |
fil: FilterNot.Aux[L0, UUID, L1] | |
): DataEq[A] = instance((x, y) => | |
fil(gen.to(x)) == fil(gen.to(y))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment