SQiShER/java-object-diff

How to apply diffs

Kira-Cesonia opened this issue · 0 comments

My use case is that I want to store diffs and the original object in a database, and then at a later date restore different versions of the object using the diffs.

For that, I am looking for a functionality that lets me apply the diffs to the base object, like this:

	@Test
	void applyDiffDeltaShouldCorrectlyApplyDeltaToCity() {
		GameCharacter sylvia = GameCharacters.sylvia();
		GameCharacter sylviaEdited = GameCharacters.sylviaEdited();
		
		DiffNode characterDelta
			= objectDiffer.compare(sylvia, sylviaEdited);
		GameCharacter sylviaWithAppliedDelta
			= DeltaApplier.applyDelta(sylvia, characterDelta);
		
		assertEquals(sylviaEdited.getName(), sylviaWithAppliedDelta.getName());
	}

	public static <T> T applyDelta(T baseObject, DiffNode diff) {
		//This won't work, but it would be cool if it did:
		T updatedObject = baseObject + diff;
		return updatedObject ;
	}

This feels like something that java-object-diff should be able to do. However, all the examples in the documentation appear to require both the start and the end version of a document, in addition to the diffs.

So, how can I make this work? How can I create the end document just from the start document and one (or more) diffs?