复杂层次数据结构的通用diff



我有一个由事例类、映射和集合组成的复杂数据结构。我想知道是否有通用机制来计算两个结构之间的"差异"。

举个简单的例子:

val s0 = Set(1,2,3)
val s1 = Set(2,3,4)
val diff = diff(s0, s1)
// this should contain the information that 1 has been removed and 4 has
// been added
assert(s1 == diff.patch(s0))

一个更复杂的例子:

case class User(name: String, roles: Set[String])
val user0 = User("Hans", Set("Developer", "Admin")
val user1 = User("Hans", Set("Admin", "Manager")
val diff = diff(user0, user1)
// this should contain the information that "Developer" has been removed
// and "Manager" has been added to the roles field

请注意,我对通用解决方案感兴趣,该解决方案适用于任意事例类和深层层次结构。我有一种感觉,这应该是可能的,使用基于类型类的方法,或者不成形,或者一些样板。允许为新集合类型指定"diff算法"的方法可获得加分。

我发现https://github.com/stacycurl/delta。这似乎与我想要的非常接近。但我真的不明白。

有一个基于宏的库s_mach.datadiff。我没有使用过它,但首页的例子是从任意大小写类生成diff。

最新更新