Extract diff between objects

MavenCentral CircleCI Coverage Status

A Scala library to extract differences between two objects. This makes easy finding out what changes are applied on the old object.

Quick example:

// Using Ammonite-REPL
@ import $ivy.`net.petitviolet::generic-diff:<VERSION>`
@ import net.petitviolet.generic.diff._
...

@ case class User(id: Long, name: String)
defined class User

@ User(1L, "alice") diff User(1L, "bob")
res2: DiffResult[User] = DiffResult(List(FieldSame("id", 1L), FieldDiff("name", "alice", "bob")))

Getting Started

libraryDependencies += "net.petitviolet" %% "generic-diff" % "<version>"

Example

import net.petitviolet.generic.diff._

case class User(id: Long, name: String, age: Int)

case class GroupName(value: String)
case class Group(id: Long, name: GroupName)

def main(args: Array[String]): Unit = {
  // extract difference between 2 objects
  val userDiff = User(1L, "alice", 20) diff User(2L, "alice", 35)

  // result contains sequence of FieldDiff and FieldSame
  assert(userDiff.fields == List(FieldDiff("id", 1, 2), FieldSame("name", "alice"), FieldDiff("age", 20, 35)))

  // dynamic field access
  assert(userDiff.name == FieldSame("name", "alice"))
  assert(userDiff.age == FieldDiff("age", 20, 35))
  // userDiff.foo // compile error!

  // extract diff from nested objects
  val groupDiff = Group(1, GroupName("tech")) diff Group(2L, GroupName("hoge"))
  assert(
    groupDiff.fields == List(
      FieldDiff("id", 1, 2),
      FieldDiff("name", GroupName("tech"), GroupName("hoge"))
    )
  )
}

Publish

sbt 'project genericDiffMacro' +publishSigned sonatypeReleaseAll \
    'project genericDiff' +publishSigned sonatypeReleaseAll

LICENSE

Apache-2.0